-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializer.clj
74 lines (63 loc) · 2.28 KB
/
initializer.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(ns system-utils.initializer
(:gen-class)
(:require [ambiente.core :as ambiente]
[clojure.java.io :as io]
[clojure.walk :refer [postwalk prewalk]]
[edamame.core :as edn]
[integrant.core :as ig]))
(def ^:private default-env "local")
(def ^:private readers {'ig/ref ig/ref
'ig/refset ig/refset})
(defn ^:private config-resource [f-name]
(->> f-name
(io/file "config")
.getPath
io/resource))
(defn ^:private read-file [f]
(some-> f
slurp
(edn/parse-string {:readers readers})))
(defn ^:private cleanup-specialized [x]
(prewalk (fn [i]
(if (map? i)
(reduce-kv (fn [a k v]
(cond-> a
(not (symbol? k))
(assoc k v)))
{} i)
i)) x))
(defn ^:private placeholder-replace [m x]
(postwalk (fn [i] (if (symbol? i)
(get x i)
i))
m))
(defn read-config []
(let [base (-> "base.edn" config-resource read-file)
env (or (ambiente/env :env) default-env)
specialized-file (str env ".edn")
specialized (-> specialized-file config-resource read-file)
merged-config (merge base (cleanup-specialized specialized))
replaced-config (placeholder-replace merged-config specialized)]
(when (nil? base)
(throw (ex-info (str "Unable to load base config: base.edn")
{:anomaly/category ::invalid-system-config})))
(when (nil? specialized)
(throw (ex-info (str "Unable to load specialized config: " specialized-file)
{:anomaly/category ::invalid-system-config})))
(when (nil? replaced-config)
(throw (ex-info (str "Unable to merge config")
{:anomaly/category ::invalid-system-config})))
replaced-config))
(def system nil)
(defn start-system []
(let [config (read-config)]
(ig/load-namespaces config)
(alter-var-root #'system (fn [_] (ig/init config)))
:started))
(defn stop-system []
(when (and (bound? #'system)
(not (nil? system)))
(alter-var-root #'system (fn [system] (ig/halt! system)))
:stopped))
(defn -main [& args]
(start-system))