livery_static (livery v0.2.0)

View Source

Static-directory file handler.

Returns a handler that serves files from a root directory, inferring Content-Type from the extension, emitting a weak ETag for conditional GET, honoring Range, and confining every path under the root so a request can never traverse out of it.

Mount it on a router wildcard route and read the captured sub-path:

Router = livery_router:add(
    '_', <<"/assets/*path">>, livery_static:handler("priv/assets"), #{}, Router0
).

Without a router binding it falls back to stripping a configured prefix from the request path. Options (all optional): binding (default <<"path">>), prefix, index (default <<"index.html">>, or false), cache_control (binary | directive list | undefined), etag (default true), range (default true).

Security: the sub-path is percent-decoded and then confined - any .. segment, control byte, or escape is rejected - and only regular files are served (directories and symlinks yield 404).

Summary

Functions

Static handler serving regular files under Root.

Types

opts()

-type opts() ::
          #{binding => binary(),
            prefix => binary(),
            index => binary() | false,
            cache_control => binary() | [livery_resp:cache_directive()] | undefined,
            etag => boolean(),
            range => boolean()}.

Functions

handler(Root)

-spec handler(file:name_all()) -> livery_middleware:handler().

Static handler serving regular files under Root.

handler(Root, Opts)

-spec handler(file:name_all(), opts()) -> livery_middleware:handler().

handler/1 with options.