0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00
cowboy/doc/src/manual/cowboy_router.asciidoc

97 lines
2.2 KiB
Text
Raw Normal View History

= cowboy_router(3)
== Name
2016-12-22 12:53:21 +01:00
cowboy_router - Router middleware
== Description
The `cowboy_router` middleware maps the requested host and
path to the handler to be used for processing the request.
2016-12-22 12:53:21 +01:00
The router takes the `dispatch` rules as input from the
middleware environment. Dispatch rules are generated by
calling the
link:man:cowboy_router:compile(3)[cowboy_router:compile(3)]
function. The environment can contain the rules directly
or a tuple `{persistent_term, Key}`, in which case Cowboy
will call `persistent_term:get(Key)` to retrieve the
dispatch rules.
2016-12-22 12:53:21 +01:00
When a route matches, the router sets the `handler` and
`handler_opts` middleware environment values containing
the handler module and initial state, respectively.
2016-12-22 12:53:21 +01:00
The router will stop execution when no route matches.
It will send a 400 response if no host was found, and
a 404 response otherwise.
2016-12-22 12:53:21 +01:00
== Exports
* link:man:cowboy_router:compile(3)[cowboy_router:compile(3)] - Compile routes to the resources
== Types
2016-12-22 12:53:21 +01:00
=== bindings()
2016-12-22 12:53:21 +01:00
[source,erlang]
----
bindings() :: #{atom() => any()}
2016-12-22 12:53:21 +01:00
----
2016-12-22 12:53:21 +01:00
Bindings found during routing.
2016-12-22 12:53:21 +01:00
=== dispatch_rules()
2016-12-22 12:53:21 +01:00
Opaque type containing the compiled routes.
2016-12-22 12:53:21 +01:00
=== routes()
2016-12-22 12:53:21 +01:00
[source,erlang]
----
routes() = [
{Host, PathList} |
{Host, Fields, PathList}
]
2016-12-22 12:53:21 +01:00
PathList :: [
{Path, Handler, InitialState} |
{Path, Fields, Handler, InitialState}
]
2016-12-22 12:53:21 +01:00
Host :: '_' | iodata()
Path :: '_' | iodata()
Fields :: cowboy:fields()
Handler :: module()
InitialState :: any()
----
2016-12-22 12:53:21 +01:00
Human readable list of routes to handlers.
2016-12-22 12:53:21 +01:00
Cowboy uses this list to map hosts and paths, optionally
augmented with constraints applied to the bindings, to
handler modules.
2016-12-22 12:53:21 +01:00
The syntax for routes is currently defined in the user guide.
// @todo The syntax should probably be in this module,
// and the user guide show more practical examples.
=== tokens()
[source,erlang]
----
tokens() :: [binary()]
----
List of `host_info` and `path_info` tokens that were found
using the `...` syntax.
2016-12-22 12:53:21 +01:00
== See also
2016-12-22 12:53:21 +01:00
link:man:cowboy(7)[cowboy(7)],
link:man:cowboy_req:binding(3)[cowboy_req:binding(3)],
link:man:cowboy_req:bindings(3)[cowboy_req:bindings(3)],
link:man:cowboy_req:host_info(3)[cowboy_req:host_info(3)],
link:man:cowboy_req:path_info(3)[cowboy_req:path_info(3)]