A fast data-driven routing library for Clojure/Script
Find a file
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
.circleci Enable cljs aot-cache 2018-07-12 14:02:48 +03:00
dev-resources test perf with different json 2018-08-25 13:28:44 +03:00
doc Fix reader conditional example 2018-10-05 13:27:56 +03:00
examples Release 0.2.3 2018-09-24 20:51:43 +03:00
modules Allow any keys in paramters, related to #145 2018-09-26 16:28:26 +03:00
perf-test/clj/reitit perf tests 2018-08-25 13:30:54 +03:00
scripts muuntaja-interceptors 2018-09-07 19:50:10 +03:00
test test parameters-interceptor 2018-09-24 20:27:42 +03:00
.gitignore Create example 2018-07-12 12:46:41 +03:00
book.json initial rework 2018-09-02 19:23:18 +03:00
CHANGELOG.md Release 0.2.3 2018-09-24 20:51:43 +03:00
CONTRIBUTING.md Initial commit 2017-08-07 14:15:45 +03:00
LICENSE Initial commit 2017-08-07 14:15:45 +03:00
package-lock.json Run CLJS tests in Circle 2018-03-13 14:47:04 +02:00
package.json Run CLJS tests in Circle 2018-03-13 14:47:04 +02:00
project.clj Release 0.2.3 2018-09-24 20:51:43 +03:00
README.md Release 0.2.3 2018-09-24 20:51:43 +03:00

reitit Build Status

A fast data-driven router for Clojure(Script).

Posts:

See the full documentation for details.

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

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

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

Quick start

(require '[reitit.core :as r])

(def router
  (r/router
    [["/api/ping" ::ping]
     ["/api/orders/:id" ::order]]))

(r/match-by-path router "/api/ping")
; #Match{:template "/api/ping"
;        :data {:name ::ping}
;        :result nil
;        :path-params {}
;        :path "/api/ping"}

(r/match-by-name router ::order {:id 2})
; #Match{:template "/api/orders/:id",
;        :data {:name ::order},
;        :result nil,
;        :path-params {:id 2},
;        :path "/api/orders/2"}

Ring example

A Ring routing app with input & output coercion using data-specs.

(require '[reitit.ring :as ring])
(require '[reitit.coercion.spec])
(require '[reitit.ring.coercion :as rrc])

(def app
  (ring/ring-handler
    (ring/router
      ["/api"
       ["/math" {:get {:parameters {:query {:x int?, :y int?}}
                       :responses {200 {:body {:total pos-int?}}}
                       :handler (fn [{{{:keys [x y]} :query} :parameters}]
                                  {:status 200
                                   :body {:total (+ x y)}})}}]]
      ;; router data effecting all routes
      {:data {:coercion reitit.coercion.spec/coercion
              :middleware [rrc/coerce-exceptions-middleware
                           rrc/coerce-request-middleware
                           rrc/coerce-response-middleware]}})))

Valid request:

(app {:request-method :get
      :uri "/api/math"
      :query-params {:x "1", :y "2"}})
; {:status 200
;  :body {:total 3}}

Invalid request:

(app {:request-method :get
      :uri "/api/math"
      :query-params {:x "1", :y "a"}})
;{:status 400,
; :body {:type :reitit.coercion/request-coercion,
;        :coercion :spec,
;        :spec "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:$spec20745/x :$spec20745/y]), :type :map, :keys #{:y :x}, :keys/req #{:y :x}})",
;        :problems [{:path [:y],
;                    :pred "clojure.core/int?",
;                    :val "a",
;                    :via [:$spec20745/y],
;                    :in [:y]}],
;        :value {:x "1", :y "a"},
;        :in [:request :query-params]}}

More examples

All examples are in https://github.com/metosin/reitit/tree/master/examples

More info

Check out the full documentation!

Join #reitit channel in Clojurians slack.

Roadmap is mostly written in issues.

Special thanks

License

Copyright © 2017-2018 Metosin Oy

Distributed under the Eclipse Public License, the same as Clojure.