forked from metosin/reitit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request metosin#197 from metosin/reitit-pedestal
Reitit pedestal
- Loading branch information
Showing
19 changed files
with
164 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,70 @@ | ||
# Pedestal | ||
|
||
[Pedestal](http://pedestal.io/) is a well known interceptor-based web framework for Clojure. To use `reitit-http` with Pedestal, we need to change the default routing interceptor into a new one. Examples projects show how to do this. | ||
[Pedestal](http://pedestal.io/) 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: | ||
|
||
```clj | ||
[metosin/reitit-ring "0.2.9"] | ||
``` | ||
|
||
You should read the [interceptor guide](interceptors.md) to understand the basics on Interceptor-based dispatch. | ||
|
||
## Example | ||
|
||
A minimalistic example on how to to swap the default-router with a reitit router. | ||
|
||
```clj | ||
; [io.pedestal/pedestal.service "0.5.5"] | ||
; [io.pedestal/pedestal.jetty "0.5.5"] | ||
; [metosin/reitit-pedestal "0.2.9"] | ||
; [metosin/reitit "0.2.9"] | ||
|
||
(ns example.server | ||
(:require [io.pedestal.http :as server] | ||
[reitit.pedestal :as pedestal] | ||
[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))) | ||
|
||
(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) | ||
``` | ||
|
||
## Caveat | ||
|
||
`reitit-http` defines Interceptors as `reitit.interceptor/Interceptor`. Compared to Pedestal 2-arity error handlers, reitit uses a simplified (1-arity) handlers. Differences in error handling are described in the [Sieppari README](https://github.com/metosin/sieppari#differences-to-pedestal). | ||
There is no common interceptor spec for Clojure and All default reitit interceptors (coercion, exceptions etc.) use the [Sieppari](https://github.com/metosin/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](http://pedestal.io/reference/error-handling) (`context` and `exception`). | ||
|
||
* you can use any [pedestal-style interceptor](http://pedestal.io/reference/interceptors) within reitit router (as Pedestal is executing those anyway) | ||
* you can use any reitit-style interceptor that doesn't have `:error`-stage defined | ||
* using a reitit-style interceptor with `:error` defined will cause `ArityException` if invoked | ||
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](https://clojurians.slack.com/messages/interceptors/) in [Clojurians Slack](http://clojurians.net/). | ||
|
||
See the [error handling guide](http://pedestal.io/reference/error-handling) on how to handle errors with Pedestal. | ||
|
||
## Examples | ||
## More examples | ||
|
||
### Simple | ||
|
||
* simple example, with both sync & async code: | ||
* https://github.com/metosin/reitit/tree/master/examples/pedestal | ||
Simple example, with both sync & async interceptors: https://github.com/metosin/reitit/tree/master/examples/pedestal | ||
|
||
### Swagger | ||
|
||
### With batteries | ||
More complete example with custom interceptors, [default interceptors](default_interceptors.md), [coercion](../coercion/coercion.md) and [swagger](../ring/swagger.md)-support: https://github.com/metosin/reitit/tree/master/examples/pedestal-swagger | ||
|
||
* with [default interceptors](default_interceptors.md), [coercion](../coercion/coercion.md) and [swagger](../ring/swagger.md)-support (note: exception handling is disabled): | ||
* https://github.com/metosin/reitit/tree/master/examples/pedestal-swagger | ||
note: exception handling is disabled in this example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
(defproject just-coercion-with-ring "0.1.0-SNAPSHOT" | ||
:description "Reitit coercion with vanilla ring" | ||
:dependencies [[org.clojure/clojure "1.9.0"] | ||
:dependencies [[org.clojure/clojure "1.10.0"] | ||
[ring/ring-jetty-adapter "1.7.0"] | ||
[metosin/muuntaja "0.4.1"] | ||
[metosin/reitit "0.2.9"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
(defproject ring-example "0.1.0-SNAPSHOT" | ||
:description "Reitit-http with pedestal" | ||
:dependencies [[org.clojure/clojure "1.9.0"] | ||
[io.pedestal/pedestal.service "0.5.4"] | ||
[io.pedestal/pedestal.jetty "0.5.4"] | ||
:dependencies [[org.clojure/clojure "1.10.0"] | ||
[io.pedestal/pedestal.service "0.5.5"] | ||
[io.pedestal/pedestal.jetty "0.5.5"] | ||
[metosin/reitit-pedestal "0.2.9"] | ||
[metosin/reitit "0.2.9"]] | ||
:repl-options {:init-ns example.server}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
(defproject ring-example "0.1.0-SNAPSHOT" | ||
:description "Reitit-http with pedestal" | ||
:dependencies [[org.clojure/clojure "1.9.0"] | ||
[io.pedestal/pedestal.service "0.5.4"] | ||
[io.pedestal/pedestal.jetty "0.5.4"] | ||
:dependencies [[org.clojure/clojure "1.10.0"] | ||
[io.pedestal/pedestal.service "0.5.5"] | ||
[io.pedestal/pedestal.jetty "0.5.5"] | ||
[metosin/reitit-pedestal "0.2.9"] | ||
[metosin/reitit "0.2.9"]] | ||
:repl-options {:init-ns example.server}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.