reitit/doc
2024-02-09 11:49:44 +02:00
..
advanced welcome 2-phase schema compilation 2023-05-21 20:32:40 +03:00
basics 0.7.0-alpha7 2023-10-03 13:34:36 +03:00
coercion Merge pull request #655 from stig/fix-link-to-jira 2023-10-30 12:34:48 +02:00
frontend Add example and update docs 2023-03-24 11:32:22 +02:00
http 0.7.0-alpha7 2023-10-03 13:34:36 +03:00
images perf image 2019-10-08 10:20:03 +03:00
ring doc: Update docs for fetching content types from Muuntaja instance 2024-02-09 11:49:44 +02:00
cljdoc.edn Revert group id change 2023-10-03 13:06:23 +03:00
development.md updated release guide 2023-02-21 15:54:52 +02:00
faq.md Update doc/faq.md 2019-10-08 22:36:27 +03:00
performance.md Fix incorrect ring-router doc references 2023-01-31 15:51:31 -08:00
README.md 0.7.0-alpha7 2023-10-03 13:34:36 +03:00
SUMMARY.md doc: initial docs for openapi support & per-content-type coercion 2023-03-08 11:23:18 +02:00

Introduction

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

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

Main Modules

  • reitit - all bundled
  • reitit-core - the routing core
  • reitit-ring - a ring router
  • reitit-middleware - common middleware for reitit-ring
  • reitit-spec clojure.spec coercion
  • reitit-schema Schema coercion
  • reitit-swagger Swagger2 apidocs
  • reitit-openapi OpenAPI 3 apidocs
  • reitit-swagger-ui Integrated Swagger UI.
  • reitit-frontend Tools for frontend routing
  • reitit-http http-routing with Pedestal-style Interceptors
  • reitit-interceptors - common interceptors for reitit-http
  • reitit-sieppari support for Sieppari Interceptors
  • reitit-dev - development utilities

Extra modules

Latest version

All bundled:

[metosin/reitit "0.7.0-alpha7"]

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

A Ring router function 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"}