-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.clj
60 lines (54 loc) · 2.02 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
53
54
55
56
57
58
59
60
(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.jetty9 :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 ::h2? boolean?)
(s/def ::h2c? boolean?)
(s/def ::http (s/keys :req-un [::host ::port]
:opt-un [::cacert ::cert ::key ::basic-auth ::h2? ::h2c?]))
(defn handle!
[chain]
(fn [request]
(interceptor/execute {:request request} chain)))
(def default-config
{:h2? true
:join? false
:h2c? true})
(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)))
base-config (merge default-config
(select-keys config [:host :h2? :h2c?]))
config (cond-> base-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)))