glimr/http/middleware

Middleware Helper

Sometimes a single route needs extra middleware that the rest of its group doesn’t — auth checks on an admin page, rate limiting on a login endpoint. Rather than creating a whole new route group for one handler, this module lets you apply a list of middleware inline with use.

Values

pub fn apply(
  middleware_list: List(
    fn(
      context.Context(app),
      fn(context.Context(app)) -> response.Response(wisp.Body),
    ) -> response.Response(wisp.Body),
  ),
  ctx: context.Context(app),
  next: fn(context.Context(app)) -> response.Response(wisp.Body),
) -> response.Response(wisp.Body)

Wraps a handler in multiple middleware without needing a route group. Middleware execute in list order — [auth, rate_limit] means auth runs first, then rate_limit, then your handler. Context modifications flow through the chain so auth can add user info that the handler sees.

Example:

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

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