mirror of
https://github.com/metosin/reitit.git
synced 2025-12-18 00:41:12 +00:00
It's possible to put the :keys keyword in the namespace of the keys one likes to destructure. With that one can use symbols in the vector again. One advantage of having symbols is, that Cursive grays them out if not used. I found two occurrences of unused destructured keys. |
||
|---|---|---|
| .. | ||
| advanced | ||
| basics | ||
| coercion | ||
| frontend | ||
| http | ||
| images | ||
| ring | ||
| cljdoc.edn | ||
| development.md | ||
| faq.md | ||
| performance.md | ||
| README.md | ||
| SUMMARY.md | ||
Introduction
Reitit is a fast data-driven router for Clojure(Script).
- Simple data-driven route syntax
- Route conflict resolution
- First-class route data
- Bi-directional routing
- Pluggable coercion (schema & clojure.spec)
- Helpers for ring, http, pedestal & frontend
- Friendly Error Messages
- Extendable
- Modular
- Fast
There is #reitit in Clojurians Slack for discussion & help.
Main Modules
reitit- all bundledreitit-core- the routing corereitit-ring- a ring routerreitit-middleware- common middleware forreitit-ringreitit-specclojure.spec coercionreitit-schemaSchema coercionreitit-swaggerSwagger2 apidocsreitit-swagger-uiIntegrated Swagger UI.reitit-frontendTools for frontend routingreitit-httphttp-routing with Pedestal-style Interceptorsreitit-interceptors- common interceptors forreitit-httpreitit-siepparisupport for Sieppari Interceptorsreitit-dev- development utilities
Extra modules
reitit-pedestalsupport for Pedestal
Latest version
All bundled:
[metosin/reitit "0.3.9"]
Optionally, the parts can be required separately.
Examples
Simple router
(require '[reitit.core :as r])
(def router
(r/router
[["/api/ping" ::ping]
["/api/orders/:id" ::order-by-id]]))
Routing:
(r/match-by-path router "/api/ipa")
; nil
(r/match-by-path router "/api/ping")
; #Match{:template "/api/ping"
; :data {:name ::ping}
; :result nil
; :path-params {}
; :path "/api/ping"}
(r/match-by-path router "/api/orders/1")
; #Match{:template "/api/orders/:id"
; :data {:name ::order-by-id}
; :result nil
; :path-params {:id "1"}
; :path "/api/orders/1"}
Reverse-routing:
(r/match-by-name router ::ipa)
; nil
(r/match-by-name router ::ping)
; #Match{:template "/api/ping"
; :data {:name ::ping}
; :result nil
; :path-params {}
; :path "/api/ping"}
(r/match-by-name router ::order-by-id)
; #PartialMatch{:template "/api/orders/:id"
; :data {:name :user/order-by-id}
; :result nil
; :path-params nil
; :required #{:id}}
(r/partial-match? (r/match-by-name router ::order-by-id))
; true
(r/match-by-name router ::order-by-id {:id 2})
; #Match{:template "/api/orders/:id",
; :data {:name ::order-by-id},
; :result nil,
; :path-params {:id 2},
; :path "/api/orders/2"}
Ring-router
Ring-router adds support for :handler functions, :middleware and routing based on :request-method. It also supports pluggable parameter coercion (clojure.spec), data-driven middleware, route and middleware compilation, dynamic extensions and more.
(require '[reitit.ring :as ring])
(defn handler [_]
{:status 200, :body "ok"})
(defn wrap [handler id]
(fn [request]
(update (handler request) :wrap (fnil conj '()) id)))
(def app
(ring/ring-handler
(ring/router
["/api" {:middleware [[wrap :api]]}
["/ping" {:get handler
:name ::ping}]
["/admin" {:middleware [[wrap :admin]]}
["/users" {:get handler
:post handler}]]])))
Routing:
(app {:request-method :get, :uri "/api/admin/users"})
; {:status 200, :body "ok", :wrap (:api :admin}
(app {:request-method :put, :uri "/api/admin/users"})
; nil
Reverse-routing:
(require '[reitit.core :as r])
(-> app (ring/get-router) (r/match-by-name ::ping))
; #Match{:template "/api/ping"
; :data {:middleware [[#object[user$wrap] :api]]
; :get {:handler #object[user$handler]}
; :name ::ping}
; :result #Methods{...}
; :path-params nil
; :path "/api/ping"}