|
9 | 9 | (ns ^:no-doc com.fulcrologic.guardrails.config
|
10 | 10 | #?(:cljs (:require-macros com.fulcrologic.guardrails.config))
|
11 | 11 | (:require
|
12 |
| - [com.fulcrologic.guardrails.utils :as util] |
13 |
| - [taoensso.timbre :as log] |
| 12 | + [com.fulcrologic.guardrails.utils :as utils] |
14 | 13 | #?@(:clj [[clojure.edn :as edn]]
|
15 | 14 | :cljs [[cljs.env :as cljs-env]])))
|
16 | 15 |
|
|
44 | 43 | read-config-file
|
45 | 44 | (fn []
|
46 | 45 | #?(:clj (try
|
47 |
| - (edn/read-string (slurp (or |
48 |
| - (System/getProperty "guardrails.config") |
49 |
| - "guardrails.edn"))) |
| 46 | + (edn/read-string |
| 47 | + (slurp |
| 48 | + (or (System/getProperty "guardrails.config") |
| 49 | + "guardrails.edn"))) |
50 | 50 | (catch Exception _ nil))
|
51 | 51 | :cljs nil))
|
52 | 52 |
|
|
56 | 56 | (let [config (let [cljs-compiler-config
|
57 | 57 | (when cljs-env/*compiler*
|
58 | 58 | (get-in @cljs-env/*compiler* [:options :external-config :guardrails]))]
|
59 |
| - (when #?(:clj (or |
60 |
| - cljs-compiler-config |
61 |
| - (System/getProperty "guardrails.enabled")) :cljs false) |
| 59 | + (when #?(:clj (or |
| 60 | + cljs-compiler-config |
| 61 | + (System/getProperty "guardrails.enabled")) |
| 62 | + :cljs false) |
62 | 63 | (when-not @warned?
|
63 | 64 | (reset! warned? true)
|
64 |
| - (log/warn "GUARDRAILS IS ENABLED. RUNTIME PERFORMANCE WILL BE AFFECTED.") |
65 |
| - (log/info "Guardrails was enabled because" |
66 |
| - (if cljs-compiler-config |
67 |
| - "the CLJS Compiler config enabled it" |
68 |
| - "the guardrails.enabled property is set to a (any) value."))) |
69 |
| - (merge {} |
70 |
| - (read-config-file) |
71 |
| - cljs-compiler-config)))] |
72 |
| - #?(:clj |
73 |
| - (when (:pro? config) |
74 |
| - (try |
75 |
| - (require 'com.fulcrologic.guardrails-pro.core) |
76 |
| - (log/info "Guardrails Pro Enabled.") |
77 |
| - (catch Exception e |
78 |
| - (log/error e) |
79 |
| - (throw (ex-info "Guardrails Pro mode requested, but Guardrails Pro is not on the classpath!" {})))))) |
| 65 | + (utils/report-problem "GUARDRAILS IS ENABLED. RUNTIME PERFORMANCE WILL BE AFFECTED.") |
| 66 | + (utils/report-problem (str "Guardrails was enabled because " |
| 67 | + (if cljs-compiler-config |
| 68 | + "the CLJS Compiler config enabled it" |
| 69 | + "the guardrails.enabled property is set to a (any) value.")))) |
| 70 | + (merge {} (read-config-file))))] |
80 | 71 | ;#?(:clj (.println System/err config)) ; DEBUG
|
81 | 72 | config))]
|
82 | 73 |
|
83 | 74 | (defn get-env-config
|
84 | 75 | ([]
|
85 | 76 | (get-env-config true))
|
86 | 77 | ([cache?]
|
87 |
| - (let [result (if (or (not cache?) |
88 |
| - #?(:clj (= (System/getProperty "guardrails.cache") "false"))) |
89 |
| - (reload-config) |
90 |
| - (let [now (identity #?(:clj (System/currentTimeMillis) :cljs (js/Date.now)))] |
91 |
| - (if (< (- now (::timestamp @*config-cache)) |
92 |
| - 2000) |
93 |
| - (::value @*config-cache) |
94 |
| - (::value (reset! *config-cache {::timestamp now |
95 |
| - ::value (reload-config)})))))] |
| 78 | + (let [result (if (or (not cache?) |
| 79 | + #?(:clj (= "false" (System/getProperty "guardrails.cache")))) |
| 80 | + (reload-config) |
| 81 | + (let [now (identity #?(:clj (System/currentTimeMillis) :cljs (js/Date.now))) |
| 82 | + since-last (- now (::timestamp @*config-cache))] |
| 83 | + (if (< since-last 2000) |
| 84 | + (::value @*config-cache) |
| 85 | + (::value (reset! *config-cache {::timestamp now |
| 86 | + ::value (reload-config)}))))) |
| 87 | + cljs-compiler-config (when cljs-env/*compiler* |
| 88 | + (get-in @cljs-env/*compiler* [:options :external-config :guardrails])) |
| 89 | + mode-config #?(:cljs nil |
| 90 | + :clj (when-let [mode (System/getProperty "guardrails.mode")] |
| 91 | + (let [?mode (read-string mode)] |
| 92 | + (if (#{:runtime :pro :all} ?mode) |
| 93 | + {:mode ?mode} |
| 94 | + (.println System/err (format "Unknown guardrails mode %s, defaulting to :runtime" mode))))))] |
96 | 95 | #?(:clj (when (and result cljs-env/*compiler*)
|
97 |
| - (let [production? (contains? #{:advanced :whitespace :simple} (get-in @cljs-env/*compiler* [:options :optimizations]))] |
| 96 | + (let [production? (contains? #{:advanced :whitespace :simple} |
| 97 | + (get-in @cljs-env/*compiler* [:options :optimizations]))] |
98 | 98 | (when (and production? (not= "production" (System/getProperty "guardrails.enabled")))
|
99 | 99 | (throw (ex-info (str "REFUSING TO COMPILE PRODUCTION BUILD WITH GUARDRAILS ENABLED!. If you really want to take "
|
100 | 100 | "that performance hit then set the JVM properter guardrails.enabled to \"production\" on the CLJS compiler's JVM")
|
101 | 101 | {}))))))
|
102 |
| - result)))) |
| 102 | + (merge result cljs-compiler-config mode-config))))) |
103 | 103 |
|
104 | 104 | (defn get-base-config-fn
|
105 | 105 | "Base config is defaults + env config."
|
|
122 | 122 | (merge a b)
|
123 | 123 | b))
|
124 | 124 | (get-base-config-fn)
|
125 |
| - (util/get-ns-meta env) |
| 125 | + (utils/get-ns-meta env) |
126 | 126 | meta-maps)
|
127 | 127 | (into {}))]
|
128 | 128 | config))
|
| 129 | + |
| 130 | +(defn mode [config] |
| 131 | + (get config :mode :runtime)) |
0 commit comments