glimr/routing/router
Router
Pattern matching router with prefix-based route groups for organizing routes with shared middleware. Provides type-safe parameter extraction and lazy loading of route handlers based on URL prefix matching.
Types
Groups routes together with a shared middleware group and handler function. Routes use pattern matching for type-safe parameter extraction. The prefix field enables efficient route matching and lazy loading of route modules.
pub type RouteGroup(context) {
RouteGroup(
prefix: String,
middleware_group: kernel.MiddlewareGroup,
routes: fn(
List(String),
http.Method,
request.Request(wisp.Connection),
context,
) -> response.Response(wisp.Body),
)
}
Constructors
-
RouteGroup( prefix: String, middleware_group: kernel.MiddlewareGroup, routes: fn( List(String), http.Method, request.Request(wisp.Connection), context, ) -> response.Response(wisp.Body), )
Values
pub fn handle(
req: request.Request(wisp.Connection),
ctx: ctx,
route_groups: List(RouteGroup(ctx)),
kernel_handle: fn(
request.Request(wisp.Connection),
ctx,
kernel.MiddlewareGroup,
fn(request.Request(wisp.Connection)) -> response.Response(
wisp.Body,
),
) -> response.Response(wisp.Body),
) -> response.Response(wisp.Body)
Main entry point for routing HTTP requests. Matches the request path against registered route groups, applies middleware, and calls the route handler with the full path.
Route groups are checked in order. The first matching prefix wins. Empty prefix (“”) acts as a catch-all and should always be last in your route group list.
Flow for GET /api/users/123:
- Parse path into [“api”, “users”, “123”]
- Find group with prefix “/api”
- Apply API middleware group
- Call routes([“api”, “users”, “123”], Get, req, ctx)
pub fn register(
routes_for: fn(String) -> fn(
List(String),
http.Method,
request.Request(wisp.Connection),
context,
) -> response.Response(wisp.Body),
) -> List(RouteGroup(context))
Registers route groups by loading config from config/route_group.toml and attaching route handlers. Takes a loader function that returns the routes function for each named group.