mirror of
https://github.com/metosin/reitit.git
synced 2026-02-07 20:33:12 +00:00
reitit-http example
This commit is contained in:
parent
9cb2e01715
commit
5210b2c5c4
3 changed files with 41 additions and 6 deletions
|
|
@ -7,7 +7,12 @@
|
||||||
(start)
|
(start)
|
||||||
```
|
```
|
||||||
|
|
||||||
Go with browser to http://localhost:3000
|
Go with browser to:
|
||||||
|
|
||||||
|
* http://localhost:3000/api/sync - synchronous
|
||||||
|
* http://localhost:3000/api/async - with core.async
|
||||||
|
* http://localhost:3000/api/future - with futures
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
(defproject ring-example "0.1.0-SNAPSHOT"
|
(defproject ring-example "0.1.0-SNAPSHOT"
|
||||||
:description "Reitit Ring App with Swagger"
|
:description "Reitit Ring App with Swagger"
|
||||||
:dependencies [[org.clojure/clojure "1.9.0"]
|
:dependencies [[org.clojure/clojure "1.9.0"]
|
||||||
|
[org.clojure/core.async "0.4.474"]
|
||||||
[ring "1.6.3"]
|
[ring "1.6.3"]
|
||||||
[metosin/reitit "0.2.0-SNAPSHOT"]]
|
[metosin/reitit "0.2.0-SNAPSHOT"]]
|
||||||
:repl-options {:init-ns example.server})
|
:repl-options {:init-ns example.server})
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,46 @@
|
||||||
(ns example.server
|
(ns example.server
|
||||||
(:require [reitit.http :as http]
|
(:require [reitit.http :as http]
|
||||||
[reitit.ring :as ring]
|
[reitit.ring :as ring]
|
||||||
|
[clojure.core.async :as a]
|
||||||
[reitit.interceptor.sieppari]
|
[reitit.interceptor.sieppari]
|
||||||
[ring.adapter.jetty :as jetty]))
|
[ring.adapter.jetty :as jetty]))
|
||||||
|
|
||||||
|
(defn -interceptor [f x]
|
||||||
|
{:enter (fn [ctx] (f (update-in ctx [:request :via] (fnil conj []) x)))
|
||||||
|
:leave (fn [ctx] (f (update-in ctx [:response :body] str "\n<- " x)))})
|
||||||
|
|
||||||
|
(def interceptor (partial -interceptor identity))
|
||||||
|
(def future-interceptor (partial -interceptor #(future %)))
|
||||||
|
(def async-interceptor (partial -interceptor #(a/go %)))
|
||||||
|
|
||||||
|
(defn -handler [f {:keys [via]}]
|
||||||
|
(f {:status 200,
|
||||||
|
:body (str (apply str (map #(str "-> " % "\n") via)) " hello!")}))
|
||||||
|
|
||||||
|
(def handler (partial -handler identity))
|
||||||
|
(def future-handler (partial -handler #(future %)))
|
||||||
|
(def async-handler (partial -handler #(a/go %)))
|
||||||
|
|
||||||
(def app
|
(def app
|
||||||
(http/ring-handler
|
(http/ring-handler
|
||||||
(http/router
|
(http/router
|
||||||
["/" {:get (fn [request]
|
["/api"
|
||||||
{:status 200
|
{:interceptors [(interceptor :api)]}
|
||||||
:body "hello!"})}])
|
|
||||||
(ring/routes
|
["/sync"
|
||||||
(ring/create-default-handler))
|
{:interceptors [(interceptor :sync)]
|
||||||
|
:get {:interceptors [(interceptor :hello)]
|
||||||
|
:handler handler}}]
|
||||||
|
|
||||||
|
["/async"
|
||||||
|
{:interceptors [(async-interceptor :async)]
|
||||||
|
:get {:interceptors [(async-interceptor :async-hello)]
|
||||||
|
:handler async-handler}}]
|
||||||
|
["/future"
|
||||||
|
{:interceptors [(future-interceptor :sync)]
|
||||||
|
:get {:interceptors [(future-interceptor :hello)]
|
||||||
|
:handler future-handler}}]])
|
||||||
|
(ring/create-default-handler)
|
||||||
{:executor reitit.interceptor.sieppari/executor}))
|
{:executor reitit.interceptor.sieppari/executor}))
|
||||||
|
|
||||||
(defn start []
|
(defn start []
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue