gflare/router

Types

pub type ParamNode {
  ParamNode(name: String, node: TreeNode)
}

Constructors

  • ParamNode(name: String, node: TreeNode)
pub type RouteParams {
  RouteParams(params: List(#(String, String)))
}

Constructors

  • RouteParams(params: List(#(String, String)))
pub type Router {
  Router(
    tree: TreeNode,
    global_middleware: List(Middleware),
    error_handler: fn(request.HttpRequest, String) -> response.Response,
    not_found_handler: Handler,
  )
}

Constructors

pub type TreeNode {
  TreeNode(
    children: dict.Dict(String, TreeNode),
    param_child: option.Option(ParamNode),
    wildcard_child: option.Option(WildcardNode),
    handlers: dict.Dict(String, Handler),
    middleware: List(Middleware),
  )
}

Constructors

pub type WildcardNode {
  WildcardNode(name: String, handler: Handler)
}

Constructors

  • WildcardNode(name: String, handler: Handler)

Values

pub fn any(
  router: Router,
  path: String,
  handler: Handler,
) -> Router

Add a route for all HTTP methods.

pub fn delete(
  router: Router,
  path: String,
  handler: Handler,
) -> Router

Add a DELETE route to the router.

pub fn get(
  router: Router,
  path: String,
  handler: Handler,
) -> Router

Add a GET route to the router.

pub fn get_param(
  params: RouteParams,
  name: String,
) -> option.Option(String)

Get a route parameter by name. Returns Some(value) if found, None otherwise.

pub fn get_param_or(
  params: RouteParams,
  name: String,
  default: String,
) -> String

Get a route parameter by name with a default value if not found.

pub fn group(
  router: Router,
  prefix: String,
  middleware: List(Middleware),
  configure: fn(Router) -> Router,
) -> Router

Create a route group with a shared prefix and middleware.

pub fn new() -> Router

Create a new empty router.

pub fn not_found(router: Router, handler: Handler) -> Router

Set a custom 404 handler for the router.

pub fn on_error(
  router: Router,
  handler: fn(request.HttpRequest, String) -> response.Response,
) -> Router

Set a custom error handler for the router.

pub fn options(
  router: Router,
  path: String,
  handler: Handler,
) -> Router

Add an OPTIONS route to the router.

pub fn patch(
  router: Router,
  path: String,
  handler: Handler,
) -> Router

Add a PATCH route to the router.

pub fn post(
  router: Router,
  path: String,
  handler: Handler,
) -> Router

Add a POST route to the router.

pub fn put(
  router: Router,
  path: String,
  handler: Handler,
) -> Router

Add a PUT route to the router.

pub fn serve(
  router: Router,
  request: request.HttpRequest,
  env: bindings.Env,
  ctx: worker.Context,
) -> promise.Promise(response.Response)

Serve a request using the router. Returns a Promise with the response.

pub fn with_middleware(
  router: Router,
  middleware: Middleware,
) -> Router

Add global middleware to the router.

Search Document