Liking cljdoc? Tell your friends :D

Pedestal

Pedestal is a backend web framework for Clojure. reitit-pedestal provides an alternative routing engine for Pedestal.

[metosin/reitit-pedestal "0.7.0-alpha4"]

Why should one use reitit instead of the Pedestal default routing?

To use Pedestal with reitit, you should first read both the Pedestal docs and the reitit interceptor guide.

Example

A minimalistic example on how to to swap the default-router with a reitit router.

; [io.pedestal/pedestal.service "0.5.5"]
; [io.pedestal/pedestal.jetty "0.5.5"]
; [metosin/reitit-pedestal "0.7.0-alpha4"]
; [metosin/reitit "0.7.0-alpha4"]

(require '[io.pedestal.http :as server])
(require '[reitit.pedestal :as pedestal])
(require '[reitit.http :as http])
(require '[reitit.ring :as ring])

(defn interceptor [number]
  {:enter (fn [ctx] (update-in ctx [:request :number] (fnil + 0) number))})

(def routes
  ["/api"
   {:interceptors [(interceptor 1)]}

   ["/number"
    {:interceptors [(interceptor 10)]
     :get {:interceptors [(interceptor 100)]
           :handler (fn [req]
                      {:status 200
                       :body (select-keys req [:number])})}}]])

(-> {::server/type :jetty
     ::server/port 3000
     ::server/join? false
     ;; no pedestal routes
     ::server/routes []}
    (server/default-interceptors)
    ;; swap the reitit router
    (pedestal/replace-last-interceptor
      (pedestal/routing-interceptor
        (http/router routes)))
    (server/dev-interceptors)
    (server/create-server)
    (server/start))

Compatibility

There is no common interceptor spec for Clojure and all default reitit interceptors (coercion, exceptions etc.) use the Sieppari interceptor model. It is mostly compatible with the Pedestal Interceptor model, only exception being that the :error handlers take just 1 arity (context) compared to Pedestal's 2-arity (context and exception).

Currently, out of the reitit default interceptors, there is only the reitit.http.interceptors.exception/exception-interceptor which has the :error defined.

You are most welcome to discuss about a common interceptor spec in #interceptors on Clojurians Slack.

More examples

Simple

Simple example with sync & async interceptors: https://github.com/metosin/reitit/tree/master/examples/pedestal

Swagger

More complete example with custom interceptors, default interceptors, coercion and swagger-support enabled: https://github.com/metosin/reitit/tree/master/examples/pedestal-swagger

Can you improve this documentation? These fine people already did:
Tommi Reiman, Juho Teperi & Miikka Koskinen
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close