1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
Routing
=======
@todo Note that this documentation is for the new routing interface
not available in master at this point.
Purpose
-------
Cowboy does nothing by default.
To make Cowboy useful, you need to map URLs to Erlang modules that will
handle the requests. This is called routing.
When Cowboy receives a request, it tries to match the requested host and
path to the resources given in the dispatch rules. If it matches, then
the associated Erlang code will be executed.
Routing rules are given per host. Cowboy will first match on the host,
and then try to find a matching path.
Routes need to be compiled before they can be used by Cowboy.
Structure
---------
The general structure for the routes is defined as follow.
``` erlang
Routes = [Host1, Host2, ... HostN].
```
Each host contains matching rules for the host along with optional
constraints, and a list of routes for the path component.
``` erlang
Host1 = {HostMatch, PathsList}.
Host2 = {HostMatch, Constraints, PathsList}.
```
The list of routes for the path component is defined similar to the
list of hosts.
``` erlang
PathsList = [Path1, Path2, ... PathN].
```
Finally, each path contains matching rules for the path along with
optional constraints, and gives us the handler module to be used
along with options that will be given to it on initialization.
```
Path1 = {PathMatch, Handler, Module}.
Path2 = {PathMatch, Constraints, Handler, Module}.
```
Continue reading to learn more about the match syntax and the optional
constraints.
Match syntax
------------
The match syntax is used to associate host names and paths with their
respective handlers.
The match syntax is the same for host and path with a few subtleties.
Indeed, the segments separator is different, and the host is matched
starting from the last segment going to the first. All examples will
feature both host and path match rules and explain the differences
when encountered.
Excluding special values that we will explain at the end of this section,
the simplest match value is a host or a path. It can be given as either
a `string()` or a `binary()`.
``` erlang
PathMatch1 = "/".
PathMatch2 = "/path/to/resource".
HostMatch1 = "cowboy.example.org".
```
As you can see, all paths defined this way must start with a slash
character. Note that these two paths are identical as far as routing
is concerned.
``` erlang
PathMatch2 = "/path/to/resource".
PathMatch3 = "/path/to/resource/".
```
Hosts with and without a trailing dot are equivalent for routing.
Similarly, hosts with and without a leading dot are also equivalent.
``` erlang
HostMatch1 = "cowboy.example.org".
HostMatch2 = "cowboy.example.org.".
HostMatch3 = ".cowboy.example.org".
```
It is possible to extract segments of the host and path and to store
the values in the `Req` object for later use. We call these kind of
values bindings.
The syntax for bindings is very simple. A segment that begins with
the `:` character means that what follows until the end of the segment
is the name of the binding in which the segment value will be stored.
``` erlang
PathMatch = "/hats/:name/prices".
HostMatch = ":subdomain.example.org".
```
If these two end up matching when routing, you will end up with two
bindings defined, `subdomain` and `hat_name`, each containing the
segment value where they were defined. For example, the URL
`http://test.example.org/hats/wild_cowboy_legendary/prices` will
result in having the value `test` bound to the name `subdomain`
and the value `wild_cowboy_legendary` bound to the name `hat_name`.
They can later be retrieved using `cowboy_req:binding/{2,3}`. The
binding name must be given as an atom.
There is a special binding name you can use to mimic the underscore
variable in Erlang. Any match against the `_` binding will succeed
but the data will be discarded. This is especially useful for
matching against many domain names in one go.
``` erlang
HostMatch = "ninenines.:_".
```
Similarly, it is possible to have optional segments. Anything
between brackets is optional.
``` erlang
PathMatch = "/hats/[page/:number]".
HostMatch = "[www.]ninenines.eu".
```
You can also have imbricated optional segments.
``` erlang
PathMatch = "/hats/[page/[:number]]".
```
You can retrieve the rest of the host or path using `[...]`.
In the case of hosts it will match anything before, in the case
of paths anything after the previously matched segments. It is
a special case of optional segments, in that it can have
zero, one or many segments. You can then find the segments using
`cowboy_req:host_info/1` and `cowboy_req:path_info/1` respectively.
They will be represented as a list of segments.
``` erlang
PathMatch = "/hats/[...]".
HostMatch = "[...]ninenines.eu".
```
Finally, if a binding appears twice in the routing rules, then the
match will succeed only if they share the same value. This copies
the Erlang pattern matching behavior.
``` erlang
PathMatch = "/hats/:name/:name".
```
This is also true when an optional segment is present. In this
case the two values must be identical only if the segment is
available.
``` erlang
PathMatch = "/hats/:name/[:name]".
```
If a binding is defined in both the host and path, then they must
also share the same value.
``` erlang
PathMatch = "/:user/[...]".
HostMatch = ":user.github.com".
```
Constraints
-----------
After the matching has completed, the resulting bindings can be tested
against a set of constraints. The match will succeed only if they all
succeed.
They are always given as a two or three elements tuple, where the first
element is the name of the binding, the second element is the constraint's
name, and the optional third element is the constraint's arguments.
The following constraints are currently defined:
* {Name, int}
* {Name, function, (fun(Value) -> true | {true, NewValue} | false)}
The `int` constraint will check if the binding is a binary string
representing an integer, and if it is, will convert the value to integer.
The `function` constraint will pass the binding value to a user specified
function that receives the binary value as its only argument and must
return whether it fulfills the constraint, optionally modifying the value.
Note that constraint functions SHOULD be pure and MUST NOT crash.
Compilation
-----------
The structure defined in this chapter needs to be compiled before it is
passed to Cowboy. This allows Cowboy to efficiently lookup the correct
handler to run instead of having to parse the routes repeatedly.
This can be done with a simple call to `cowboy_routing:compile/1`.
``` erlang
{ok, Routes} = cowboy_routing:compile([
%% {HostMatch, list({PathMatch, Handler, Opts})}
{'_', [{'_', my_handler, []}]}
]),
%% Name, NbAcceptors, TransOpts, ProtoOpts
cowboy:start_http(my_http_listener, 100,
[{port, 8080}],
[{env, [{routes, Routes}]}]
).
```
Note that this function will return `{error, badarg}` if the structure
given is incorrect.
|