reitit/doc
Alexander Kiel a19849fe58 Make Map Destructuring of Namespaced Keys more Beautiful
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.
2019-07-13 17:02:41 +03:00
..
advanced Update docs 2019-06-09 20:46:20 +03:00
basics 0.3.9 2019-06-16 20:20:49 +03:00
coercion Fix typos 2019-05-22 19:17:10 +02:00
frontend Mention browser history stack manipulation in docs 2019-03-22 08:42:28 +02:00
http 0.3.9 2019-06-16 20:20:49 +03:00
images Error docs 2019-05-12 21:16:01 +03:00
ring Make Map Destructuring of Namespaced Keys more Beautiful 2019-07-13 17:02:41 +03:00
cljdoc.edn Exceptions docs 2019-05-25 15:57:34 +03:00
development.md Announce that we use Break Versioning 2019-03-14 09:50:21 +02:00
faq.md much 2018-02-19 22:41:52 +02:00
performance.md More perf tests 2019-02-28 12:12:28 +02:00
README.md 0.3.9 2019-06-16 20:20:49 +03:00
SUMMARY.md Error docs 2019-05-12 21:16:01 +03:00

Introduction

Reitit is a fast data-driven router for Clojure(Script).

There is #reitit in Clojurians Slack for discussion & help.

Main Modules

Extra modules

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"}