| 1 |
= cowboy_router(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_router - Router middleware |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
The `cowboy_router` middleware maps the requested host and |
| 10 |
path to the handler to be used for processing the request. |
| 11 |
|
| 12 |
The router takes the `dispatch` rules as input from the |
| 13 |
middleware environment. Dispatch rules are generated by |
| 14 |
calling the |
| 15 |
link:man:cowboy_router:compile(3)[cowboy_router:compile(3)] |
| 16 |
function. The environment can contain the rules directly |
| 17 |
or a tuple `{persistent_term, Key}`, in which case Cowboy |
| 18 |
will call `persistent_term:get(Key)` to retrieve the |
| 19 |
dispatch rules. |
| 20 |
|
| 21 |
When a route matches, the router sets the `handler` and |
| 22 |
`handler_opts` middleware environment values containing |
| 23 |
the handler module and initial state, respectively. |
| 24 |
|
| 25 |
The router will stop execution when no route matches. |
| 26 |
It will send a 400 response if no host was found, and |
| 27 |
a 404 response otherwise. |
| 28 |
|
| 29 |
== Exports |
| 30 |
|
| 31 |
* link:man:cowboy_router:compile(3)[cowboy_router:compile(3)] - Compile routes to the resources |
| 32 |
|
| 33 |
== Types |
| 34 |
|
| 35 |
=== bindings() |
| 36 |
|
| 37 |
[source,erlang] |
| 38 |
---- |
| 39 |
bindings() :: #{atom() => any()} |
| 40 |
---- |
| 41 |
|
| 42 |
Bindings found during routing. |
| 43 |
|
| 44 |
=== dispatch_rules() |
| 45 |
|
| 46 |
Opaque type containing the compiled routes. |
| 47 |
|
| 48 |
=== routes() |
| 49 |
|
| 50 |
[source,erlang] |
| 51 |
---- |
| 52 |
routes() = [ |
| 53 |
{Host, PathList} | |
| 54 |
{Host, Fields, PathList} |
| 55 |
] |
| 56 |
|
| 57 |
PathList :: [ |
| 58 |
{Path, Handler, InitialState} | |
| 59 |
{Path, Fields, Handler, InitialState} |
| 60 |
] |
| 61 |
|
| 62 |
Host :: '_' | iodata() |
| 63 |
Path :: '_' | iodata() |
| 64 |
Fields :: cowboy:fields() |
| 65 |
Handler :: module() |
| 66 |
InitialState :: any() |
| 67 |
---- |
| 68 |
|
| 69 |
Human readable list of routes to handlers. |
| 70 |
|
| 71 |
Cowboy uses this list to map hosts and paths, optionally |
| 72 |
augmented with constraints applied to the bindings, to |
| 73 |
handler modules. |
| 74 |
|
| 75 |
The syntax for routes is currently defined in the user guide. |
| 76 |
|
| 77 |
// @todo The syntax should probably be in this module, |
| 78 |
// and the user guide show more practical examples. |
| 79 |
|
| 80 |
=== tokens() |
| 81 |
|
| 82 |
[source,erlang] |
| 83 |
---- |
| 84 |
tokens() :: [binary()] |
| 85 |
---- |
| 86 |
|
| 87 |
List of `host_info` and `path_info` tokens that were found |
| 88 |
using the `...` syntax. |
| 89 |
|
| 90 |
== See also |
| 91 |
|
| 92 |
link:man:cowboy(7)[cowboy(7)], |
| 93 |
link:man:cowboy_req:binding(3)[cowboy_req:binding(3)], |
| 94 |
link:man:cowboy_req:bindings(3)[cowboy_req:bindings(3)], |
| 95 |
link:man:cowboy_req:host_info(3)[cowboy_req:host_info(3)], |
| 96 |
link:man:cowboy_req:path_info(3)[cowboy_req:path_info(3)] |