-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.clj
65 lines (56 loc) · 2.16 KB
/
server.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
63
64
65
(ns deercreeklabs.capsule.server
(:require
[bidi.bidi :as bidi]
[deercreeklabs.capsule.endpoint :as endpoint]
[deercreeklabs.capsule.utils :as u]
[deercreeklabs.log-utils :as lu :refer [debugs]]
[deercreeklabs.tube.connection :as tc]
[deercreeklabs.tube.server :as ts]
[schema.core :as s]
[taoensso.timbre :as timbre :refer [debugf errorf infof]]))
(def default-port 8080)
(defprotocol ICapsuleServer
(start [this])
(stop [this])
(get-conn-counts [this]))
(defrecord CapsuleServer [tube-server endpoints]
ICapsuleServer
(start [this]
(ts/start tube-server))
(stop [this]
(ts/stop tube-server))
(get-conn-counts [this]
(reduce (fn [acc endpoint]
(let [path (endpoint/get-path endpoint)
conn-count (endpoint/get-conn-count endpoint)]
(assoc acc path conn-count)))
{} endpoints)))
(defn make-route [endpoint]
(let [path (endpoint/get-path endpoint)
connection-handler (fn [tube-conn]
(endpoint/on-connect endpoint tube-conn))]
[path connection-handler]))
(defn make-routes [endpoints]
["/" (apply hash-map (mapcat make-route endpoints))])
(defn make-on-server-connect [routes]
(fn on-server-connect [conn]
(let [uri (tc/get-uri conn)
{:keys [handler]} (bidi/match-route routes uri)]
(if handler
(handler conn)
(errorf "No handler matches for path %s" uri)))))
(s/defn make-server :- (s/protocol ICapsuleServer)
([endpoints :- [(s/protocol endpoint/IEndpoint)]]
(make-server endpoints default-port {}))
([endpoints :- [(s/protocol endpoint/IEndpoint)]
port :- s/Int
options :- {s/Keyword s/Any}]
(let [routes (make-routes endpoints)
on-connect (make-on-server-connect routes)
on-disconnect (fn [conn code reason])
compression-type :smart
tube-server-options (select-keys options
[:handle-http :http-timeout-ms])
tube-server (ts/make-tube-server port on-connect on-disconnect
compression-type tube-server-options)]
(->CapsuleServer tube-server endpoints))))