A fast data-driven routing library for Clojure/Script
Find a file
2018-06-06 10:34:23 +03:00
.circleci Run CLJS tests in Circle 2018-03-13 14:47:04 +02:00
dev-resources Support :index-files in create-resource-handler 2018-04-29 16:51:53 +03:00
doc Update static.md 2018-06-05 22:21:19 +10:00
examples update swagger-ui path, fixes #93 2018-05-31 16:46:56 +03:00
modules Strip nil routes from all positions 2018-06-06 10:34:23 +03:00
perf-test/clj/reitit use ring for serving resources. 2018-04-25 08:32:01 +03:00
scripts welcome swagger-ui! 2018-05-14 08:21:47 +03:00
test Strip nil routes from all positions 2018-06-06 10:34:23 +03:00
.gitignore Add karma packages 2017-10-25 17:32:12 +03:00
book.json Explicitly use the highlight plugin for gitbook 2017-09-14 17:39:26 +03:00
CHANGELOG.md Strip nil routes from all positions 2018-06-06 10:34:23 +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 0.1.2-SNAPSHOT, update deps 2018-05-29 08:03:15 +03:00
README.md Update README.md 2018-05-20 22:46:39 +03:00

reitit Build Status

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

Posts:

See the full documentation for details.

Modules

Bubblin' under:

  • reitit-http with enchanced Pedestal-style Interceptors (WIP)
  • reitit-frontend with Keechma-style Controllers (WIP)

Latest version

All bundled:

[metosin/reitit "0.1.1"]

Optionally, the parts can be required separately:

[metosin/reitit-core "0.1.1"]
[metosin/reitit-ring "0.1.1"]
[metosin/reitit-spec "0.1.1"]
[metosin/reitit-schema "0.1.1"]
[metosin/reitit-swagger "0.1.1"]
[metosin/reitit-swagger-ui "0.1.1"]

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

NOTE: Reitit is not a batteries included web-stack. You should also include at least:

  • content negotiation library like Muuntaja
  • some default Ring-middleware like ring.middleware.params/wrap-params

See Ring with Swagger Example for a runnable example. Plan is to have more batteries as separate modules, templates and example projects. Stay tuned.

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.