-
Notifications
You must be signed in to change notification settings - Fork 4
/
webrouting_service_core.clj
164 lines (140 loc) · 6.69 KB
/
webrouting_service_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(ns puppetlabs.trapperkeeper.services.webrouting.webrouting-service-core
(:require [puppetlabs.i18n.core :as i18n]
[puppetlabs.trapperkeeper.services :as tk-services]
[puppetlabs.trapperkeeper.services.webserver.jetty10-core :as jetty10-core]
[schema.core :as schema]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Schemas
(def RouteWithServerConfig
{:route schema/Str
:server schema/Str})
(def WebroutingMultipleConfig
{schema/Keyword (schema/either schema/Str RouteWithServerConfig)})
(def WebroutingServiceConfig
{schema/Keyword (schema/either schema/Str RouteWithServerConfig WebroutingMultipleConfig)})
(def RouteOption
{(schema/optional-key :route-id) schema/Keyword})
(def CommonOptions
(dissoc (merge jetty10-core/CommonOptions RouteOption) :server-id))
(def ContextHandlerOptions
(dissoc (merge jetty10-core/ContextHandlerOptions RouteOption) :server-id))
(def ServletHandlerOptions
(dissoc (merge jetty10-core/ServletHandlerOptions RouteOption) :server-id))
(def ProxyRouteOptions
(dissoc (merge jetty10-core/ProxyOptions RouteOption) :server-id))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Private Utility Functions
(defn get-endpoint-and-server-from-config
[context svc route-id]
(let [config (:web-router-service context)
no-route-id? (nil? route-id)
multi-route? (> (count (keys (get-in config [svc]))) 1)
route-id (if no-route-id?
:default
route-id)
endpoint (get-in config [svc route-id])
no-service? (nil? (get config svc))
no-endpoint? (nil? endpoint)
no-server? (nil? (schema/check schema/Str endpoint))
server? (nil? (schema/check RouteWithServerConfig endpoint))]
(cond
no-service?
(throw
(IllegalArgumentException.
(i18n/trs "service {0} does not appear in configuration" svc)))
no-endpoint?
(throw
(IllegalArgumentException.
(i18n/trs "endpoint with id {0} does not appear in configuration for service {1}"
route-id
svc)))
(and no-route-id? multi-route?)
(throw
(IllegalArgumentException.
(i18n/trs "no route-id specified for a service with multiple routes")))
no-server? {:route endpoint :server nil}
server? endpoint)))
(defn compute-common-elements
[context svc options]
(let [svc-id (keyword (tk-services/service-symbol svc))
route-id (:route-id options)
route-and-server (get-endpoint-and-server-from-config context svc-id route-id)
path (:route route-and-server)
server (keyword (:server route-and-server))
options (dissoc options :route-id)
opts (if (nil? server)
options
(assoc options :server-id server))]
{:path path :opts opts}))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Lifecycle implementations
(schema/defn ^:always-validate init
[context config :- WebroutingServiceConfig]
(let [configuration (into {} (for [[svc svc-config] config]
(cond
(nil? (schema/check (schema/either schema/Str RouteWithServerConfig) svc-config))
[svc {:default svc-config}]
(nil? (schema/check WebroutingMultipleConfig svc-config))
[svc svc-config])))]
(assoc context :web-router-service configuration)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Service function implementations
(defn get-route
[context svc route-id]
(let [svc-id (keyword (tk-services/service-symbol svc))
endpoint-and-server (get-endpoint-and-server-from-config context
svc-id
route-id)]
(:route endpoint-and-server)))
(schema/defn ^:always-validate get-server :- (schema/maybe schema/Str)
[context
svc :- (schema/protocol tk-services/Service)
route-id]
(let [svc-id (keyword (tk-services/service-symbol svc))
endpoint-and-server (get-endpoint-and-server-from-config context
svc-id
route-id)]
(:server endpoint-and-server)))
(schema/defn ^:always-validate add-context-handler!
[context webserver-service
svc :- (schema/protocol tk-services/Service)
base-path
options :- ContextHandlerOptions]
(let [{:keys [path opts]} (compute-common-elements context svc options)
add-context-handler (:add-context-handler webserver-service)]
(add-context-handler base-path path opts)))
(schema/defn ^:always-validate add-ring-handler!
[context webserver-service
svc :- (schema/protocol tk-services/Service)
handler options :- CommonOptions]
(let [{:keys [path opts]} (compute-common-elements context svc options)
add-ring-handler (:add-ring-handler webserver-service)]
(add-ring-handler handler path opts)))
(schema/defn ^:always-validate add-servlet-handler!
[context webserver-service
svc :- (schema/protocol tk-services/Service)
servlet options :- ServletHandlerOptions]
(let [{:keys [path opts]} (compute-common-elements context svc options)
add-servlet-handler (:add-servlet-handler webserver-service)]
(add-servlet-handler servlet path opts)))
(schema/defn ^:always-validate add-websocket-handler!
[context webserver-service
svc :- (schema/protocol tk-services/Service)
handlers options :- CommonOptions]
(let [{:keys [path opts]} (compute-common-elements context svc options)
add-websocket-handler (:add-websocket-handler webserver-service)]
(add-websocket-handler handlers path opts)))
(schema/defn ^:always-validate add-war-handler!
[context webserver-service
svc :- (schema/protocol tk-services/Service)
war options :- RouteOption]
(let [{:keys [path opts]} (compute-common-elements context svc options)
add-war-handler (:add-war-handler webserver-service)]
(add-war-handler war path opts)))
(schema/defn ^:always-validate add-proxy-route!
[context webserver-service
svc :- (schema/protocol tk-services/Service)
target options :- ProxyRouteOptions]
(let [{:keys [path opts]} (compute-common-elements context svc options)
add-proxy-route (:add-proxy-route webserver-service)]
(add-proxy-route target path opts)))