-
Notifications
You must be signed in to change notification settings - Fork 11
/
core.clj
41 lines (39 loc) · 1.62 KB
/
core.clj
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
(ns framework.handler.core
"Provides the default handler function"
(:require
[framework.interceptor.queue :as interceptor.queue]
[framework.route.core :as route]
[framework.state.core :as state]
[org.httpkit.server :refer [as-channel]]
[xiana.core :as xiana]))
(defn handler-fn
"Returns handler function for server, which do the routing, and executes interceptors and given action.
Execution order:
router interceptors: enters in order
router interceptors leaves in reversed order
routing
around interceptors enters in order
controller interceptors enters in order
inside interceptors enters in order
action
inside interceptors leaves in reversed order
controller interceptors leaves in reversed order
around interceptors leaves in reversed order"
[deps]
(fn handle*
([http-request]
(let [websocket? (:websocket? http-request)
state (state/make deps http-request)
queue (list #(interceptor.queue/execute % (:router-interceptors deps))
#(route/match %)
#(interceptor.queue/execute % (if websocket?
(:web-socket-interceptors deps)
(:controller-interceptors deps))))
result (-> (xiana/apply-flow-> state queue)
(xiana/extract))
channel (get-in result [:response-data :channel])]
(if (and websocket? channel)
(as-channel http-request channel)
(:response result))))
([request respond _]
(respond (handle* request)))))