mirror of
https://github.com/metosin/reitit.git
synced 2025-12-16 16:01:11 +00:00
1.2 KiB
1.2 KiB
Router
Routes are just data and to do routing, we need a router instance satisfying the reitit.core/Router protocol. Routers are created with reitit.core/router function, taking the raw routes and optionally an options map.
The Router protocol:
(defprotocol Router
(router-name [this])
(routes [this])
(options [this])
(route-names [this])
(match-by-path [this path])
(match-by-name [this name] [this name params]))
Creating a router:
(require '[reitit.core :as r])
(def router
(r/router
["/api"
["/ping" ::ping]
["/user/:id" ::user]]))
Name of the created router:
(r/router-name router)
; :mixed-router
The flattened route tree:
(r/routes router)
; [["/api/ping" {:name :user/ping}]
; ["/api/user/:id" {:name :user/user}]]
Behind the scenes
When router is created, the following steps are done:
- route tree is flattened
- route arguments are expanded (via
reitit.core/Expandprotocol) and optionally coerced - route conflicts are resolved
- route tree is compiled
- actual router implementation is selected and created