-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
49 lines (43 loc) · 1.51 KB
/
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
(ns cawdy.core
(:require
[clojure.pprint :refer [pprint]]
[cheshire.core :as json]
[clj-http.client :as http]))
(defn config [address]
(-> (http/get (str address "/config")
{:content-type :json})
:body
(json/parse-string true)
(or {})))
(defn clean-config [address]
(-> (http/delete (str address "/config/")
{:content-type :json})
:body))
(defn static-handler [{:keys [listen body host]
:or {listen ":2015"
host "localhost"}}]
{:listen [listen]
:routes [{:handle [{:handler "static_response"
:body body}]}]})
(defn files-handler [{:keys [listen directory host]
:or {listen ":2015"
host "localhost"}}]
{:listen [listen]
:routes [{:handle [{:handler "file_server"
:host host
:root directory}]}]})
(def handler-types
{:static static-handler
:files files-handler})
(defn add-server [address id type args]
(when (nil? (get handler-types type))
(throw (Exception. (format "Couldn't find handler of type %s" type))))
(let [cfg (config address)
handler-fn (get handler-types type)
handler-res (handler-fn args)
new-cfg (assoc-in cfg
[:apps :http :servers id]
handler-res)]
(http/post (str address "/load")
{:content-type :json
:body (json/generate-string new-cfg)})))