Skip to content

router #

fn new_radix_node #

fn new_radix_node(path string, node_type NodeType) &RadixNode

new_radix_node creates a new radix tree node

fn new_radix_tree #

fn new_radix_tree() &RadixTree

new_radix_tree creates a new empty radix tree

fn new_router #

fn new_router() &Router

new_router creates a new router

fn normalize_path #

fn normalize_path(path string) string

normalize_path normalizes a route path for consistent matching- Ensures path starts with /

  • Removes trailing slashes (except for root)
  • Collapses consecutive slashes

fn parse_path #

fn parse_path(path string) []PathSegment

parse_path parses a route path into segments

fn parse_segment #

fn parse_segment(segment string) PathSegment

parse_segment parses a single path segment and determines its type

fn run_benchmarks #

fn run_benchmarks()

Run all benchmarks

fn validate_path #

fn validate_path(path string) !

validate_path validates a route path and returns an error if invalid

type HandlerFunc #

type HandlerFunc = fn (mut ctx ttp.Context) http.Response

HandlerFunc is the same as http.HandlerFunc for convenience

enum NodeType #

enum NodeType {
	static   // Static path segment (e.g., /posts)
	param    // Parameter segment (e.g., /:id)
	catchall // Catch-all segment (e.g., /*path)
}

NodeType represents the type of a radix tree node

struct PathSegment #

struct PathSegment {
pub:
	value      string   // The segment value (without : or * prefix)
	node_type  NodeType // Type of segment
	param_name string   // Parameter name for param/catchall segments
}

PathSegment represents a parsed segment of a route path

struct RadixNode #

struct RadixNode {
pub mut:
	path       string       // Path segment for this node
	node_type  NodeType     // Type of node (static, param, catchall)
	children   []&RadixNode // Child nodes
	indices    string       // First character of each child's path (for fast lookup)
	handler_id int          // Handler ID if this is a leaf (-1 if not a leaf)
	priority   u32          // Priority for child ordering (higher = checked first)
	param_name string       // Parameter name for param nodes (e.g., "id" for /:id)
}

RadixNode represents a node in the radix tree

fn (RadixNode) increment_priority #

fn (mut node RadixNode) increment_priority()

increment_priority increases the priority of this node

fn (RadixNode) add_child #

fn (mut node RadixNode) add_child(child &RadixNode)

add_child adds a child node and updates indices

fn (RadixNode) get_child_index #

fn (node &RadixNode) get_child_index(c u8) int

get_child_index returns the index of the child that matches the first character Returns -1 if no child matches

fn (RadixNode) insert_child #

fn (mut node RadixNode) insert_child(pos int, child &RadixNode)

insert_child inserts a child at the specified position, maintaining priority order

fn (RadixNode) sort_children #

fn (mut node RadixNode) sort_children()

sort_children sorts children by priority (highest first)

fn (RadixNode) split_at #

fn (mut node RadixNode) split_at(pos int) &RadixNode

split_at splits this node at the given position Returns a new node containing the suffix after the split point

fn (RadixNode) find_longest_common_prefix #

fn (node &RadixNode) find_longest_common_prefix(path string) int

find_longest_common_prefix finds the length of the longest common prefix between this node's path and the given path

struct RadixTree #

struct RadixTree {
pub mut:
	root     &RadixNode    // Root node of the tree
	handlers []HandlerFunc // Handler function registry
}

RadixTree is a radix tree for storing and matching routes

fn (RadixTree) insert #

fn (mut tree RadixTree) insert(path string, handler HandlerFunc) !

insert adds a route to the radix tree

fn (RadixTree) lookup #

fn (tree &RadixTree) lookup(path string) RouteMatch

lookup searches for a route match in the tree

struct RouteInfo #

struct RouteInfo {
pub:
	method  string       // HTTP method (GET, POST, etc.)
	path    string       // Route path pattern
	handler ?HandlerFunc // Route handler function (optional)
}

RouteInfo contains metadata about a registered route

struct RouteMatch #

struct RouteMatch {
pub:
	handler_id int               // Index into handler registry
	params     map[string]string // Extracted route parameters
	matched    bool              // Whether a route was matched
}

