nova_router behaviour (nova v0.18.1)

View Source

Router module for nova. This module is responsible for compiling routes, dispatching requests to the correct handler and managing the routing table. It also exposes an API for modulating the routing table at runtime.

Summary

Functions

Works the same way as add_routes/2 but with the exception that you don't need to provide the routes explicitly. When using this it's expected that there's a routing-module associated with the application. Eg. for the application 'test' the corresponding router would then be 'test_router'. Read more about routers in the official documentation.

Add routes to the dispatch-table for the given app. The routes can be either a list of maps or a map. It use the same structure as the routes-callback in the router-module.

As add_routes/2, but against the routing table addressed by DispatchKey.

Compile the given applications into the routing table addressed by DispatchKey, merging into whatever is already stored there.

Forget a routing table entirely. Called when the listener that owned it is stopped, so its routes and compiled-application list do not outlive it. The default table belongs to the bootstrap listener and is never deleted.

Remove all routes associated with the given application.

As remove_application/1, but against the routing table addressed by DispatchKey.

Types

bindings()

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

dispatch_key()

-type dispatch_key() :: term().

lookup_result()

-type lookup_result() ::
          {ok, bindings(), Value :: term()} |
          {ok, bindings(), Value :: term(), PathInfo :: [binary()]} |
          {error, not_found} |
          {error, comparator_not_found, AllowedMethods :: [binary()]}.

Callbacks

controllers(Env)

(optional)
-callback controllers(Env :: atom()) -> Controllers :: [module() | {module(), map()}].

routes(Env)

(optional)
-callback routes(Env :: atom()) -> Routes :: [map()].

Functions

add_routes(App)

-spec add_routes(App :: atom()) -> ok.

Works the same way as add_routes/2 but with the exception that you don't need to provide the routes explicitly. When using this it's expected that there's a routing-module associated with the application. Eg. for the application 'test' the corresponding router would then be 'test_router'. Read more about routers in the official documentation.

add_routes(App, Routes)

-spec add_routes(App :: atom(), Routes :: [map()] | map()) -> ok.

Add routes to the dispatch-table for the given app. The routes can be either a list of maps or a map. It use the same structure as the routes-callback in the router-module.

add_routes(App, Routes, DispatchKey)

-spec add_routes(App :: atom(), Routes :: [map()] | map(), DispatchKey :: dispatch_key()) -> ok.

As add_routes/2, but against the routing table addressed by DispatchKey.

compile(Apps)

-spec compile(Apps :: [atom() | {atom(), map()}]) -> nova_routing_trie:trie().

compile(Apps, DispatchKey)

-spec compile(Apps :: [atom() | {atom(), map()}], DispatchKey :: dispatch_key()) ->
                 nova_routing_trie:trie().

Compile the given applications into the routing table addressed by DispatchKey, merging into whatever is already stored there.

compiled_apps()

-spec compiled_apps() -> [{App :: atom(), Prefix :: list()}].

compiled_apps(DispatchKey)

-spec compiled_apps(DispatchKey :: dispatch_key()) -> [{App :: atom(), Prefix :: list()}].

delete_dispatch(DispatchKey)

-spec delete_dispatch(DispatchKey :: dispatch_key()) -> ok.

Forget a routing table entirely. Called when the listener that owned it is stopped, so its routes and compiled-application list do not outlive it. The default table belongs to the bootstrap listener and is never deleted.

execute(Req, Env)

-spec execute(Req, Env :: cowboy_middleware:env()) -> {ok, Req, Env0} | {stop, Req}
                 when Req :: cowboy_req:req(), Env0 :: cowboy_middleware:env().

lookup_url(Path)

-spec lookup_url(Path :: nova_routing_trie:path()) -> lookup_result().

lookup_url(Host, Path)

-spec lookup_url(Host :: binary() | atom(), Path :: nova_routing_trie:path()) -> lookup_result().

lookup_url(Host, Path, Method)

-spec lookup_url(Host :: binary() | atom(),
                 Path :: nova_routing_trie:path(),
                 Method :: nova_routing_trie:comparator()) ->
                    lookup_result().

plugins()

remove_application(Application)

-spec remove_application(Application :: atom()) -> ok.

Remove all routes associated with the given application.

remove_application(Application, DispatchKey)

-spec remove_application(Application :: atom(), DispatchKey :: dispatch_key()) -> ok.

As remove_application/1, but against the routing table addressed by DispatchKey.

render_status_page(StatusCode, Req)

-spec render_status_page(StatusCode :: integer(), Req :: cowboy_req:req()) ->
                            {ok, Req0 :: cowboy_req:req(), Env :: map()}.

render_status_page(StatusCode, Data, Req)

-spec render_status_page(StatusCode :: integer(), Data :: map(), Req :: cowboy_req:req()) ->
                            {ok, Req0 :: cowboy_req:req(), Env :: map()}.

render_status_page(Host, StatusCode, Data, Req, Env)

-spec render_status_page(Host :: binary() | atom(),
                         StatusCode :: integer(),
                         Data :: map(),
                         Req :: cowboy_req:req(),
                         Env :: map()) ->
                            {ok, Req0 :: cowboy_req:req(), Env :: map()}.

routes(Env)

-spec routes(Env :: atom()) -> [map()].