Tanguv0.35.0
Web & HTTP29 exports
Raw Markdown

/web/app/middleware

Production HTTP middlewares: CSRF, CORS, session, rate limiting, security headers, auth, tracing, and scripts injection.

(use /web/app/middleware)

Constants#

*scripts-accumulator*#

constant · Any

Dynamic accumulator collecting ES module script specifiers during SSR or Stage rendering.

Value:(Array)

*scripts-seen*#

constant · Any

Dynamic deduplication set tracking script specifiers already emitted for the current response.

Value:none

default-scripts-insertion-marker#

constant · Any

Default HTML placeholder comment replaced with injected ES module `<script type="module">` tags.

Value:"<!--scripts-middleware-slot-->"

Functions & symbols#

allow-methods-middleware#

function
(allow-methods-middleware allowed-methods next-handler)

Rejects methods outside allowlist with 405 and Allow header.

ParameterTypeOptionalDescription
allowed-methodsAnyrequired—
next-handlerAnyrequired—
Returns:Any

basic-auth-middleware#

function
(basic-auth-middleware ...args)

Enforces HTTP Basic authentication using `verify-fn` (supports curried `(basic-auth-middleware verify-fn)` and direct forms).

ParameterTypeOptionalDescription
argsAnyoptional—
Returns:Any

bearer-auth-middleware#

function
(bearer-auth-middleware verify-fn next-handler)

Authenticates API requests via `Authorization: Bearer <token>`. Supports both `(bearer-auth-middleware verify-fn next-handler)` and curried `((bearer-auth-middleware verify-fn) next-handler)` forms.

ParameterTypeOptionalDescription
verify-fnAnyrequired—
next-handlerAnyrequired—
Returns:Any

body-size-limit-middleware#

function
(body-size-limit-middleware max-bytes next-handler)

Limits request payload size and returns 413 when known size exceeds max-bytes.

ParameterTypeOptionalDescription
max-bytesAnyrequired—
next-handlerAnyrequired—
Returns:Any

cors-middleware#

function
(cors-middleware ...args)

Middleware applying CORS headers and handling preflight `OPTIONS` requests according to `options`.

ParameterTypeOptionalDescription
argsAnyoptional—
Returns:Any

csrf-middleware#

function
(csrf-middleware ...args)

Middleware validating synchronizer/double-submit CSRF tokens on state-changing HTTP requests.

ParameterTypeOptionalDescription
argsAnyoptional—
Returns:Any

current-form-of#

function
(current-form-of req)

Extracts the validated form struct stored on `req:attrs:_form` by `validate-form-middleware`.

ParameterTypeOptionalDescription
reqAnyrequired—
Returns:Any

etag-middleware#

function
(etag-middleware inner-handler)

Middleware that generates Weak ETags (W/"<16-hex-hash>") using a-hash (15+ GB/s) for GET/HEAD 200 OK responses. Returns 304 Not Modified with zero body payload when the client If-None-Match header matches.

ParameterTypeOptionalDescription
inner-handlerAnyrequired—
Returns:Any

hreflang-alternates#

function
(hreflang-alternates spec)

Renders `#html` `<link rel="alternate" hreflang="..." href="...">` elements from `{:base-url :path :alternates :x-default}`.

ParameterTypeOptionalDescription
specAnyrequired—
Returns:Any

i18n-middleware#

function
(i18n-middleware ...args)

Web middleware negotiating locale and binding `*locale*` / `*bundle*`. Supports `(i18n-middleware next-handler)`, `(i18n-middleware options)` (curried), and `(i18n-middleware options next-handler)`.

ParameterTypeOptionalDescription
argsAnyoptional—
Returns:Any

inject-scripts-into-html#

function
(inject-scripts-into-html html scripts insertion-marker)

Injects script imports at the configured marker or before </body>.

ParameterTypeOptionalDescription
htmlAnyrequired—
scriptsAnyrequired—
insertion-markerAnyrequired—
Returns:Any

method-override-middleware#

function
(method-override-middleware ...args)

Middleware that rewrites `POST` requests to `PUT`, `PATCH`, or `DELETE` via `X-HTTP-Method-Override` header or `_method` form field.

