glimr/http/middleware


Middleware Helper

Utility for applying multiple middleware functions in sequence. Middleware are applied in order, with each having access to the request and context, and ability to call the next middleware in the chain.

Values

pub fn apply(
  middleware_list: List(
    fn(
      request.Request(wisp.Connection),
      context,
      fn(request.Request(wisp.Connection)) -> response.Response(
        wisp.Body,
      ),
    ) -> response.Response(wisp.Body),
  ),
  req: request.Request(wisp.Connection),
  ctx: context,
  next: fn(request.Request(wisp.Connection)) -> response.Response(
    wisp.Body,
  ),
) -> response.Response(wisp.Body)

Apply Middleware

Applies a list of middleware functions in sequence to a request. Each middleware receives the request, context, and a ‘next’ function to continue the chain. Middleware execute in order: [first, second, third] → first wraps second wraps third.

This is useful when you want to apply multiple middleware to a specific route without adding them to the route group’s global middleware stack.


Example:

pub fn routes(path, method, req, ctx) {
  case path, method {
    ["admin"], Get -> {
      use req <- middleware.apply([auth, admin_check], req, ctx)
      admin_controller.show(req, ctx)
    }

    ["contact"], Get -> contact_controller.show(req, ctx)

    _, _ -> wisp.response(404)
  }
}
Search Document