-
Notifications
You must be signed in to change notification settings - Fork 11
/
core.clj
62 lines (57 loc) · 2.11 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(ns framework.websockets.core
(:require
[clojure.data.json :refer [read-str]]
[clojure.edn :as edn]
[clojure.string :as str]
[clojure.tools.logging :as log]
[framework.interceptor.queue :as queue]
[reitit.core :as r]
[xiana.core :as xiana]))
(defn string->
"String to 'uri', uses the first word as action key"
[s]
(first (str/split s #"\s")))
(defn edn->
"EDN to 'uri', converts edn string to map, extract :action key"
[e]
(:action (edn/read-string e)))
(defn json->
"JSON to 'uri', converts json string to map, extract :action key"
[j]
(:action (read-str j :key-fn keyword)))
(defn probe->
[e]
(name
(or (try (json-> e)
(catch Exception _ nil))
(try (edn-> e)
(catch Exception _ nil))
(try (string-> e)
(catch Exception _ nil)))))
(defn router
"Router for webSockets.
Parameters:
routes: reitit routes
msg->uri: function makes routing base from message. If missing tries to solve message as json, edn and string
state: xiana state record"
([routes state]
(router routes probe-> state))
([routes msg->uri {{income-msg :income-msg
fallback :fallback} :request-data
:as state}]
(when-not (str/blank? income-msg)
(let [match (r/match-by-path routes (msg->uri income-msg))
action (get-in match [:data :action] fallback)
interceptors (get-in match [:data :interceptors])
default-interceptors (get-in match [:data :default-interceptors])
_ (or (get-in match [:data :hide])
(log/info "Processing: " (str/trim income-msg)))
update-state (-> state
(update :request-data assoc
:action action
:interceptors interceptors)
(xiana/flow->
(queue/execute default-interceptors))
(xiana/extract))]
(when-let [reply-fn (get-in update-state [:response-data :reply-fn])]
(reply-fn update-state))))))