Fork me on GitHub
#reitit
<
2018-08-25
>
ikitommi13:08:31

merged current status of reitit-http and reitit-sieppari into master, for the adventurous: https://github.com/metosin/reitit/tree/master/examples/http

👍 4
ikitommi13:08:47

(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
      ["/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 []
  (jetty/run-jetty #'app {:port 3000, :join? false, :async? true})
  (println "server running in port 3000"))

(comment
  (start))

ikitommi13:08:14

example is kinda crap, but should demonstrate how one can mix sync & async steps in both :enter and :leave stages, and in handlers.

ikitommi13:08:39

doesn’t yet support Manifold or Promesa, but both on the roadmap (for sieppari). Pedestal-sample next on the backlog.