2018-03-15 06:41:25 +00:00
# reitit [](https://circleci.com/gh/metosin/reitit)
2017-08-07 11:08:39 +00:00
2018-02-11 17:15:25 +00:00
A fast data-driven router for Clojure(Script).
2017-08-07 11:08:39 +00:00
2017-10-30 06:46:20 +00:00
* Simple data-driven [route syntax ](https://metosin.github.io/reitit/basics/route_syntax.html )
* Route [conflict resolution ](https://metosin.github.io/reitit/basics/route_conflicts.html )
2017-11-18 10:47:16 +00:00
* First-class [route data ](https://metosin.github.io/reitit/basics/route_data.html )
2017-10-29 07:29:06 +00:00
* Bi-directional routing
2017-12-10 15:50:17 +00:00
* [Pluggable coercion ](https://metosin.github.io/reitit/coercion/coercion.html ) ([schema](https://github.com/plumatic/schema) & [clojure.spec ](https://clojure.org/about/spec ))
2018-08-25 12:29:45 +00:00
* Helpers for [ring ](https://metosin.github.io/reitit/ring/ring.html ) & [the browser ](https://metosin.github.io/reitit/frontend/basics.html )
2017-08-08 12:31:00 +00:00
* Extendable
2017-11-27 06:02:35 +00:00
* Modular
2017-11-01 08:17:57 +00:00
* [Fast ](https://metosin.github.io/reitit/performance.html )
2017-08-08 12:31:00 +00:00
2018-05-20 19:46:39 +00:00
Posts:
* [Reitit, Data-Driven Routing with Clojure(Script) ](https://www.metosin.fi/blog/reitit/ )
* [Data-Driven Ring with Reitit ](https://www.metosin.fi/blog/reitit-ring/ )
2018-08-25 12:29:45 +00:00
See the [full documentation ](https://metosin.github.io/reitit/ ) for details.
There is [#reitit ](https://clojurians.slack.com/messages/reitit/ ) in [Clojurians Slack ](http://clojurians.net/ ) for discussion & help.
2018-05-20 19:46:39 +00:00
## Modules
2018-05-20 16:51:33 +00:00
2018-08-19 19:56:54 +00:00
* `reitit` - all bundled
2018-05-20 16:51:33 +00:00
* `reitit-core` - the routing core
2018-08-03 10:10:16 +00:00
* `reitit-ring` - a [ring router ](https://metosin.github.io/reitit/ring/ring.html )
2018-08-25 12:29:45 +00:00
* `reitit-middleware` - [common middleware ](https://metosin.github.io/reitit/ring/default_middleware.html ) for `reitit-ring`
2018-05-20 16:51:33 +00:00
* `reitit-spec` [clojure.spec ](https://clojure.org/about/spec ) coercion
* `reitit-schema` [Schema ](https://github.com/plumatic/schema ) coercion
* `reitit-swagger` [Swagger2 ](https://swagger.io/ ) apidocs
2018-08-03 10:10:16 +00:00
* `reitit-swagger-ui` Integrated [Swagger UI ](https://github.com/swagger-api/swagger-ui )
2018-08-31 07:48:13 +00:00
* `reitit-frontend` Tools for [frontend routing ]((https://metosin.github.io/reitit/frontend/basics.html ))
2018-05-20 16:51:33 +00:00
Bubblin' under:
2018-08-19 19:56:54 +00:00
* `reitit-http` http-routing with Pedestal-style Interceptors (WIP)
* `reitit-sieppari` support for [Sieppari ](https://github.com/metosin/sieppari ) Interceptors (WIP)
2017-12-27 19:35:37 +00:00
2017-08-07 11:08:39 +00:00
## Latest version
2017-11-01 07:29:16 +00:00
All bundled:
2017-11-01 17:03:41 +00:00
```clj
2018-07-28 09:07:16 +00:00
[metosin/reitit "0.2.0-SNAPSHOT"]
2017-11-01 17:03:41 +00:00
```
2017-11-01 07:29:16 +00:00
2017-11-01 17:03:41 +00:00
Optionally, the parts can be required separately:
2017-11-01 07:29:16 +00:00
2017-11-01 17:03:41 +00:00
```clj
2018-07-28 09:07:16 +00:00
[metosin/reitit-core "0.2.0-SNAPSHOT"]
2018-08-05 14:32:57 +00:00
;; coercion
2018-07-28 09:07:16 +00:00
[metosin/reitit-spec "0.2.0-SNAPSHOT"]
[metosin/reitit-schema "0.2.0-SNAPSHOT"]
2018-08-05 14:32:57 +00:00
;; ring helpers
[metosin/reitit-ring "0.2.0-SNAPSHOT"]
[metosin/reitit-middleware "0.2.0-SNAPSHOT"]
;; swagger-support for ring & http
2018-07-28 09:07:16 +00:00
[metosin/reitit-swagger "0.2.0-SNAPSHOT"]
[metosin/reitit-swagger-ui "0.2.0-SNAPSHOT"]
2018-08-05 14:32:57 +00:00
;; frontend helpers (alpha)
[metosin/reitit-frontend "0.2.0-SNAPSHOT"]
2018-08-19 19:56:54 +00:00
;; http with interceptors (alpha)
2018-08-05 14:32:57 +00:00
[metosin/reitit-http "0.2.0-SNAPSHOT"]
2018-08-19 19:56:54 +00:00
[metosin/reitit-sieppari "0.2.0-SNAPSHOT"]
2017-11-01 17:03:41 +00:00
```
2017-11-01 07:29:16 +00:00
2017-10-30 06:46:20 +00:00
## Quick start
```clj
(require '[reitit.core :as r])
(def router
(r/router
[["/api/ping" ::ping]
2017-10-31 08:27:51 +00:00
["/api/orders/:id" ::order]]))
2017-10-30 06:46:20 +00:00
(r/match-by-path router "/api/ping")
; #Match {:template "/api/ping"
2017-11-18 10:47:16 +00:00
; :data {:name ::ping}
2017-10-30 06:46:20 +00:00
; :result nil
2018-02-01 14:23:44 +00:00
; :path-params {}
2017-10-30 06:46:20 +00:00
; :path "/api/ping"}
2017-10-31 08:27:51 +00:00
(r/match-by-name router ::order {:id 2})
2017-10-30 06:46:20 +00:00
; #Match {:template "/api/orders/:id",
2017-11-18 10:47:16 +00:00
; :data {:name ::order},
2017-10-30 06:46:20 +00:00
; :result nil,
2018-02-01 14:23:44 +00:00
; :path-params {:id 2},
2017-10-30 06:46:20 +00:00
; :path "/api/orders/2"}
```
2017-12-27 19:35:37 +00:00
## Ring example
A Ring routing app with input & output coercion using [data-specs ](https://github.com/metosin/spec-tools/blob/master/README.md#data-specs ).
```clj
(require '[reitit.ring :as ring])
(require '[reitit.coercion.spec])
2017-12-31 09:29:51 +00:00
(require '[reitit.ring.coercion :as rrc])
2017-12-27 19:35:37 +00:00
(def app
(ring/ring-handler
(ring/router
["/api"
2017-12-31 09:29:51 +00:00
["/math" {:get {:parameters {:query {:x int?, :y int?}}
2018-01-01 20:10:49 +00:00
:responses {200 {:body {:total pos-int?}}}
2017-12-27 19:35:37 +00:00
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200
:body {:total (+ x y)}})}}]]
2017-12-31 09:29:51 +00:00
;; router data effecting all routes
{:data {:coercion reitit.coercion.spec/coercion
:middleware [rrc/coerce-exceptions-middleware
rrc/coerce-request-middleware
rrc/coerce-response-middleware]}})))
2017-12-27 19:35:37 +00:00
```
Valid request:
```clj
(app {:request-method :get
:uri "/api/math"
:query-params {:x "1", :y "2"}})
; {:status 200
; :body {:total 3}}
```
Invalid request:
```clj
(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}})",
2017-12-29 10:17:53 +00:00
; :problems [{:path [:y],
; :pred "clojure.core/int?",
; :val "a",
; :via [:$spec20745/y],
2017-12-27 19:35:37 +00:00
; :in [:y]}],
; :value {:x "1", :y "a"},
; :in [:request :query-params]}}
2018-05-20 18:41:38 +00:00
```
2017-12-27 19:35:37 +00:00
2018-05-20 18:41:38 +00:00
**NOTE**: Reitit is not a batteries included web-stack. You should also include at least:
* content negotiation library like [Muuntaja ](https://github.com/metosin/muuntaja )
* some default Ring-middleware like `ring.middleware.params/wrap-params`
2017-12-27 19:35:37 +00:00
2018-05-20 18:58:10 +00:00
See [Ring with Swagger Example ](https://github.com/metosin/reitit/tree/master/examples/ring-swagger ) for a runnable example. Plan is to have more batteries as separate modules, templates and example projects. Stay tuned.
2017-12-27 19:35:37 +00:00
2017-11-12 19:44:40 +00:00
## More info
2017-08-07 11:08:39 +00:00
2017-09-14 13:33:36 +00:00
[Check out the full documentation! ](https://metosin.github.io/reitit/ )
2017-08-11 04:48:55 +00:00
2017-12-29 10:17:53 +00:00
Join [#reitit ](https://clojurians.slack.com/messages/reitit/ ) channel in [Clojurians slack ](http://clojurians.net/ ).
2017-11-12 19:44:40 +00:00
2017-12-29 10:17:53 +00:00
Roadmap is mostly written in [issues ](https://github.com/metosin/reitit/issues ).
2017-11-12 19:44:40 +00:00
2017-08-08 12:31:00 +00:00
## Special thanks
2017-12-29 10:17:53 +00:00
* Existing Clojure(Script) routing libs, expecially to
2017-08-08 12:31:00 +00:00
[Ataraxy ](https://github.com/weavejester/ataraxy ), [Bide ](https://github.com/funcool/bide ), [Bidi ](https://github.com/juxt/bidi ), [Compojure ](https://github.com/weavejester/compojure ) and
2017-08-09 07:36:57 +00:00
[Pedestal ](https://github.com/pedestal/pedestal/tree/master/route ).
2017-12-29 10:17:53 +00:00
* [Compojure-api ](https://github.com/metosin/compojure-api ), [Kekkonen ](https://github.com/metosin/kekkonen ), [Ring-swagger ](https://github.com/metosin/ring-swagger ) and [Yada ](https://github.com/juxt/yada ) and for ideas, coercion & stuff.
2018-02-11 17:15:25 +00:00
* [Schema ](https://github.com/plumatic/schema ) and [clojure.spec ](https://clojure.org/about/spec ) for the validation part.
2017-09-08 05:29:39 +00:00
2017-08-07 11:08:39 +00:00
## License
2018-02-19 19:58:27 +00:00
Copyright © 2017-2018 [Metosin Oy ](http://www.metosin.fi )
2017-08-07 11:08:39 +00:00
Distributed under the Eclipse Public License, the same as Clojure.