-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPlug.Router.html
More file actions
318 lines (248 loc) · 12.6 KB
/
Plug.Router.html
File metadata and controls
318 lines (248 loc) · 12.6 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<!DOCTYPE html>
<html>
<head>
<title>Plug.Router</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
<script type="text/javascript" charset="utf-8">
relpath = '';
if (relpath != '') relpath += '/';
</script>
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
</head>
<body>
<script type="text/javascript" charset="utf-8">
if (window.top.frames.main) document.body.className = 'frames';
</script>
<div id="content">
<div class="breadcrumbs">plug v0.5.1 → <a href="overview.html">Overview</a> → <a href="Plug.html">Plug</a> → <a href="Plug.Router.html">Router</a></div>
<h1>
Plug.Router
</h1>
<ul class="summary_links">
<li><a href="#summary">Summary</a></li>
<li><a href="#macros_details">Macros</a></li>
</ul>
<div id="moduledoc" class="docstring">
<p>A DSL to define a routing algorithm that works with Plug.</p>
<p>It provides a set of macros to generate routes. For example:</p>
<pre><code>defmodule AppRouter do
use Plug.Router
import Plug.Conn
plug :match
plug :dispatch
get "/hello" do
send_resp(conn, 200, "world")
end
match _ do
send_resp(conn, 404, "oops")
end
end</code></pre>
<p>Each route needs to return a connection, as per the Plug spec. A catch all <code>match</code> is recommended to be defined, as in the example above, otherwise routing fails with a function clause error.</p>
<p>The router is a plug, which means it can be invoked as:</p>
<pre><code>AppRouter.call(conn, [])</code></pre>
<p>Notice the router contains a plug stack and by default it requires two plugs: <code>match</code> and <code>dispatch</code>. <code>match</code> is responsible for finding a matching route which is then forwarded to <code>dispatch</code>. This means users can easily hook into the router mechanism and add behaviour before match, before dispatch or after both.</p>
<h2 id="routes">Routes</h2>
<pre><code>get "/hello" do
send_resp(conn, 200, "world")
end</code></pre>
<p>In the example above, a request will only match if it is a <code>GET</code> request and the route "/hello". The supported HTTP methods are <code>get</code>, <code>post</code>, <code>put</code>, <code>patch</code>, <code>delete</code> and <code>options</code>.</p>
<p>A route can also specify parameters which will then be available in the function body:</p>
<pre><code>get "/hello/:name" do
send_resp(conn, 200, "hello #{name}")
end</code></pre>
<p>Routes allow for globbing which will match the remaining parts of a route and can be available as a parameter in the function body, also note that a glob can't be followed by other segments:</p>
<pre><code>get "/hello/*_rest" do
send_resp(conn, 200, "matches all routes starting with /hello")
end
get "/hello/*glob" do
send_resp(conn, 200, "route after /hello: #{inspect glob}")
end</code></pre>
<p>Finally, a general <code>match</code> function is also supported:</p>
<pre><code>match "/hello" do
send_resp(conn, 200, "world")
end</code></pre>
<p>A <code>match</code> will match any route regardless of the HTTP method. Check <a href="#match/3"><code>match/3</code></a> for more information on how route compilation works and a list of supported options.</p>
<h2 id="routes-compilation">Routes compilation</h2>
<p>All routes are compiled to a match function that receives three arguments: the method, the request path split on "/" and the connection. Consider this example:</p>
<pre><code>match "/foo/bar", via: :get do
send_resp(conn, 200, "hello world")
end</code></pre>
<p>It is compiled to:</p>
<pre><code>defp match("GET", ["foo", "bar"], conn) do
send_resp(conn, 200, "hello world")
end</code></pre>
<p>This opens up a few possibilities. First, guards can be given to match:</p>
<pre><code>match "/foo/:bar" when size(bar) <= 3, via: :get do
send_resp(conn, 200, "hello world")
end</code></pre>
<p>Second, a list of splitten paths (which is the compiled result) is also allowed:</p>
<pre><code>match ["foo", bar], via: :get do
send_resp(conn, 200, "hello world")
end</code></pre>
<p>After a match is found, the block given as <code>do/end</code> is stored as a function in the connection. This function is then retrieved and invoked in the <code>dispatch</code> plug.</p>
</div>
<h2 id="summary">Summary<div class="detail_header_links"><a class="to_top_link" href="#content" title="To the top of the page">↑</a></div></h2>
<table class="summary">
<tr>
<td class="summary_signature"><a href="#delete/2">delete(path, contents)</a></td>
<td class="summary_synopsis"><p>Dispatches to the path only if it is delete request. See <a href="#match/3"><code>match/3</code></a> for more examples</p>
</td>
</tr>
<tr>
<td class="summary_signature"><a href="#forward/2">forward(path, options)</a></td>
<td class="summary_synopsis"><p>Forwards requests to another Plug. The path_info of the forwarded connection will exclude the portion of the path specified in the call to <code>forward</code></p>
</td>
</tr>
<tr>
<td class="summary_signature"><a href="#get/2">get(path, contents)</a></td>
<td class="summary_synopsis"><p>Dispatches to the path only if it is get request. See <a href="#match/3"><code>match/3</code></a> for more examples</p>
</td>
</tr>
<tr>
<td class="summary_signature"><a href="#match/3">match(expression, options, contents \\ [])</a></td>
<td class="summary_synopsis"><p>Main API to define routes. It accepts an expression representing the path and many options allowing the match to be configured</p>
</td>
</tr>
<tr>
<td class="summary_signature"><a href="#options/2">options(path, contents)</a></td>
<td class="summary_synopsis"><p>Dispatches to the path only if it is options request. See <a href="#match/3"><code>match/3</code></a> for more examples</p>
</td>
</tr>
<tr>
<td class="summary_signature"><a href="#patch/2">patch(path, contents)</a></td>
<td class="summary_synopsis"><p>Dispatches to the path only if it is patch request. See <a href="#match/3"><code>match/3</code></a> for more examples</p>
</td>
</tr>
<tr>
<td class="summary_signature"><a href="#post/2">post(path, contents)</a></td>
<td class="summary_synopsis"><p>Dispatches to the path only if it is post request. See <a href="#match/3"><code>match/3</code></a> for more examples</p>
</td>
</tr>
<tr>
<td class="summary_signature"><a href="#put/2">put(path, contents)</a></td>
<td class="summary_synopsis"><p>Dispatches to the path only if it is put request. See <a href="#match/3"><code>match/3</code></a> for more examples</p>
</td>
</tr>
</table>
<div id="macros_details" class="details_list">
<h2>Macros</h2>
<div class="detail">
<div class="detail_header" id="delete/2">
<span class="signature"><strong>delete(path, contents)</strong></span>
<div class="detail_header_links">
<span class="detail_type">(macro)</span>
<a href="#delete/2" class="detail_link" title="Link to this macro">#</a>
<a class="to_top_link" href="#content" title="To the top of the page">↑</a>
</div>
</div>
<div class="docstring"><p>Dispatches to the path only if it is delete request. See <a href="#match/3"><code>match/3</code></a> for more examples.</p>
</div>
</div>
<div class="detail">
<div class="detail_header" id="forward/2">
<span class="signature"><strong>forward(path, options)</strong></span>
<div class="detail_header_links">
<span class="detail_type">(macro)</span>
<a href="#forward/2" class="detail_link" title="Link to this macro">#</a>
<a class="to_top_link" href="#content" title="To the top of the page">↑</a>
</div>
</div>
<div class="docstring"><p>Forwards requests to another Plug. The path_info of the forwarded connection will exclude the portion of the path specified in the call to <code>forward</code>.</p>
<h2 id="examples">Examples</h2>
<pre><code>forward "/users", to: UserRouter</code></pre>
<h2 id="options">Options</h2>
<p><code>forward</code> accepts the following options:</p>
<ul>
<li><code>:to</code> - a Plug where the requests will be forwarded</li>
</ul>
<p>All remaining options are passed to the underlying plug.</p>
</div>
</div>
<div class="detail">
<div class="detail_header" id="get/2">
<span class="signature"><strong>get(path, contents)</strong></span>
<div class="detail_header_links">
<span class="detail_type">(macro)</span>
<a href="#get/2" class="detail_link" title="Link to this macro">#</a>
<a class="to_top_link" href="#content" title="To the top of the page">↑</a>
</div>
</div>
<div class="docstring"><p>Dispatches to the path only if it is get request. See <a href="#match/3"><code>match/3</code></a> for more examples.</p>
</div>
</div>
<div class="detail">
<div class="detail_header" id="match/3">
<span class="signature"><strong>match(expression, options, contents \\ [])</strong></span>
<div class="detail_header_links">
<span class="detail_type">(macro)</span>
<a href="#match/3" class="detail_link" title="Link to this macro">#</a>
<a class="to_top_link" href="#content" title="To the top of the page">↑</a>
</div>
</div>
<div class="docstring"><p>Main API to define routes. It accepts an expression representing the path and many options allowing the match to be configured.</p>
<h2 id="examples">Examples</h2>
<pre><code>match "/foo/bar", via: :get do
send_resp(conn, 200, "hello world")
end</code></pre>
<h2 id="options">Options</h2>
<p><code>match</code> accepts the following options:</p>
<ul>
<li><code>:via</code> - matches the route against some specific HTTP methods</li>
<li><code>:do</code> - contains the implementation to be invoked in case the route matches</li>
</ul>
</div>
</div>
<div class="detail">
<div class="detail_header" id="options/2">
<span class="signature"><strong>options(path, contents)</strong></span>
<div class="detail_header_links">
<span class="detail_type">(macro)</span>
<a href="#options/2" class="detail_link" title="Link to this macro">#</a>
<a class="to_top_link" href="#content" title="To the top of the page">↑</a>
</div>
</div>
<div class="docstring"><p>Dispatches to the path only if it is options request. See <a href="#match/3"><code>match/3</code></a> for more examples.</p>
</div>
</div>
<div class="detail">
<div class="detail_header" id="patch/2">
<span class="signature"><strong>patch(path, contents)</strong></span>
<div class="detail_header_links">
<span class="detail_type">(macro)</span>
<a href="#patch/2" class="detail_link" title="Link to this macro">#</a>
<a class="to_top_link" href="#content" title="To the top of the page">↑</a>
</div>
</div>
<div class="docstring"><p>Dispatches to the path only if it is patch request. See <a href="#match/3"><code>match/3</code></a> for more examples.</p>
</div>
</div>
<div class="detail">
<div class="detail_header" id="post/2">
<span class="signature"><strong>post(path, contents)</strong></span>
<div class="detail_header_links">
<span class="detail_type">(macro)</span>
<a href="#post/2" class="detail_link" title="Link to this macro">#</a>
<a class="to_top_link" href="#content" title="To the top of the page">↑</a>
</div>
</div>
<div class="docstring"><p>Dispatches to the path only if it is post request. See <a href="#match/3"><code>match/3</code></a> for more examples.</p>
</div>
</div>
<div class="detail">
<div class="detail_header" id="put/2">
<span class="signature"><strong>put(path, contents)</strong></span>
<div class="detail_header_links">
<span class="detail_type">(macro)</span>
<a href="#put/2" class="detail_link" title="Link to this macro">#</a>
<a class="to_top_link" href="#content" title="To the top of the page">↑</a>
</div>
</div>
<div class="docstring"><p>Dispatches to the path only if it is put request. See <a href="#match/3"><code>match/3</code></a> for more examples.</p>
</div>
</div>
</div>
</div>
</body>
</html>