2018-08-20 15:48:22 +00:00
|
|
|
(ns example.server
|
|
|
|
|
(:require [reitit.http :as http]
|
|
|
|
|
[reitit.ring :as ring]
|
2018-08-25 13:28:57 +00:00
|
|
|
[clojure.core.async :as a]
|
2018-08-20 15:48:22 +00:00
|
|
|
[reitit.interceptor.sieppari]
|
|
|
|
|
[ring.adapter.jetty :as jetty]))
|
|
|
|
|
|
2018-08-25 13:28:57 +00:00
|
|
|
(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 %)))
|
|
|
|
|
|
2018-08-20 15:48:22 +00:00
|
|
|
(def app
|
|
|
|
|
(http/ring-handler
|
|
|
|
|
(http/router
|
2018-08-25 13:28:57 +00:00
|
|
|
["/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)
|
2018-08-20 15:48:22 +00:00
|
|
|
{:executor reitit.interceptor.sieppari/executor}))
|
|
|
|
|
|
|
|
|
|
(defn start []
|
|
|
|
|
(jetty/run-jetty #'app {:port 3000, :join? false, :async? true})
|
|
|
|
|
(println "server running in port 3000"))
|
|
|
|
|
|
|
|
|
|
(comment
|
|
|
|
|
(start))
|