-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cljs
72 lines (58 loc) · 2.04 KB
/
server.cljs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(ns fifql.server
(:require
[fif.core :as fif]
[fifql.core :as fifql]))
(def body-parser (js/require "body-parser"))
(defn get-request-input-string
[request]
(let [url-query (aget request "query" "query")
body-query (aget request "body")
body-query (if (string? body-query) body-query nil)]
(or body-query url-query)))
(defn add-request-middleware!
[app]
(doto app
(.use (.text body-parser #js {:type "text/plain"}))
(.use (.text body-parser #js {:type "application/edn"}))
(.use (.text body-parser #js {:type "application/fif"}))))
(defn- get-content-type
[request]
(let [content-type (.get request "Content-Type")]
(if (or (= content-type "application/edn")
(= content-type "application/fif"))
"application/edn"
"text/plain")))
(defn- create-output-fn
""
[*stdout *stderr]
(fn [{:keys [tag value]}]
(case tag
:out (swap! *stdout conj value)
:error (swap! *stderr conj value))))
(defn- get-stack-machine
"If `stack-machine` is a function, passes in the `request` to the function to get the desired stack-machine.
Otherwise, returns the stack-machine."
[request stack-machine]
(if (fn? stack-machine)
(stack-machine request)
stack-machine))
(defn create-express-request-handler
""
[& {:keys [prepare-stack-machine
post-response]
:or {post-response (fn [sm request response])}}]
(fn [request response]
(let [*stdout (atom [])
*stderr (atom [])
output-fn (create-output-fn *stdout *stderr)
input-string (get-request-input-string request)
stack-machine (get-stack-machine request prepare-stack-machine)
evaled-sm (fif/prepl-eval stack-machine input-string output-fn)]
(post-response evaled-sm request response)
(doto response
(.append "Content-Type" (get-content-type request))
(.send
(pr-str {:input-string input-string
:stack (fif/get-stack evaled-sm)
:stdout @*stdout
:stderr @*stderr}))))))