-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_reitit.clj
More file actions
50 lines (46 loc) · 1.88 KB
/
example_reitit.clj
File metadata and controls
50 lines (46 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(ns example-reitit
(:require
[commando.core :as commando]
[commando.commands.builtin :as command-builtin]
[commando.commands.query-dsl :as command-query-dsl]
[reitit.http :as http]
[reitit.interceptor.sieppari :as reitit-sieppari]
[reitit.ring :as ring]
[ring.middleware.transit :as ring-transit]))
;; --------------------------
;; 1. Define Commando Handler
;; --------------------------
(defn ^:private commando-handler [request]
(if-let [instruction-to-run (get request [:body :instruction])]
(let [result
(commando/execute
[command-builtin/command-apply-spec
command-builtin/command-fn-spec
command-builtin/command-from-spec
command-builtin/command-mutation-spec
command-query-dsl/command-resolve-spec]
instruction-to-run)]
(if (= :ok (:status result))
{:status 200
:headers {"Content-Type" "application/transit+json; charset=UTF-8"}
:body (select-keys result [:status :instruction :successes])}
{:status 500
:headers {"Content-Type" "application/transit+json; charset=UTF-8"}
:body (select-keys result [:status :errors :warnings :successes])}))
{:status 500
:headers {"Content-Type" "application/transit+json; charset=UTF-8"}
:body {:status :failed
:errors [{:message "Instruction is empty"}]}}))
;; ------------------------------------
;; 2. Registering Endpoint With Transit
;; ------------------------------------
(def reitit-handler
(http/ring-handler
(http/router
[["/commando"
{:post {:handler (-> commando-handler
ring-transit/wrap-transit-body
ring-transit/wrap-transit-response)}}]])
(ring/create-default-handler)
{:executor reitit-sieppari/executor
:interceptors []}))