glimr/router
Router
Web apps and API endpoints need different middleware – HTML error pages vs JSON responses, static file serving vs CORS headers. Route groups let you split routes by prefix (e.g. “/api” vs everything else) and wire each group to the right middleware stack automatically.
Types
Each group bundles a URL prefix, a middleware stack, and the compiled route handler for that section of the app. The prefix enables fast matching – “/api/users/123” checks if the path starts with “/api” before even looking at the route table, and lazily loads only the matching module.
pub type RouteGroup(context) {
RouteGroup(
prefix: String,
middleware_group: middleware.MiddlewareGroup,
routes: fn(List(String), http.Method, context) -> response.Response(
wisp.Body,
),
)
}
Constructors
-
RouteGroup( prefix: String, middleware_group: middleware.MiddlewareGroup, routes: fn(List(String), http.Method, context) -> response.Response( wisp.Body, ), )
A typed struct so the route compiler can pattern match on the middleware variant at code generation time. The prefix determines which routes belong to this group, the name becomes the generated module name, and the middleware controls which kernel pipeline is wired in at compile time.
pub type RouteGroupConfig {
RouteGroupConfig(
name: String,
prefix: String,
middleware: middleware.MiddlewareGroup,
)
}
Constructors
-
RouteGroupConfig( name: String, prefix: String, middleware: middleware.MiddlewareGroup, )
Values
pub fn handle(
ctx: context.Context(app),
route_groups: List(RouteGroup(context.Context(app))),
kernel_handle: fn(
context.Context(app),
middleware.MiddlewareGroup,
fn(context.Context(app)) -> response.Response(wisp.Body),
) -> response.Response(wisp.Body),
) -> response.Response(wisp.Body)
The main request entry point. Splits the URL into segments, finds the first route group whose prefix matches, applies that group’s middleware, and calls its route handler. Groups are checked in order – put specific prefixes like “/api” before the catch-all “” so they get a chance to match.
pub fn load(
routes_for: fn(String) -> fn(List(String), http.Method, context) -> response.Response(
wisp.Body,
),
) -> List(RouteGroup(context))
Route groups are defined in config/route_group.toml so
adding a new API version or admin section is a config
change, not a code change. The routes_for callback maps
each group name to its compiled route handler so the router
knows which module handles which prefix.