-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.clj
More file actions
52 lines (47 loc) · 1.85 KB
/
Copy pathserver.clj
File metadata and controls
52 lines (47 loc) · 1.85 KB
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 rocks.mygiftlist.server
(:require [integrant.core :as ig]
[org.httpkit.server :as http-kit]
[rocks.mygiftlist.authentication :as auth]
[rocks.mygiftlist.config :as config]
[rocks.mygiftlist.parser :as parser]
[rocks.mygiftlist.transit :as transit]
[com.fulcrologic.fulcro.server.api-middleware
:refer [handle-api-request
wrap-transit-params
wrap-transit-response]]
[ring.util.response :as resp]
[ring.middleware.defaults :refer [wrap-defaults
site-defaults]]
[ring.middleware.gzip :as gzip]
[taoensso.timbre :as log]))
(defn- not-found-handler [_]
(assoc-in (resp/resource-response "public/index.html")
[:headers "Content-Type"] "text/html"))
(defn- wrap-api [handler parser uri]
(fn [request]
(if (= uri (:uri request))
{:status 200
:body (parser {:ring/request request}
(:transit-params request))
:headers {"Content-Type" "application/transit+json"}}
(handler request))))
(defn handler [{:keys [parser config wrap-jwt]}]
(-> not-found-handler
(wrap-api parser "/api")
wrap-jwt
(wrap-transit-params {:opts {:handlers transit/read-handlers}})
(wrap-transit-response {:opts {:handlers transit/write-handlers}})
(wrap-defaults (assoc-in site-defaults
[:security :anti-forgery] false))
gzip/wrap-gzip))
(defmethod ig/init-key ::server
[_ {::parser/keys [parser]
::config/keys [config]
::auth/keys [wrap-jwt]}]
(http-kit/run-server (handler {:parser parser
:config config
:wrap-jwt wrap-jwt})
{:port (:port config)}))
(defmethod ig/halt-key! ::server
[_ server]
(server))