Skip to content

Instantly share code, notes, and snippets.

@dazld
Created July 23, 2020 16:08
Show Gist options
  • Save dazld/65b0d8d0ef5cc8e631dcc364713e0e96 to your computer and use it in GitHub Desktop.
Save dazld/65b0d8d0ef5cc8e631dcc364713e0e96 to your computer and use it in GitHub Desktop.
(ns backend.dummy
(:require [io.pedestal.interceptor.chain :as chain]
[io.pedestal.interceptor :as i]
[io.pedestal.interceptor.error :as err]
[ring.util.response :as rr]))
(def notify-enabled? (constantly true))
(def websocket-notifier (i/interceptor {:name :pusher
:leave (fn [ctx]
(prn ::notified)
ctx)}))
(def notify (i/interceptor {:name :notify
:enter (fn [{:keys [action-taken] :as ctx}]
(prn "---------")
(if (notify-enabled?)
(chain/enqueue ctx [websocket-notifier])
ctx))}))
(defn execute-db [query]
(i/interceptor {:name (keyword "db" query)
:error (fn [ctx exception]
(prn ::error (:interceptor (ex-data exception)))
ctx)
:enter (fn [{:keys [name dbargs] :as ctx}]
(prn ::execute-db query name)
(when (= name "throw")
(throw (ex-info "synthetic" {:ctx ctx
:type :synthetic})))
(merge ctx
{:consequences [notify]
:message query
:status "ok"}))}))
(defn validator [schema]
(i/interceptor {:name (keyword "validator" schema)
:enter (fn [ctx]
(prn ::validate schema)
ctx)}))
(def consequences (i/interceptor {:name :consequences
:leave (fn [{:keys [consequences]
:as ctx}]
(prn ::consequence consequences)
(chain/enqueue ctx consequences))}))
(defn invoke-say-hello [ctx]
(chain/execute ctx [consequences
(validator "hello")
(execute-db "say-hello")]))
(defn say-hello [{{:keys [name location likes]} :body}]
(let [{:keys [message status]} (invoke-say-hello {:name name
:location location
:likes likes})]
(-> (rr/response message)
(rr/status status))))
(comment
(say-hello {:body {:name "hi"}}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment