livery_router (livery v0.1.0)

View Source

Radix-style path-segment router.

Routes are compiled into an immutable trie. Each segment of a pattern is one of:

  • static, e.g. users in /users/new;
  • parameter, prefixed with :, e.g. :id in /users/:id;
  • wildcard, prefixed with *, e.g. *path in /files/*path, matching all remaining segments joined back with /.

match/3 returns {ok, Handler, Bindings, Meta} on a method-aware hit, {error, {method_not_allowed, Methods}} when the path matches but no route is registered for the requested method, or {error, not_found} otherwise.

Summary

Functions

Build a router from a list of routes in one shot.

Look up a route by method and path.

Empty router with no routes.

Types

bindings()

-type bindings() :: #{binary() => binary()}.

handler()

-type handler() :: term().

meta()

-type meta() :: term().

method()

-type method() :: binary() | '_'.

pattern()

-type pattern() :: binary().

router()

-opaque router()

Functions

add(Method, Pattern, Handler, Meta, Router)

-spec add(method(), pattern(), handler(), meta(), router()) -> router().

Insert one route.

Method may be '_' to match any HTTP method. Later additions with the same Method+Pattern replace the previous entry.

compile(Routes)

-spec compile([{method(), pattern(), handler()} | {method(), pattern(), handler(), meta()}]) -> router().

Build a router from a list of routes in one shot.

match(Method, Path, Router)

-spec match(method(), binary(), router()) ->
               {ok, handler(), bindings(), meta()} |
               {error, not_found} |
               {error, {method_not_allowed, [method()]}}.

Look up a route by method and path.

new()

-spec new() -> router().

Empty router with no routes.