reitit/doc
2017-12-06 22:03:20 +02:00
..
advanced polish docs 2017-12-06 22:03:20 +02:00
basics BREAKING: route :meta => :data 2017-11-18 12:47:16 +02:00
images Rewrite performance.md 2017-11-13 07:25:22 +02:00
ring reitit.ring.middleware => reitit.middleware 2017-12-04 23:58:05 +02:00
faq.md Tune docs 2017-11-27 08:02:35 +02:00
performance.md polish docs 2017-12-06 22:03:20 +02:00
README.md Tune docs 2017-11-27 08:02:35 +02:00
SUMMARY.md Tune docs 2017-11-27 08:02:35 +02:00

Introduction

Reitit is a small Clojure(Script) library for data-driven routing.

To use Reitit, add the following dependecy to your project:

[metosin/reitit "0.1.0-SNAPSHOT"]

Optionally, the parts can be required separately:

[metosin/reitit-core "0.1.0-SNAPSHOT"] ; just the router
[metosin/reitit-ring "0.1.0-SNAPSHOT"] ; ring-router
[metosin/reitit-spec "0.1.0-SNAPSHOT"] ; spec-coercion
[metosin/reitit-schema "0.1.0-SNAPSHOT"] ; schema coercion

For discussions, there is a #reitit channel in Clojurians slack.

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
;        :params {}
;        :path "/api/ping"}

(r/match-by-path router "/api/orders/1")
; #Match{:template "/api/orders/:id"
;        :data {:name ::order-by-id}
;        :result nil
;        :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
;        :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
;               :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,
;        :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])

(def 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{...}
;        :params nil
;        :path "/api/ping"}