2021-02-15 12:15:16 +01:00
|
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;;
|
2022-09-20 23:23:22 +02:00
|
|
|
;; Copyright (c) KALEIDOS INC
|
2021-02-15 12:15:16 +01:00
|
|
|
|
2020-06-29 16:07:48 +02:00
|
|
|
(ns app.config
|
2021-05-05 09:28:12 +02:00
|
|
|
(:refer-clojure :exclude [get])
|
2020-06-29 16:07:48 +02:00
|
|
|
(:require
|
|
|
|
["process" :as process]
|
2021-09-15 14:28:57 +02:00
|
|
|
[app.common.data :as d]
|
2021-05-05 09:28:12 +02:00
|
|
|
[app.common.spec :as us]
|
2021-09-21 15:12:46 +02:00
|
|
|
[app.common.version :as v]
|
2021-05-05 09:28:12 +02:00
|
|
|
[cljs.core :as c]
|
2021-09-15 14:28:57 +02:00
|
|
|
[cljs.pprint]
|
|
|
|
[cljs.spec.alpha :as s]
|
2022-03-18 12:34:02 +01:00
|
|
|
[cuerdas.core :as str]))
|
2020-06-29 16:07:48 +02:00
|
|
|
|
2021-05-05 09:28:12 +02:00
|
|
|
(def defaults
|
|
|
|
{:public-uri "http://localhost:3449"
|
2021-09-21 16:08:42 +02:00
|
|
|
:tenant "dev"
|
|
|
|
:host "devenv"
|
2021-05-05 09:28:12 +02:00
|
|
|
:http-server-port 6061
|
2022-03-18 12:34:02 +01:00
|
|
|
:http-server-host "localhost"
|
2022-06-24 15:24:11 +02:00
|
|
|
:redis-uri "redis://redis/0"})
|
2020-06-29 16:07:48 +02:00
|
|
|
|
2021-09-21 15:12:46 +02:00
|
|
|
(s/def ::http-server-port ::us/integer)
|
2022-03-18 12:34:02 +01:00
|
|
|
(s/def ::http-server-host ::us/string)
|
|
|
|
(s/def ::public-uri ::us/uri)
|
2021-09-21 16:08:42 +02:00
|
|
|
(s/def ::tenant ::us/string)
|
|
|
|
(s/def ::host ::us/string)
|
2022-03-23 13:19:35 +01:00
|
|
|
(s/def ::browser-pool-max ::us/integer)
|
|
|
|
(s/def ::browser-pool-min ::us/integer)
|
2022-03-18 12:34:02 +01:00
|
|
|
|
2021-05-05 09:28:12 +02:00
|
|
|
(s/def ::config
|
|
|
|
(s/keys :opt-un [::public-uri
|
2021-09-21 16:08:42 +02:00
|
|
|
::host
|
|
|
|
::tenant
|
2021-05-05 09:28:12 +02:00
|
|
|
::http-server-port
|
2022-03-18 12:34:02 +01:00
|
|
|
::http-server-host
|
2022-03-23 13:19:35 +01:00
|
|
|
::browser-pool-max
|
2022-06-24 15:24:11 +02:00
|
|
|
::browser-pool-min]))
|
2022-03-18 12:34:02 +01:00
|
|
|
|
2021-05-05 09:28:12 +02:00
|
|
|
(defn- read-env
|
|
|
|
[prefix]
|
|
|
|
(let [env (unchecked-get process "env")
|
|
|
|
kwd (fn [s] (-> (str/kebab s) (str/keyword)))
|
2021-05-09 16:42:42 +02:00
|
|
|
prefix (str prefix "_")
|
2021-05-05 09:28:12 +02:00
|
|
|
len (count prefix)]
|
|
|
|
(reduce (fn [res key]
|
2021-05-09 16:42:42 +02:00
|
|
|
(let [val (unchecked-get env key)
|
|
|
|
key (str/lower key)]
|
|
|
|
(cond-> res
|
|
|
|
(str/starts-with? key prefix)
|
|
|
|
(assoc (kwd (subs key len)) val))))
|
2021-05-05 09:28:12 +02:00
|
|
|
{}
|
|
|
|
(js/Object.keys env))))
|
|
|
|
|
2021-05-09 16:42:42 +02:00
|
|
|
(defn- prepare-config
|
|
|
|
[]
|
2022-03-18 12:34:02 +01:00
|
|
|
(try
|
|
|
|
(let [env (read-env "penpot")
|
|
|
|
env (d/without-nils env)
|
|
|
|
data (merge defaults env)]
|
|
|
|
(us/conform ::config data))
|
|
|
|
(catch :default cause
|
|
|
|
(js/console.log (us/pretty-explain (ex-data cause)))
|
|
|
|
(throw cause))))
|
2021-05-09 16:42:42 +02:00
|
|
|
|
2021-05-05 09:28:12 +02:00
|
|
|
(def config
|
2021-05-09 16:42:42 +02:00
|
|
|
(atom (prepare-config)))
|
|
|
|
|
2021-09-21 15:12:46 +02:00
|
|
|
(def version
|
2022-01-20 14:27:03 +01:00
|
|
|
(atom (v/parse "%version%")))
|
2021-09-21 15:12:46 +02:00
|
|
|
|
2021-05-05 09:28:12 +02:00
|
|
|
(defn get
|
|
|
|
"A configuration getter."
|
|
|
|
([key]
|
|
|
|
(c/get @config key))
|
|
|
|
([key default]
|
|
|
|
(c/get @config key default)))
|