middleware
Built-in middleware functions. All follow the fn(Conn(body), fn(Conn(body)) -> Response) -> Response signature.
Types
pub type CorsOptions {
CorsOptions(
allow_origins: List(String),
allow_methods: List(String),
allow_headers: List(String),
max_age_seconds: Option(Int),
)
}Functions
log
pub fn log(c: Conn(Connection), next: fn(Conn(Connection)) -> Response(ResponseData)) -> Response(ResponseData)Logs method, path, status code, and duration using the logging library.
request_id
pub fn request_id(c: Conn(body), next: fn(Conn(body)) -> Response(ResponseData)) -> Response(ResponseData)Reads X-Request-Id from the incoming request; generates a UUID v4 if absent. Sets X-Request-Id on the response.
cors
pub fn cors(opts: CorsOptions, c: Conn(body), next: fn(Conn(body)) -> Response(ResponseData)) -> Response(ResponseData)Adds CORS headers based on opts. Handles OPTIONS preflight requests with a 204 response.
cors_allow_all
pub fn cors_allow_all() -> CorsOptionsReturns permissive CorsOptions with * origin and common methods. Useful for development.
static_files
pub fn static_files(under prefix: String, from dir: String) -> Middleware(Connection)Serves files from dir on disk under the given URL prefix.
body_limit
pub fn body_limit(limit_bytes: Int) -> Middleware(Connection)Reads and buffers the full request body, rejecting requests larger than limit_bytes with 413. Converts Conn(Connection) to Conn(BitArray) for downstream handlers.
Request body helpers (in mistweaver/request)
These live in mistweaver/request and require the body to have been buffered first by body_limit.
json_body
pub fn json_body(req: Request(BitArray), using decoder: decode.Decoder(a)) -> Result(a, String)Decode the request body as JSON using the given decoder. Returns Error if the body is not valid UTF-8, not valid JSON, or does not match the decoder.
form_params
pub fn form_params(req: Request(BitArray)) -> List(#(String, String))Parse a URL-encoded (application/x-www-form-urlencoded) body into a key/value list.
form_param
pub fn form_param(params: List(#(String, String)), key: String) -> Option(String)Look up a single field from a pre-parsed form params list.