/
core.clj
279 lines (237 loc) · 8.36 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
(ns stavka.core
(:require [stavka.protocols :as sp]
[stavka.resolvers.env]
[stavka.resolvers.options]
[stavka.resolvers.dict]
[stavka.resolvers.properties]
[stavka.sources.file]
[stavka.updaters.poller]
[stavka.formats.edn]
[stavka.formats.properties]
[stavka.utils :as utils]))
(defrecord ConfigHolder [state source updater format resolver parent])
(defrecord ConfigHolders [holders listeners])
(defn config-holders [holders-coll]
(let [holders (ConfigHolders. holders-coll (atom []))]
(doseq [h holders-coll]
(reset! (.-parent h) holders))
holders))
(defrecord UpdaterSourceHolder [source updater])
;; source loaders
(utils/import-var file stavka.sources.file/file)
(utils/if-provided
'clj-http.client
;; when clj-http
(do
(require 'stavka.sources.url)
(let [the-url-source (resolve 'stavka.sources.url/url)]
(def url the-url-source)))
;; otherwise throws exception
(defn url [_]
(throw (UnsupportedOperationException.
"clj-http not found on classpath. Url source is disbaled."))))
(utils/import-var classpath stavka.sources.file/classpath)
(declare $)
;; helper
(defn- load-from-source! [holder reload?]
(when-let [stream (sp/reload (.-source holder))]
(let [result (sp/parse (.-format holder) stream)]
(if reload?
(when-let [parent @(.parent holder)]
(let [listeners @(.-listeners parent)
interested (mapv #($ parent (:key %)) listeners)]
(reset! (.-state holder) result)
(let [updated-intereseted (mapv #($ parent (:key %)) listeners)]
;; check all monitored keys
(doseq [[{config-key :key callback :callback} p c] (map list listeners interested updated-intereseted)]
;; value changed, trigger callback
(when (not= p c)
(callback c p))))))
(do
(reset! (.-state holder) result))))))
(defn holder-from-source
"Create a holder from source readable source, useful when creating your own
configuration format"
[source format resolver]
(let [[source updater-factory] (if (vector? source)
source
[source nil])
holder-ref (promise)
updater (when updater-factory
(updater-factory #(when-let [inner @holder-ref]
(load-from-source! inner true))))
holder (ConfigHolder. (atom (sp/initial-state resolver))
source
updater
format
resolver
(atom nil))]
(deliver holder-ref holder)
(load-from-source! holder false)
(when updater
(sp/start! updater))
holder))
;; resolvers
(defn env
"Environment variables as configuration source. Environment variable key is
lowercased and had `_` replaced by `.`.
Options:
`:disable-underscore-to-dot?` disabling transforming underscore to dot"
[& {:as options}]
(ConfigHolder. nil nil nil nil (stavka.resolvers.env/resolver options) (atom nil)))
(defn options
"JVM option as configuration source."
[]
(ConfigHolder. nil nil nil nil (stavka.resolvers.options/resolver) (atom nil)))
(utils/if-provided
'[cheshire.core]
;; when cheshire is on classpath
(do
(require 'stavka.formats.json)
(let [fmt @(resolve 'stavka.formats.json/the-format)]
(defn json
"JSON configuration from some source"
[source]
(holder-from-source source (fmt)
(stavka.resolvers.dict/resolver)))))
;; else
(defn json [_]
(throw (UnsupportedOperationException.
"cheshire is not on classpath, json format disabled."))))
(defn edn
"EDN configuration from some source"
[source]
(holder-from-source source
(stavka.formats.edn/the-format)
(stavka.resolvers.dict/resolver)))
(defn properties
"java.util.Properties from some source"
[source]
(holder-from-source source
(stavka.formats.properties/the-format)
(stavka.resolvers.properties/resolver)))
(utils/if-provided
'[clj-yaml.core]
;; clj-yaml provided
(do
(require 'stavka.formats.yaml)
(let [fmt @(resolve 'stavka.formats.yaml/the-format)]
(defn yaml
"YAML configuration from source source"
[source]
(holder-from-source source (fmt)
(stavka.resolvers.dict/resolver)))))
;; else
(defn yaml [_]
(throw (UnsupportedOperationException.
"clj-yaml is not on classpath, yaml format disabled."))))
;; updaters
(utils/if-provided
'hawk.core
;; hawk is provided
(do
(require 'stavka.updaters.watcher)
(let [the-watcher (resolve 'stavka.updaters.watcher/watch)]
(def watch the-watcher)))
;; otherwise disable fs watcher
(defn watch [_]
(throw (UnsupportedOperationException.
"hawk is not found on classpath, file system watching is disabled."))))
(utils/import-var poll stavka.updaters.poller/poll)
(defmacro using
"Put your configuration sources inside to create a configuration store."
[& holders]
`(config-holders (reverse (vector ~@holders))))
(defonce global-config (atom nil))
(defmacro global!
"Create global stavka configuration."
[& holders]
`(let [config# (using ~@holders)]
(reset! global-config config#)))
(defn $
"Get configuration item from store."
([holders key] ($ holders key nil))
([holders key default-value]
(or (->> (.-holders holders)
(map #(sp/resolve (.-resolver %) (utils/deref-safe (.-state %)) (name key)))
(filter some?)
first)
default-value)))
(defn $$
"Get configuration item from the global store."
([key] ($$ key nil))
([key default-value]
(if-let [holders @global-config]
($ holders key default-value)
(throw (ex-info "No global config available. Use stavka.core/global! to setup" {})))))
(defn $l
"Get configuration as long"
([holders key] ($l holders key nil))
([holders key default-value]
(if-let [c ($ holders key)]
(Long/valueOf c)
default-value)))
(defn $$l
"Get configuration from the global store as long"
([key] ($$l key nil))
([key default-value]
(if-let [holders @global-config]
($l holders key default-value)
(throw (ex-info "No global config available. Use stavka.core/global! to setup")))))
(defn $f
"Get configuration as double"
([holders key] ($f holders key nil))
([holders key default-value]
(if-let [c ($ holders key)]
(Double/valueOf c)
default-value)))
(defn $$f
"Get configuration from the global store as double"
([key] ($$f key nil))
([key default-value]
(if-let [holders @global-config]
($f holders key default-value)
(throw (ex-info "No global config available. Use stavka.core/global! to setup" {})))))
(defn $s
"Get configuration as string"
([holders key] ($s holders key nil))
([holders key default-value]
(if-let [c ($ holders key)]
(str c)
default-value)))
(defn $$s
"Get configuration from the global store as string"
([key] ($$s key nil))
([key default-value]
(if-let [holders @global-config]
($s holders key default-value)
(throw (ex-info "No global config available. Use stavka.core/global! to setup" {})))))
(defn $b
"Get configuration as boolean"
([holders key] ($b holders key nil))
([holders key default-value]
(if-let [c ($ holders key)]
(Boolean/valueOf c)
default-value)))
(defn $$b
"Get configuration from the global store as boolean"
([key] ($$b key nil))
([key default-value]
(if-let [holders @global-config]
($b holders key default-value)
(throw (ex-info "No global config available. Use stavka.core/global! to setup" {})))))
(defn stop-updaters!
"Stop updater associated with holders."
([] (when-let [holders @global-config]
(stop-updaters! holders)))
([holders] (doseq [h (.-holders holders)]
(when-let [updater (.-updater h)]
(sp/stop! updater)))))
(defn on-change!
"Register a listener on configuration change"
([config-key callback]
(when-let [holders @global-config]
(on-change! holders config-key callback)))
([holders config-key callback]
(swap! (.-listeners holders) conj {:key config-key
:callback callback})))