🔥 Remove the need of static (build time) configuration.

This commit is contained in:
Andrey Antukh 2020-08-18 16:33:12 +02:00
parent 2746d598b0
commit fb910a24e1
4 changed files with 32 additions and 64 deletions

View file

@ -1,4 +0,0 @@
#!/usr/bin/env bash
set -ex
#clojure -Ojmx-remote -A:dev -e "(set! *warn-on-reflection* true)" -m rebel-readline.main
clojure -Ojmx-remote -A:dev -m rebel-readline.main

View file

@ -108,44 +108,15 @@ function readLocales() {
return JSON.stringify(result); return JSON.stringify(result);
} }
function readConfig(data) { function readManifest() {
const googleClientID = process.env.UXBOX_GOOGLE_CLIENT_ID;
const demoWarn = process.env.UXBOX_DEMO_WARNING;
const deployDate = process.env.UXBOX_DEPLOY_DATE;
const deployCommit = process.env.UXBOX_DEPLOY_COMMIT;
const loginWithLDAP = process.env.UXBOX_LOGIN_WITH_LDAP;
let cfg = {
demoWarning: demoWarn === "true"
};
if (googleClientID !== undefined) {
cfg.googleClientID = googleClientID;
}
if (deployDate !== undefined) {
cfg.deployDate = deployDate;
}
if (deployCommit !== undefined) {
cfg.deployCommit = deployCommit;
}
if (loginWithLDAP !== undefined) {
cfg.loginWithLDAP = loginWithLDAP;
}
Object.assign(cfg, data);
return JSON.stringify(cfg);
}
function readManifest(publicURI) {
try { try {
const path = __dirname + "/resources/public/js/manifest.json"; const path = __dirname + "/resources/public/js/manifest.json";
const content = JSON.parse(fs.readFileSync(path, {encoding: "utf8"})); const content = JSON.parse(fs.readFileSync(path, {encoding: "utf8"}));
const index = {}; const index = {
"config": "/js/config.js?ts=" + Date.now()
};
for (let item of content) { for (let item of content) {
index[item.name] = "/js/" + item["output-name"]; index[item.name] = "/js/" + item["output-name"];
}; };
@ -154,6 +125,7 @@ function readManifest(publicURI) {
} catch (e) { } catch (e) {
console.error("Error on reading manifest, using default."); console.error("Error on reading manifest, using default.");
return { return {
"config": "/js/config.js",
"main": "/js/main.js", "main": "/js/main.js",
"shared": "/js/shared.js", "shared": "/js/shared.js",
"worker": "/js/worker.js" "worker": "/js/worker.js"
@ -180,24 +152,23 @@ function templatePipeline(options) {
const output = options.output; const output = options.output;
const ts = Math.floor(new Date()); const ts = Math.floor(new Date());
const publicURI = process.env.UXBOX_PUBLIC_URI || "http://localhost:3449";
const th = process.env.UXBOX_THEME || "default"; const th = process.env.UXBOX_THEME || "default";
const deployDate = process.env.UXBOX_DEPLOY_DATE;
const deployCommit = process.env.UXBOX_DEPLOY_COMMIT;
const themes = ["default"]; const themes = ["default"];
const locales = readLocales(); const locales = readLocales();
const manifest = readManifest(publicURI); const manifest = readManifest();
const config = readConfig({ const defaultConf = `var uxboxConfig = {demoWarning: false, googleClientID: null, loginWithLDAP: null, publicURI: null};`
workerURI: manifest.worker, fs.writeFileSync(__dirname + "/resources/public/js/config.js", defaultConf)
publicURI: publicURI
});
const tmpl = mustache({ const tmpl = mustache({
deployCommit,
deployDate,
ts: ts, ts: ts,
th: th, th: th,
manifest: manifest, manifest: manifest,
config: JSON.stringify(config),
translations: JSON.stringify(locales), translations: JSON.stringify(locales),
themes: JSON.stringify(themes), themes: JSON.stringify(themes),
}); });

View file

@ -12,13 +12,17 @@
<section id="app" tabindex="1"></section> <section id="app" tabindex="1"></section>
<section id="modal"></section> <section id="modal"></section>
<script> <script>
window.uxboxConfig = JSON.parse({{& config }}); window.uxboxTranslations = JSON.parse({{& translations }});
window.uxboxTranslations = JSON.parse({{& translations }}); window.uxboxThemes = {{& themes }};
window.uxboxThemes = {{& themes }}; window.uxboxDeployDate = "{{& deployDate }}";
window.uxboxDeployCommit = "{{& deployCommit }}";
</script> </script>
{{# manifest}} {{# manifest}}
<script src="{{& shared}}"></script>
<script src="{{& main}}"></script> <script>window.uxboxWorkerURI="{{& worker}}"</script>
<script src="{{& config}}"></script>
<script src="{{& shared}}"></script>
<script src="{{& main}}"></script>
{{/manifest}} {{/manifest}}
</body> </body>
</html> </html>

View file

@ -11,20 +11,17 @@
(:require [uxbox.util.object :as obj])) (:require [uxbox.util.object :as obj]))
(this-as global (this-as global
(let [config (obj/get global "uxboxConfig") (let [config (obj/get global "uxboxConfig" {})
puri (obj/get config "publicURI") wuri (obj/get global "uxboxWorkerURI" "/js/worker.js")]
wuri (obj/get config "workerURI")
gcid (obj/get config "googleClientID" true)
lwl (obj/get config "loginWithLDAP" false)
warn (obj/get config "demoWarning" true)]
(def default-language "en") (def default-language "en")
(def demo-warning warn) (def demo-warning (obj/get config "demoWarning" false))
(def google-client-id gcid) (def google-client-id (obj/get config "googleClientID"))
(def login-with-ldap lwl) (def login-with-ldap (obj/get config "loginWithLDAP" false))
(def worker-uri wuri) (def worker-uri wuri)
(def public-uri puri) (def public-uri (or (obj/get config "publicURI")
(def media-uri (str puri "/media")) (.-origin ^js js/location)))
(def default-theme "default"))) (def media-uri (str public-uri "/media"))
(def default-theme "default")))
(defn resolve-media-path (defn resolve-media-path
[path] [path]