-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.clj
52 lines (47 loc) · 1.83 KB
/
http.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
(ns corbihttp.http
(:require [clojure.spec.alpha :as s]
[com.stuartsierra.component :as component]
[corbihttp.spec :as spec]
[exoscale.interceptor :as interceptor]
[less.awful.ssl :as less-ssl]
[ring.adapter.jetty :as jetty]))
(s/def ::host ::spec/ne-string)
(s/def ::port ::spec/port)
(s/def ::cacert ::spec/file-spec)
(s/def ::cert ::spec/file-spec)
(s/def ::key ::spec/file-spec)
(s/def ::username ::spec/ne-string)
(s/def ::password ::spec/secret)
(s/def ::basic-auth (s/keys :req-un [::username ::password]))
(s/def ::http (s/keys :req-un [::host ::port]
:opt-un [::cacert ::cert ::key ::basic-auth]))
(defn handle!
[chain]
(fn [request]
(interceptor/execute {:request request} chain)))
(defrecord Server [config chain-builder server chain]
component/Lifecycle
(start [this]
(let [ssl-context (when (:cacert config)
(less-ssl/ssl-context (:key config)
(:cert config)
(:cacert config)))
config (cond-> {:join? false
:host (:host config)}
(not ssl-context) (assoc :port (:port config))
ssl-context
(assoc :ssl? true
:http? false
:ssl-port (:port config)
:ssl-context ssl-context
:client-auth :need))
chain (->> (chain-builder this)
(remove nil?))]
(assoc this
:server (jetty/run-jetty (handle! chain)
config)
:chain chain)))
(stop [this]
(when server
(.stop ^org.eclipse.jetty.server.Server server))
(assoc this :server nil :chain nil)))