/
in_memory_http_server.clj
35 lines (30 loc) · 1.09 KB
/
in_memory_http_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
(ns common-clj.components.http-server.in-memory-http-server
(:require [com.stuartsierra.component :as component]
[common-clj.components.http-server.protocol :refer [HttpServer]
:as http-server.protocol]
[common-clj.schemata.http :as schemata.http]
[io.pedestal.http :as http]
[schema.core :as s]))
(s/defn routes->pedestal [routes :- schemata.http/Routes]
(into
#{}
(map
(fn [[route-name {:keys [path method handler]}]]
[path method handler :route-name route-name]))
routes))
(s/defrecord InMemoryHttpServer [routes]
component/Lifecycle
(start [component]
(let [server (atom (http-server.protocol/create-server component))]
(assoc component :server server)))
(stop [component]
(assoc component :server nil))
HttpServer
(create-server [component]
(http/create-server
{::http/routes (routes->pedestal routes)
::http/type :jetty
::http/port 8080
::http/join? false})))
(s/defn new-http-server [routes :- schemata.http/Routes]
(map->InMemoryHttpServer {:routes routes}))