reitit/doc
Hannu Hartikainen 9e05d58880
Fix reader conditional example
The previous example only worked by accident (because there was an even number of map keys with conditional values).

{:get #?(:clj 1), :post #?(:clj 2)} gets read as {:get :post} in a non-clj context. The splicing reader conditional or a :cljs (or :default) value is needed to have the code work as intended. Here we change the docs to use the splicing version, as that is (imho) more beautiful than adding another branch.
2018-10-05 13:27:56 +03:00
..
advanced Fix reader conditional example 2018-10-05 13:27:56 +03:00
basics Merge pull request #121 from KingMob/feature/encode-routes 2018-09-06 08:35:13 +03:00
coercion spec warning 2018-09-05 22:18:32 +03:00
frontend no wip 2018-09-08 00:05:59 +03:00
http Release 0.2.3 2018-09-24 20:51:43 +03:00
images better docs 2018-08-22 21:51:02 +03:00
ring Release 0.2.3 2018-09-24 20:51:43 +03:00
cljdoc.edn 0.2.2 2018-09-09 22:29:00 +03:00
development.md Move development guides into docs 2018-05-20 19:49:41 +03:00
faq.md much 2018-02-19 22:41:52 +02:00
performance.md docs 2018-08-22 21:55:03 +03:00
README.md Release 0.2.3 2018-09-24 20:51:43 +03:00
SUMMARY.md Interceptor docs 2018-09-08 10:42:01 +03:00

Introduction

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

Modules:

Latest version

All bundled:

[metosin/reitit "0.2.3"]

Optionally, the parts can be required separately:

[metosin/reitit-core "0.2.3"]

;; coercion
[metosin/reitit-spec "0.2.3"]
[metosin/reitit-schema "0.2.3"]

;; ring helpers
[metosin/reitit-ring "0.2.3"]
[metosin/reitit-middleware "0.2.3"]

;; swagger-support for ring & http
[metosin/reitit-swagger "0.2.3"]
[metosin/reitit-swagger-ui "0.2.3"]

;; frontend helpers
[metosin/reitit-frontend "0.2.3"]

;; http with interceptors
[metosin/reitit-http "0.2.3"]
[metosin/reitit-interceptors "0.2.3"]
[metosin/reitit-sieppari "0.2.3"]

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

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