reitit/basics.html
2017-10-22 14:51:17 +00:00

682 lines
27 KiB
HTML

<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Route syntax · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="basics.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" >
<span>
Basics
</span>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="basics.html">
<a href="basics.html#route-syntax">
Route syntax
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="basics.html">
<a href="basics.html#router">
Router
</a>
</li>
<li class="chapter " data-level="1.2.3" data-path="basics.html">
<a href="basics.html#path-based-routing">
Path-based Routing
</a>
</li>
<li class="chapter " data-level="1.2.4" data-path="basics.html">
<a href="basics.html#name-based-routing">
Name-based Routing
</a>
</li>
<li class="chapter " data-level="1.2.5" data-path="basics.html">
<a href="basics.html#route-data">
Route data
</a>
</li>
<li class="chapter " data-level="1.2.6" data-path="basics.html">
<a href="basics.html#different-routers">
Different Routers
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" >
<span>
Advanced
</span>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="route_conflicts.html">
<a href="route_conflicts.html">
Route conflicts
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="route_validation.html">
<a href="route_validation.html">
Route Validation
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="configuring_routers.html">
<a href="configuring_routers.html">
Configuring routers
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" >
<span>
Ring
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="ring.html">
<a href="ring.html">
Ring-router
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="dynamic_extensions.html">
<a href="dynamic_extensions.html">
Dynamic extensions
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="parameter_coercion.html">
<a href="parameter_coercion.html">
Parameter coercion
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="compiling_middleware.html">
<a href="compiling_middleware.html">
Compiling middleware
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" >
<span>
TODO: Swagger & OpenAPI
</span>
</li>
<li class="chapter " data-level="1.6" >
<span>
TODO: Interceptors
</span>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >Route syntax</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="route-syntax">Route Syntax</h1>
<p>Raw routes are defined as vectors, which have a String path, optional (non-sequential) route argument and optional child routes. Routes can be wrapped in vectors and lists and <code>nil</code> routes are ignored. Paths can have path-parameters (<code>:id</code>) or catch-all-parameters (<code>*path</code>).</p>
<p>Simple route:</p>
<pre><code class="lang-clj">[<span class="hljs-string">&quot;/ping&quot;</span>]
</code></pre>
<p>Two routes:</p>
<pre><code class="lang-clj">[[<span class="hljs-string">&quot;/ping&quot;</span>]
[<span class="hljs-string">&quot;/pong&quot;</span>]]
</code></pre>
<p>Routes with route arguments:</p>
<pre><code class="lang-clj">[[<span class="hljs-string">&quot;/ping&quot;</span> <span class="hljs-symbol">::ping</span>]
[<span class="hljs-string">&quot;/pong&quot;</span> {<span class="hljs-symbol">:name</span> <span class="hljs-symbol">::pong</span>}]]
</code></pre>
<p>Routes with path parameters:</p>
<pre><code class="lang-clj">[[<span class="hljs-string">&quot;/users/:user-id&quot;</span>]
[<span class="hljs-string">&quot;/api/:version/ping&quot;</span>]]
</code></pre>
<p>Route with catch-all parameter:</p>
<pre><code class="lang-clj">[<span class="hljs-string">&quot;/public/*path&quot;</span>]
</code></pre>
<p>Nested routes:</p>
<pre><code class="lang-clj">[<span class="hljs-string">&quot;/api&quot;</span>
[<span class="hljs-string">&quot;/admin&quot;</span> {<span class="hljs-symbol">:middleware</span> [<span class="hljs-symbol">::admin</span>]}
[<span class="hljs-string">&quot;&quot;</span> <span class="hljs-symbol">::admin</span>]
[<span class="hljs-string">&quot;/db&quot;</span> <span class="hljs-symbol">::db</span>]]
[<span class="hljs-string">&quot;/ping&quot;</span> <span class="hljs-symbol">::ping</span>]]
</code></pre>
<p>Same routes flattened:</p>
<pre><code class="lang-clj">[[<span class="hljs-string">&quot;/api/admin&quot;</span> {<span class="hljs-symbol">:middleware</span> [<span class="hljs-symbol">::admin</span>], <span class="hljs-symbol">:name</span> <span class="hljs-symbol">::admin</span>}]
[<span class="hljs-string">&quot;/api/admin/db&quot;</span> {<span class="hljs-symbol">:middleware</span> [<span class="hljs-symbol">::admin</span>], <span class="hljs-symbol">:name</span> <span class="hljs-symbol">::db</span>}]
[<span class="hljs-string">&quot;/api/ping&quot;</span> {<span class="hljs-symbol">:name</span> <span class="hljs-symbol">::ping</span>}]]
</code></pre>
<p>As routes are just data, it&apos;s easy to create them programamtically:</p>
<pre><code class="lang-clj">(<span class="hljs-name"><span class="hljs-builtin-name">defn</span></span> cqrs-routes [actions dev-mode?]
[<span class="hljs-string">&quot;/api&quot;</span> {<span class="hljs-symbol">:interceptors</span> [<span class="hljs-symbol">::api</span> <span class="hljs-symbol">::db</span>]}
(<span class="hljs-name"><span class="hljs-builtin-name">for</span></span> [[type interceptor] actions
<span class="hljs-symbol">:let</span> [path (<span class="hljs-name"><span class="hljs-builtin-name">str</span></span> <span class="hljs-string">&quot;/&quot;</span> (<span class="hljs-name"><span class="hljs-builtin-name">name</span></span> interceptor))
method (<span class="hljs-name"><span class="hljs-builtin-name">condp</span></span> = type
<span class="hljs-symbol">:query</span> <span class="hljs-symbol">:get</span>
<span class="hljs-symbol">:command</span> <span class="hljs-symbol">:post</span>)]]
[path {method {<span class="hljs-symbol">:interceptors</span> [interceptor]}}])
(<span class="hljs-name"><span class="hljs-builtin-name">if</span></span> dev-mode? [<span class="hljs-string">&quot;/dev-tools&quot;</span> <span class="hljs-symbol">::dev-tools</span>])])
</code></pre>
<pre><code class="lang-clj">(<span class="hljs-name">cqrs-routes</span>
[[<span class="hljs-symbol">:query</span> &apos;get-user]
[<span class="hljs-symbol">:command</span> &apos;add-user]
[<span class="hljs-symbol">:command</span> &apos;add-order]]
<span class="hljs-literal">false</span>)
<span class="hljs-comment">; [&quot;/api&quot; {:interceptors [::api ::db]}</span>
<span class="hljs-comment">; ([&quot;/get-user&quot; {:get {:interceptors [get-user]}}]</span>
<span class="hljs-comment">; [&quot;/add-user&quot; {:post {:interceptors [add-user]}}]</span>
<span class="hljs-comment">; [&quot;/add-order&quot; {:post {:interceptors [add-order]}}])</span>
<span class="hljs-comment">; nil]</span>
</code></pre>
<h1 id="router">Router</h1>
<p>Routes are just data and to do actual routing, we need a Router satisfying the <code>reitit.core/Router</code> protocol. Routers are created with <code>reitit.core/router</code> function, taking the raw routes and optionally an options map. Raw routes gets expanded and optionally coerced and compiled.</p>
<p><code>Router</code> protocol:</p>
<pre><code class="lang-clj">(<span class="hljs-name"><span class="hljs-builtin-name">defprotocol</span></span> Router
(<span class="hljs-name">router-name</span> [this])
(<span class="hljs-name">routes</span> [this])
(<span class="hljs-name">options</span> [this])
(<span class="hljs-name">route-names</span> [this])
(<span class="hljs-name">match-by-path</span> [this path])
(<span class="hljs-name">match-by-name</span> [this name] [this name params]))
</code></pre>
<p>Creating a router:</p>
<pre><code class="lang-clj">(<span class="hljs-name">require</span> &apos;[reitit.core <span class="hljs-symbol">:as</span> r])
(<span class="hljs-name"><span class="hljs-builtin-name">def</span></span> router
(<span class="hljs-name">r/router</span>
[[<span class="hljs-string">&quot;/api&quot;</span>
[<span class="hljs-string">&quot;/ping&quot;</span> <span class="hljs-symbol">::ping</span>]
[<span class="hljs-string">&quot;/user/:id&quot;</span> <span class="hljs-symbol">::user</span>]]]))
</code></pre>
<p>Router flattens the raw routes and expands the route arguments using <code>reitit.core/Expand</code> protocol. By default, <code>Keyword</code>s are expanded to <code>:name</code> and functions are expaned to <code>:handler</code>. <code>nil</code> routes are removed. The expanded routes can be retrieved with router:</p>
<pre><code class="lang-clj">(<span class="hljs-name">r/routes</span> router)
<span class="hljs-comment">; [[&quot;/api/ping&quot; {:name :user/ping}]</span>
<span class="hljs-comment">; [&quot;/api/user/:id&quot; {:name :user/user}]]</span>
</code></pre>
<h2 id="path-based-routing">Path-based routing</h2>
<p>Path-based routing is done using the <code>reitit.core/match-by-path</code> function. It takes the router and path as arguments and returns one of the following:</p>
<ul>
<li><code>nil</code>, no match</li>
<li><code>PartialMatch</code>, path matched, missing path-parameters (only in reverse-routing)</li>
<li><code>Match</code>, exact match</li>
</ul>
<pre><code class="lang-clj">(<span class="hljs-name">r/match-by-path</span> router <span class="hljs-string">&quot;/hello&quot;</span>)
<span class="hljs-comment">; nil</span>
</code></pre>
<pre><code class="lang-clj">(<span class="hljs-name">r/match-by-path</span> router <span class="hljs-string">&quot;/api/user/1&quot;</span>)
<span class="hljs-comment">; #Match{:template &quot;/api/user/:id&quot;</span>
<span class="hljs-comment">; :meta {:name :user/user}</span>
<span class="hljs-comment">; :path &quot;/api/user/1&quot;</span>
<span class="hljs-comment">; :result nil</span>
<span class="hljs-comment">; :params {:id &quot;1&quot;}}</span>
</code></pre>
<h2 id="name-based-routing">Name-based routing</h2>
<p>All routes which <code>:name</code> route data defined, can be matched by name.</p>
<p>Listing all route names:</p>
<pre><code class="lang-clj">(<span class="hljs-name">r/route-names</span> router)
<span class="hljs-comment">; [:user/ping :user/user]</span>
</code></pre>
<p>Matching by name:</p>
<pre><code class="lang-clj">(<span class="hljs-name">r/match-by-name</span> router <span class="hljs-symbol">::user</span>)
<span class="hljs-comment">; #PartialMatch{:template &quot;/api/user/:id&quot;,</span>
<span class="hljs-comment">; :meta {:name :user/user},</span>
<span class="hljs-comment">; :result nil,</span>
<span class="hljs-comment">; :params nil,</span>
<span class="hljs-comment">; :required #{:id}}</span>
(<span class="hljs-name">r/partial-match?</span> (<span class="hljs-name">r/match-by-name</span> router <span class="hljs-symbol">::user</span>))
<span class="hljs-comment">; true</span>
</code></pre>
<p>We only got a partial match as we didn&apos;t provide the needed path-parameters. Let&apos;s provide the them too:</p>
<pre><code class="lang-clj">(<span class="hljs-name">r/match-by-name</span> router <span class="hljs-symbol">::user</span> {<span class="hljs-symbol">:id</span> <span class="hljs-string">&quot;1&quot;</span>})
<span class="hljs-comment">; #Match{:template &quot;/api/user/:id&quot;</span>
<span class="hljs-comment">; :meta {:name :user/user}</span>
<span class="hljs-comment">; :path &quot;/api/user/1&quot;</span>
<span class="hljs-comment">; :result nil</span>
<span class="hljs-comment">; :params {:id &quot;1&quot;}}</span>
</code></pre>
<p>There is also a exception throwing version:</p>
<pre><code class="lang-clj">(<span class="hljs-name">r/match-by-name!</span> router <span class="hljs-symbol">::user</span>)
<span class="hljs-comment">; ExceptionInfo missing path-params for route /api/user/:id: #{:id}</span>
</code></pre>
<h1 id="route-data">Route data</h1>
<p>Routes can have arbitrary meta-data, interpreted by the router (via it&apos;s <code>:compile</code> hook) or the application itself. For nested routes, route data is accumulated recursively using <a href="https://github.com/weavejester/meta-merge" target="_blank">meta-merge</a>. By default, it appends collections, but it can be overridden to do <code>:prepend</code>, <code>:replace</code> or <code>:displace</code>.</p>
<p>An example router with nested data:</p>
<pre><code class="lang-clj">(<span class="hljs-name"><span class="hljs-builtin-name">def</span></span> router
(<span class="hljs-name">r/router</span>
[<span class="hljs-string">&quot;/api&quot;</span> {<span class="hljs-symbol">:interceptors</span> [<span class="hljs-symbol">::api</span>]}
[<span class="hljs-string">&quot;/ping&quot;</span> <span class="hljs-symbol">::ping</span>]
[<span class="hljs-string">&quot;/admin&quot;</span> {<span class="hljs-symbol">:roles</span> #{<span class="hljs-symbol">:admin</span>}}
[<span class="hljs-string">&quot;/users&quot;</span> <span class="hljs-symbol">::users</span>]
[<span class="hljs-string">&quot;/db&quot;</span> {<span class="hljs-symbol">:interceptors</span> [<span class="hljs-symbol">::db</span>]
<span class="hljs-symbol">:roles</span> ^<span class="hljs-symbol">:replace</span> #{<span class="hljs-symbol">:db-admin</span>}}
[<span class="hljs-string">&quot;/:db&quot;</span> {<span class="hljs-symbol">:parameters</span> {<span class="hljs-symbol">:db</span> String}}
[<span class="hljs-string">&quot;/drop&quot;</span> <span class="hljs-symbol">::drop-db</span>]
[<span class="hljs-string">&quot;/stats&quot;</span> <span class="hljs-symbol">::db-stats</span>]]]]]))
</code></pre>
<p>Resolved route tree:</p>
<pre><code class="lang-clj">(<span class="hljs-name">reitit/routes</span> router)
<span class="hljs-comment">; [[&quot;/api/ping&quot; {:interceptors [::api]</span>
<span class="hljs-comment">; :name ::ping}]</span>
<span class="hljs-comment">; [&quot;/api/admin/users&quot; {:interceptors [::api]</span>
<span class="hljs-comment">; :roles #{:admin}</span>
<span class="hljs-comment">; :name ::users}]</span>
<span class="hljs-comment">; [&quot;/api/admin/db/:db/drop&quot; {:interceptors [::api ::db]</span>
<span class="hljs-comment">; :roles #{:db-admin}</span>
<span class="hljs-comment">; :parameters {:db String}</span>
<span class="hljs-comment">; :name ::drop-db}]</span>
<span class="hljs-comment">; [&quot;/api/admin/db/:db/stats&quot; {:interceptors [::api ::db]</span>
<span class="hljs-comment">; :roles #{:db-admin}</span>
<span class="hljs-comment">; :parameters {:db String}</span>
<span class="hljs-comment">; :name ::db-stats}]]</span>
</code></pre>
<p>Route data is returned with <code>Match</code> and the application can act based on it.</p>
<pre><code class="lang-clj">(<span class="hljs-name">r/match-by-path</span> router <span class="hljs-string">&quot;/api/admin/db/users/drop&quot;</span>)
<span class="hljs-comment">; #Match{:template &quot;/api/admin/db/:db/drop&quot;</span>
<span class="hljs-comment">; :meta {:interceptors [::api ::db]</span>
<span class="hljs-comment">; :roles #{:db-admin}</span>
<span class="hljs-comment">; :parameters {:db String}</span>
<span class="hljs-comment">; :name ::drop-db}</span>
<span class="hljs-comment">; :result nil</span>
<span class="hljs-comment">; :params {:db &quot;users&quot;}</span>
<span class="hljs-comment">; :path &quot;/api/admin/db/users/drop&quot;}</span>
</code></pre>
<h1 id="different-routers">Different Routers</h1>
<p>Reitit ships with several different implementations for the <code>Router</code> protocol, originally based on the awesome <a href="https://github.com/pedestal/pedestal/tree/master/route" target="_blank">Pedestal</a> implementation. <code>router</code> selects the most suitable implementation by inspecting the expanded routes. The implementation can be set manually using <code>:router</code> ROUTER OPTION.</p>
<table>
<thead>
<tr>
<th>router</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>:linear-router</code></td>
<td>Matches the routes one-by-one starting from the top until a match is found. Works with any kind of routes.</td>
</tr>
<tr>
<td><code>:lookup-router</code></td>
<td>Fastest router, uses hash-lookup to resolve the route. Valid if no paths have path or catch-all parameters.</td>
</tr>
<tr>
<td><code>:mixed-router</code></td>
<td>Creates internally a <code>:linear-router</code> and a <code>:lookup-router</code> and used them to effectively get best-of-both-worlds. Valid if there are no CONFLICTING ROUTES.</td>
</tr>
<tr>
<td><code>:prefix-tree-router</code></td>
<td><a href="https://github.com/julienschmidt/httprouter#how-does-it-work" target="_blank">TODO</a></td>
</tr>
</tbody>
</table>
<p>The router name can be asked from the router</p>
<pre><code class="lang-clj">(<span class="hljs-name">r/router-name</span> router)
<span class="hljs-comment">; :mixed-router</span>
</code></pre>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="basics.html#router" class="navigation navigation-next navigation-unique" aria-label="Next page: Router">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Route syntax","level":"1.2.1","depth":2,"next":{"title":"Router","level":"1.2.2","depth":2,"anchor":"#router","path":"basics.md","ref":"basics.md#router","articles":[]},"previous":{"title":"Basics","level":"1.2","depth":1,"ref":"","articles":[{"title":"Route syntax","level":"1.2.1","depth":2,"anchor":"#route-syntax","path":"basics.md","ref":"basics.md#route-syntax","articles":[]},{"title":"Router","level":"1.2.2","depth":2,"anchor":"#router","path":"basics.md","ref":"basics.md#router","articles":[]},{"title":"Path-based Routing","level":"1.2.3","depth":2,"anchor":"#path-based-routing","path":"basics.md","ref":"basics.md#path-based-routing","articles":[]},{"title":"Name-based Routing","level":"1.2.4","depth":2,"anchor":"#name-based-routing","path":"basics.md","ref":"basics.md#name-based-routing","articles":[]},{"title":"Route data","level":"1.2.5","depth":2,"anchor":"#route-data","path":"basics.md","ref":"basics.md#route-data","articles":[]},{"title":"Different Routers","level":"1.2.6","depth":2,"anchor":"#different-routers","path":"basics.md","ref":"basics.md#different-routers","articles":[]}]},"dir":"ltr"},"config":{"plugins":["editlink","github","highlight"],"root":"doc","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"editlink":{"label":"Edit This Page","multilingual":false,"base":"https://github.com/metosin/reitit/tree/master/doc"},"github":{"url":"https://github.com/metosin/reitit"},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"gitbook":"*"},"file":{"path":"basics.md","mtime":"2017-10-22T14:50:39.064Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-10-22T14:51:15.589Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-editlink/plugin.js"></script>
<script src="gitbook/gitbook-plugin-github/plugin.js"></script>
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>