ParameterTypeOptionalDescription
argsAnyoptional—
Returns:Any

mount-localized-routes#

function
(mount-localized-routes prefixes ...rest)

Mounts a list of localized route prefixes into `/web/app` `:scope` blocks matching `router.tan`'s contract. Usage: `(mount-localized-routes prefixes routes)` or `(mount-localized-routes prefixes middlewares routes)`.

ParameterTypeOptionalDescription
prefixesAnyrequired—
restAnyoptional—
Returns:Any

negotiate-locale#

function
(negotiate-locale req ...rest)

Negotiates the active BCP 47 locale tag for `req`.

ParameterTypeOptionalDescription
reqAnyrequired—
restAnyoptional—
Returns:Any

negotiate-locale-info#

function
(negotiate-locale-info req ...rest)

Negotiates the active locale and returns `{:locale tag :source :path | :resolver | :cookie | :accept-language | :default}`.

ParameterTypeOptionalDescription
reqAnyrequired—
restAnyoptional—
Returns:Any

parse-accept-language#

function
(parse-accept-language header-str)

Parses an RFC 4647 `Accept-Language` header into quality-sorted `{:tag :language :region :q}` entries.

ParameterTypeOptionalDescription
header-strAnyrequired—
Returns:Any

rate-limit-middleware#

function
(rate-limit-middleware ...args)

Bounded in-memory fixed-window rate limiter middleware. Supports `(rate-limit-middleware options next-handler)`, `(rate-limit-middleware next-handler)`, and curried `((rate-limit-middleware options) next-handler)`.

ParameterTypeOptionalDescription
argsAnyoptional—
Returns:Any

request-id-middleware#

function
(request-id-middleware next-handler)

Ensures request-id is available in attrs and returned in response headers.

ParameterTypeOptionalDescription
next-handlerAnyrequired—
Returns:Any

scripts-middleware#

function
(scripts-middleware ...args)

Middleware that accumulates `(use-script ...)` module specifiers and injects them into HTML or Stage slot responses.

ParameterTypeOptionalDescription
argsAnyoptional—
Returns:Any

security-headers-middleware#

function
(security-headers-middleware ...args)

Middleware injecting defensive HTTP security headers (CSP, X-Frame-Options, Referrer-Policy, X-Content-Type-Options).

ParameterTypeOptionalDescription
argsAnyoptional—
Returns:Any

session-middleware#

function
(session-middleware store options)

Session middleware factory. Creates middleware that manages sessions via cookies. Parameters: store - Session store instance (from construct-session-store) options - Configuration map: :cookie-name - Cookie name (default: "SESSION-ID") :max-age - Session lifetime in seconds (default: 3600) :secure - Set Secure flag on cookie (default: false) :same-site - SameSite policy: "Strict", "Lax", or "None" (default: "Lax") :path - Cookie path (default: "/") :domain - Cookie domain (default: not set)

ParameterTypeOptionalDescription
storeAnyrequired—
optionsAnyrequired—
Returns:Any

static-files-middleware#

function
(static-files-middleware next-handler)

Middleware serving static files from `./public` before falling back to `next-handler`.

ParameterTypeOptionalDescription
next-handlerAnyrequired—
Returns:Any

traceparent-middleware#

function
(traceparent-middleware next-handler)

Middleware propagating W3C `traceparent` / `x-trace-id` distributed tracing context headers.

ParameterTypeOptionalDescription
next-handlerAnyrequired—
Returns:Any

tracing-middleware#

function
(tracing-middleware next-handler)

Middleware logging request method, URI, response status, and elapsed duration.

ParameterTypeOptionalDescription
next-handlerAnyrequired—
Returns:Any

use-script#

function
(use-script script)

Registers an ES module script specifier to be imported on the current page or Stage slot payload.

ParameterTypeOptionalDescription
scriptAnyrequired—
Returns:Any

validate-form-middleware#

function
(validate-form-middleware struct-type ...rest)

Route middleware that parses and validates `req` into `struct-type`, short-circuiting with 422 on `ValidationErrors` and storing the valid struct at `req:attrs:_form`.

ParameterTypeOptionalDescription
struct-typeAnyrequired—
restAnyoptional—
Returns:Any

Related modules