воскресенье, 18 ноября 2018 г.

clojure async interceptors demo

1. deps.edn file
{:paths       ["src/clj" "src/cljs" "test" "resources" "target"]
 :extra-paths ["resources" "resources/public"]              ;; fix cambada packaging
 :deps        {org.clojure/clojure          {:mvn/version "1.10.0-beta6"}
               org.clojure/core.async       {:mvn/version "0.4.474"}
               org.clojure/spec.alpha       {:mvn/version "0.2.176"}
               org.clojure/core.specs.alpha {:mvn/version "0.2.44"}
               http-kit/http-kit            {:mvn/version "2.3.0"}
               metosin/sieppari             {:mvn/version "0.0.0-alpha6"}
               metosin/reitit               {:mvn/version "0.2.7"}
               fipp                         {:mvn/version "0.6.14"}}
 :aliases     {:repl {:extra-deps  {nrepl/nrepl                {:mvn/version "0.4.5"}
                                    com.bhauman/rebel-readline {:mvn/version "0.1.4"}
                                    cider/piggieback           {:mvn/version "0.3.8"}
                                    com.bhauman/figwheel-main  {:mvn/version "0.1.9"}}
                      :extra-paths ["src" "env/development/clj"]
                      :main-opts   ["--main" "nrepl.cmdline" "--port" "7888" "--middleware" "[cider.piggieback/wrap-cljs-repl]"]}}}


2. Source code

(ns test01.core  (:gen-class)
  (:require [org.httpkit.server :as kit]
            [org.httpkit.client :as kit-client]
            [sieppari.core :as sieppari]
            [sieppari.context]
            [muuntaja.interceptor]
            [clojure.core.async :as a :refer [go chan <! <!! >! >!! timeout close! put! take! alts! alts!!]]
            [reitit.ring :as ring]
            [reitit.http :as http]
            [reitit.core :as r]
            [fipp.edn :refer (pprint)]
            [reitit.interceptor.sieppari]))


(def  #(a/go %))
(def  identity)
(def  #(a/thread %))


(defn prn-tap [v]
  (println v))

(add-tap prn-tap)

(defn interceptor [f x]
  {:enter (fn [ctx] (f                      (update-in ctx [:request :via] (fnil conj []) {:enter  x                                                                     :thread (.getName (Thread/currentThread))})))
   :leave (fn [ctx] (f                      (update-in ctx [:response :body] conj {:leave  x                                                             :thread (.getName (Thread/currentThread))})))})

(defn handler [f data]
  (fn [{:keys [via] :as request}]
    (f      (do        (pprint (dissoc request :reitit.core/match :reitit.core/router))
        {:status 200,
         :body   (conj via (str {:handler data                                 :thread  (.getName (Thread/currentThread))}))}))))



(def routes (http/router [["/" {:get {:handler (handler  "root")}}]
                          ["/test1"                           ["" {:name         ::test1                                :interceptors [(interceptor  :test1)]
                                :get          {:handler (handler  "test1")}}]
                           ["/test2" {:interceptors [(interceptor  :test2-level)]
                                      :get          {:interceptors [(interceptor  :test2-get)]
                                                     :handler      (handler  "test2")}}]]]))

;;(r/routes routes)
(def app (http/ring-handler routes                            (ring/create-default-handler)
                            {:executor     reitit.interceptor.sieppari/executor                             :interceptors []}))

(app {:uri            "/test1/test2"      :request-method :get})


(def params {:host "0.0.0.0" :port 8080})

(defn start-server  [handler params]
  (kit/run-server handler params))


(defn -main "entry point"  [& args]
  (start-server app params)
  (println "server is started: " params))



;; ----------- client and repl zone---------------------------------------
(defn handle-response  [resp]
  (println (:body resp)))

(comment  (def stop-fn (start-server app params))
  (time (dotimes [_ 10]
          (kit-client/get "http://localhost:8080" {:timeout 20000} handle-response)))
  (time (slurp "http://localhost:8080/test1"))
  (stop-fn))

Комментариев нет:

Отправить комментарий