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/.
|
|
|
|
;;
|
2021-04-10 09:43:04 +02:00
|
|
|
;; Copyright (c) UXBOX Labs SL
|
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
|
2021-09-21 15:12:46 +02:00
|
|
|
["fs" :as fs]
|
2020-06-29 16:07:48 +02:00
|
|
|
["process" :as process]
|
2021-09-21 15:12:46 +02:00
|
|
|
[app.common.exceptions :as ex]
|
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]
|
|
|
|
[cuerdas.core :as str]
|
2021-05-05 09:28:12 +02:00
|
|
|
[lambdaisland.uri :as u]))
|
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
|
|
|
|
:browser-concurrency 5
|
|
|
|
:browser-strategy :incognito})
|
2020-06-29 16:07:48 +02:00
|
|
|
|
2021-05-05 09:28:12 +02:00
|
|
|
(s/def ::browser-concurrency ::us/integer)
|
2021-09-21 15:12:46 +02:00
|
|
|
(s/def ::browser-executable-path ::us/string)
|
2021-05-05 09:28:12 +02:00
|
|
|
(s/def ::browser-strategy ::us/keyword)
|
2021-09-21 15:12:46 +02:00
|
|
|
(s/def ::http-server-port ::us/integer)
|
|
|
|
(s/def ::public-uri ::us/string)
|
2021-09-21 16:08:42 +02:00
|
|
|
(s/def ::sentry-dsn ::us/string)
|
|
|
|
(s/def ::tenant ::us/string)
|
|
|
|
(s/def ::host ::us/string)
|
2020-06-29 16:07:48 +02: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
|
|
|
::sentry-dsn
|
|
|
|
::host
|
|
|
|
::tenant
|
2021-05-05 09:28:12 +02:00
|
|
|
::http-server-port
|
|
|
|
::browser-concurrency
|
2021-09-15 14:28:57 +02:00
|
|
|
::browser-strategy
|
|
|
|
::browser-executable-path]))
|
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
|
|
|
|
[]
|
|
|
|
(let [env (read-env "penpot")
|
|
|
|
env (d/without-nils env)
|
|
|
|
data (merge defaults env)]
|
|
|
|
(us/conform ::config data)))
|
|
|
|
|
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
|
2021-09-21 16:08:42 +02:00
|
|
|
(atom (v/parse (or (some-> (ex/ignoring (fs/readFileSync "version.txt"))
|
|
|
|
(str/trim))
|
|
|
|
"%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)))
|