reitit/examples/http/src/example/server.clj

53 lines
1.6 KiB
Clojure
Raw Normal View History

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}}]
2018-08-25 13:30:16 +00:00
["/future"
{:interceptors [(future-interceptor :future)]
:get {:interceptors [(future-interceptor :hello)]
:handler future-handler}}]
2018-08-25 13:28:57 +00:00
["/async"
{:interceptors [(async-interceptor :async)]
:get {:interceptors [(async-interceptor :async-hello)]
2018-08-25 13:30:16 +00:00
:handler async-handler}}]])
2018-08-25 13:28:57 +00:00
(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))