reitit/doc
Martin Klepsch fc7ae2252f
fix various links in documentation
These probably work in Gitbook to some extent but they don't work on
GitHub. In most places of the documentation files are referenced
via their source files (`.md`) and I assume Gitbook deals fine with that

For cljdoc the correct links are required to properly fix links that are
broken when rendering the document on different URLs.
2018-05-20 12:04:49 +02:00
..
advanced Added "Dev Workflow" to "Advanced" docs 2018-02-20 11:53:54 +01:00
basics Polish code 2018-03-21 08:15:28 +02:00
coercion fix various links in documentation 2018-05-20 12:04:49 +02:00
images Update docs 2018-05-14 08:22:06 +03:00
ring fix various links in documentation 2018-05-20 12:04:49 +02:00
cljdoc.edn cljdoc 2018-05-18 18:42:10 +03:00
faq.md much 2018-02-19 22:41:52 +02:00
interceptors.md Tuning the docs 2018-02-11 19:25:59 +02:00
performance.md Tuning the docs 2018-02-11 19:25:59 +02:00
README.md Update docs 2018-05-14 08:22:06 +03:00
SUMMARY.md Update docs 2018-05-14 08:22:06 +03:00

Introduction

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

The following higher-level routers are also available as separate modules:

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

[metosin/reitit "0.1.1-SNAPSHOT"]

Optionally, the parts can be required separately:

[metosin/reitit-core "0.1.1-SNAPSHOT"] ; routing core
[metosin/reitit-ring "0.1.1-SNAPSHOT"] ; ring-router
[metosin/reitit-spec "0.1.1-SNAPSHOT"] ; spec coercion
[metosin/reitit-schema "0.1.1-SNAPSHOT"] ; schema coercion
[metosin/reitit-swagger "0.1.1-SNAPSHOT"] ; swagger
[metosin/reitit-swagger-ui "0.1.1-SNAPSHOT"] ; swagger-ui

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