reitit-http example

This commit is contained in:
Tommi Reiman 2018-08-25 16:28:57 +03:00
parent 9cb2e01715
commit 5210b2c5c4
3 changed files with 41 additions and 6 deletions

View file

@ -7,7 +7,12 @@
(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

View file

@ -1,6 +1,7 @@
(defproject ring-example "0.1.0-SNAPSHOT"
:description "Reitit Ring App with Swagger"
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/core.async "0.4.474"]
[ring "1.6.3"]
[metosin/reitit "0.2.0-SNAPSHOT"]]
:repl-options {:init-ns example.server})

View file

@ -1,17 +1,46 @@
(ns example.server
(:require [reitit.http :as http]
[reitit.ring :as ring]
[clojure.core.async :as a]
[reitit.interceptor.sieppari]
[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
(http/ring-handler
(http/router
["/" {:get (fn [request]
{:status 200
:body "hello!"})}])
(ring/routes
(ring/create-default-handler))
["/api"
{:interceptors [(interceptor :api)]}
["/sync"
{: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}))
(defn start []