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), context) -> response.Response(
        wisp.Body,
      ),
    ) -> response.Response(wisp.Body),
  ),
  req: request.Request(wisp.Connection),
  ctx: context,
  next: fn(request.Request(wisp.Connection), context) -> 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 can modify both the request and context, with changes flowing through to subsequent middleware and the final handler.

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:

// admin_controller.gleam
pub fn show(req: Request, ctx: Context) -> Response {
  use req, ctx <- middleware.apply([auth, admin_check], req, ctx)

  // handle the rest of your controller logic
}
Search Document