Line data Source code
1 4 : (ns matchscore.config
2 : (:require
3 : [aero.core :as aero]
4 : [clojure.java.io :as io]
5 : [clojure.string :as str]
6 : [malli.core :as m]
7 : [malli.error :as me]))
8 :
9 :
10 4 : (def ConfigSchema
11 8 : [:map
12 28 : [:env [:enum "development" "production" "test"]]
13 8 : [:options
14 8 : [:map
15 36 : [:log-level [:enum :trace :debug :info :warn :error]]
16 12 : [:telegram-auth-enabled? :boolean]
17 12 : [:hot-reload-enabled? :boolean]
18 12 : [:malli-dev-enabled? :boolean]
19 12 : [:runtime-validation? :boolean]]]
20 12 : [:backend-url :string]
21 8 : [:telegram
22 8 : [:map
23 12 : [:bot-token :string]
24 20 : [:bot-username {:optional true}
25 12 : [:maybe :string]]
26 24 : [:client-id {:optional true}
27 : :string]
28 12 : [:webhook-url-prefix :string]
29 12 : [:bot-api-secret-token :string]
30 12 : [:webhook-url-secret-token :string]]]
31 8 : [:auth
32 8 : [:map
33 8 : [:session
34 8 : [:and
35 8 : [:map
36 28 : [:cookie-name [:string {:min 1}]]
37 28 : [:ttl-seconds [:int {:min 1}]]
38 28 : [:auth-date-lifetime-ms [:int {:min 1}]]
39 12 : [:secure? :boolean]
40 28 : [:same-site [:enum "lax" "strict" "none"]]
41 20 : [:domain [:maybe :string]]]
42 20 : [:fn {:error/message "when :same-site is \"none\", :secure? must be true"}
43 4 : (fn [{:keys [same-site secure?]}]
44 33 : (or (not= same-site "none") secure?))]]]
45 20 : [:root-telegram-ids {:optional true}
46 24 : [:or :string [:sequential :int]]]
47 20 : [:root-user-uuids {:optional true}
48 24 : [:or :string [:sequential :string]]]]]
49 8 : [:postgres
50 8 : [:map
51 12 : [:host :string]
52 12 : [:port :int]
53 12 : [:dbname :string]
54 12 : [:user :string]
55 12 : [:password :string]
56 8 : [:flyway
57 44 : [:map [:migrations-location :string] [:baseline-on-migrate :boolean] [:table :string]]]]]
58 8 : [:redis
59 8 : [:map
60 12 : [:host :string]
61 12 : [:port :int]
62 12 : [:timeout-ms :int]
63 20 : [:password {:optional true}
64 12 : [:maybe :string]]]]
65 40 : [:nats [:map [:url :string] [:consumer-name :string]]]
66 40 : [:jetty [:map [:port :int] [:join? :boolean]]]
67 20 : [:sentry {:optional true}
68 8 : [:map
69 24 : [:enabled? {:optional true}
70 : :boolean]
71 20 : [:dsn [:maybe :string]]]]
72 20 : [:otel {:optional true}
73 8 : [:map
74 12 : [:enabled? :boolean]
75 20 : [:endpoint {:optional true}
76 12 : [:maybe :string]]
77 20 : [:service-name {:optional true}
78 12 : [:maybe :string]]]]
79 20 : [:meilisearch {:optional true}
80 8 : [:map
81 20 : [:url [:maybe :string]]
82 20 : [:api-key {:optional true}
83 12 : [:maybe :string]]
84 20 : [:index-prefix {:optional true}
85 12 : [:maybe :string]]
86 20 : [:timeout-ms {:optional true}
87 20 : [:int {:min 1}]]
88 24 : [:indexer-enabled? {:optional true}
89 : :boolean]
90 20 : [:poll-seconds {:optional true}
91 20 : [:int {:min 1}]]]]
92 8 : [:media
93 8 : [:map
94 12 : [:uploads-enabled? :boolean]
95 52 : [:storage [:map [:bucket :string] [:region :string] [:endpoint :string]]]]]])
96 :
97 :
98 44 : (defn- without-nil-values [m] (into {} (remove (comp nil? val)) m))
99 :
100 :
101 4 : (defn- apply-defaults
102 : [config]
103 28 : (let [enabled (get-in config [:media :uploads-enabled])]
104 12 : (->
105 8 : (merge
106 20 : {:sentry {}
107 4 : :otel {}
108 12 : :media {:storage {}}}
109 4 : config)
110 16 : (update
111 : :sentry
112 17 : #(if (map? %)
113 8 : (merge
114 20 : {:enabled? false
115 : :dsn nil}
116 12 : (without-nil-values %))
117 : %))
118 16 : (update
119 : :otel
120 17 : #(if (map? %)
121 8 : (merge
122 28 : {:enabled? false
123 : :endpoint "http://localhost:4317"
124 : :service-name "matchscore-backend"}
125 12 : (without-nil-values %))
126 : %))
127 40 : (update :media #(merge {:storage {}} %))
128 12 : (update-in
129 12 : [:media :storage]
130 12 : #(merge
131 28 : {:bucket ""
132 : :region ""
133 : :endpoint ""}
134 : %))
135 8 : (assoc-in
136 12 : [:media :uploads-enabled?]
137 16 : (if (boolean? enabled)
138 0 : enabled
139 8 : (=
140 : "true"
141 62 : (some->
142 4 : enabled
143 4 : str
144 4 : str/lower-case))))
145 8 : (update :media dissoc :uploads-enabled))))
146 :
147 :
148 4 : (defn load-config
149 : "Reads and validates the configuration from config.edn with the specified profile."
150 : []
151 40 : (let [profile (keyword (or (System/getenv "APP_ENV") "dev"))
152 8 : config (apply-defaults
153 32 : (aero/read-config (io/resource "matchscore/config.edn") {:profile profile}))]
154 20 : (if (m/validate ConfigSchema config)
155 4 : config
156 0 : (throw
157 0 : (ex-info "Invalid configuration" {:errors (me/humanize (m/explain ConfigSchema config))})))))
|