RouteMatch represents the result of a route lookup

struct Router #

@[heap]
struct Router {
pub mut:
	trees              map[string]&RadixTree    // One radix tree per HTTP method
	routes             []RouteInfo              // Metadata about registered routes
	middleware_structs []middleware.IMiddleware // Struct-based middleware (v0.2.0+)
	renderer           &templates.Renderer = unsafe { nil } // Template renderer (optional)
	db                 &vareldb.DB         = unsafe { nil } // Database connection (single process)
	has_db             bool // Flag: database configured
}

Router manages routing trees for different HTTP methods

fn (Router) add_route #

fn (mut r Router) add_route(method string, path string, handler HandlerFunc) !

add_route registers a route for a specific HTTP method

fn (Router) count_routes #

fn (r &Router) count_routes() int

count_routes returns the total number of registered routes

fn (Router) delete #

fn (mut r Router) delete(path string, handler HandlerFunc) !

delete registers a DELETE route

fn (Router) get #

fn (mut r Router) get(path string, handler HandlerFunc) !

get registers a GET route

fn (Router) get_handler #

fn (r &Router) get_handler(method string, handler_id int) ?HandlerFunc

get_handler retrieves a handler function by its ID for a specific method

fn (Router) get_routes_for_method #

fn (r &Router) get_routes_for_method(method string) []RouteInfo

get_routes_for_method returns all routes for a specific HTTP method

fn (Router) group #

fn (mut r Router) group(prefix string) &RouterGroup

group creates a new route group with a path prefix

fn (Router) head #

fn (mut r Router) head(path string, handler HandlerFunc) !

head registers a HEAD route

fn (Router) match_route #

fn (r &Router) match_route(method string, path string) RouteMatch

match_route finds a matching route for the given method and path

fn (Router) options #

fn (mut r Router) options(path string, handler HandlerFunc) !

options registers an OPTIONS route

fn (Router) patch #

fn (mut r Router) patch(path string, handler HandlerFunc) !

patch registers a PATCH route

fn (Router) post #

fn (mut r Router) post(path string, handler HandlerFunc) !

post registers a POST route

fn (Router) put #

fn (mut r Router) put(path string, handler HandlerFunc) !

put registers a PUT route

fn (Router) use_middleware #

fn (mut r Router) use_middleware(mw middleware.IMiddleware)

use_middleware adds struct-based middleware to the router (v0.2.0+) This is the new preferred way to register middleware

struct RouterGroup #

struct RouterGroup {
mut:
	router             &Router
	prefix             string
	middleware_structs []middleware.IMiddleware // Struct-based middleware (v0.2.0+)
}

RouterGroup represents a group of routes with a common prefix and middleware

fn (RouterGroup) use_middleware #

fn (mut group RouterGroup) use_middleware(mw middleware.IMiddleware)

use_middleware adds struct-based middleware to this group (v0.2.0+) This is the new preferred way to register middleware

fn (RouterGroup) get #

fn (mut group RouterGroup) get(path string, handler HandlerFunc) !

get registers a GET route in this group

fn (RouterGroup) post #

fn (mut group RouterGroup) post(path string, handler HandlerFunc) !

post registers a POST route in this group

fn (RouterGroup) put #

fn (mut group RouterGroup) put(path string, handler HandlerFunc) !

put registers a PUT route in this group

fn (RouterGroup) delete #

fn (mut group RouterGroup) delete(path string, handler HandlerFunc) !

delete registers a DELETE route in this group

fn (RouterGroup) patch #

fn (mut group RouterGroup) patch(path string, handler HandlerFunc) !

patch registers a PATCH route in this group

fn (RouterGroup) head #

fn (mut group RouterGroup) head(path string, handler HandlerFunc) !

head registers a HEAD route in this group

fn (RouterGroup) options #

fn (mut group RouterGroup) options(path string, handler HandlerFunc) !

options registers an OPTIONS route in this group

fn (RouterGroup) subgroup #

fn (mut group RouterGroup) subgroup(prefix string) &RouterGroup

subgroup creates a nested group with combined prefixes and middleware