From 416b6fae94a471f64f42a5990c2e70624e7e69c7 Mon Sep 17 00:00:00 2001
From: Automatic build /foo bar should be /foo%20bar.
Pedestal is a well known interceptor-based web framework for Clojure. To use reitit-http with Pedestal, we need to change the default routing interceptor. The needed helpers for this are found in a separate package:
[metosin/reitit-ring "0.2.9"]
+Pedestal is a backend web framework for Clojure. reitit-pedestal provides an alternative routing engine for Pedestal.
+[metosin/reitit-pedestal "0.2.9"]
-You should read the interceptor guide to understand the basics on Interceptor-based dispatch.
+Why should one use reitit instead of the Pedestal default routing?
+
+- One simple route syntax, with full route conflict resolution.
+- Supports first class route data with spec validation.
+- Fixes some known problems in routing.
+- Can handle trailing backslashes.
+- One router for both backend and frontend.
+- Supports parameter coercion & Swagger.
+- Is even faster.
+
+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"]
@@ -753,39 +763,32 @@
[reitit.http :as http]
[reitit.ring :as ring]))
-(def router
- (pedestal/routing-interceptor
- (http/router
- ["/ping" (fn [_] {:status 200, :body "pong"})])
- (ring/create-default-handler)))
+(def routes
+ ["/ping" {:get (fn [_] {:status 200, :body "pong"})}])
-(defn start []
- (-> {::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 router)
- (server/dev-interceptors)
- (server/create-server)
- (server/start))
- (println "server running in port 3000"))
-
-(start)
+(-> {::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))
-Caveat
-There is no common interceptor spec for Clojure and All default reitit interceptors (coercion, exceptions etc.) use the Sieppari interceptor model. For most parts, they are fully 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, there is only the reitit.http.interceptors.exception/exception-interceptor which has :error defined - just don't use it and everything should just work.
-You are most welcome to discuss about a common interceptor spec in #interceptors in Clojurians Slack.
-See the error handling guide on how to handle errors with Pedestal.
+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 both sync & async interceptors: https://github.com/metosin/reitit/tree/master/examples/pedestal
+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: https://github.com/metosin/reitit/tree/master/examples/pedestal-swagger
-note: exception handling is disabled in this example
+More complete example with custom interceptors, default interceptors, coercion and swagger-support enabled: https://github.com/metosin/reitit/tree/master/examples/pedestal-swagger