Use nested config structure
This commit is contained in:
1 parent
581a510960
commit
30dbf0cf40
15 files changed
+366
-251
No files matched your search
+29
-64
@@ -1,72 +1,37 @@
|
||||
"use strict";
|
||||
|
||||
const filterParser = require("../common/filter-parser.js");
|
||||
const Filter = require("../common/filter.js");
|
||||
|
||||
const configFile = require("../config.json");
|
||||
|
||||
const cache = {};
|
||||
|
||||
function parse(name, value) {
|
||||
if (/^ui\.overview\.charts\.[^.]+\.slices\.[^.]+\.filter$/.test(name))
|
||||
return new Filter(value);
|
||||
else if (/^permissions\.[^.]+\.[^.]+\.devices\.filter$/.test(name))
|
||||
return new Filter(value);
|
||||
else if (/^auth\.[^.]+\.users\.[^.]+\.roles$/.test(name))
|
||||
return value
|
||||
.split(",")
|
||||
.map(x => x.trim())
|
||||
.sort();
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function get(name) {
|
||||
let v = cache[name];
|
||||
if (v === undefined) {
|
||||
v = configFile[name];
|
||||
if (v != null) {
|
||||
v = parse(name, v);
|
||||
} else {
|
||||
const prefix = `${name}.`;
|
||||
for (let k of Object.keys(configFile))
|
||||
if (k.startsWith(prefix)) {
|
||||
v = v || {};
|
||||
let ar = k.slice(prefix.length).split(".");
|
||||
let ref = v;
|
||||
for (let a of ar.slice(0, -1)) ref = ref[a] = ref[a] || {};
|
||||
ref[ar[ar.length - 1]] = parse(k, configFile[k]);
|
||||
}
|
||||
}
|
||||
if (v) cache[name] = v;
|
||||
else cache[name] = null;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
let config;
|
||||
|
||||
function getClientConfig() {
|
||||
const regex = [
|
||||
/^ui\.pageSize$/,
|
||||
/^ui\.filters\.[^.]+\.label$/,
|
||||
/^ui\.filters\.[^.]+\.parameter$/,
|
||||
/^ui\.filters\.[^.]+\.type$/,
|
||||
/^ui\.index\./,
|
||||
/^ui\.overview\.groups\./,
|
||||
/^ui\.overview\.charts\./,
|
||||
/^ui\.device/
|
||||
];
|
||||
|
||||
const clientConfig = {};
|
||||
for (let [k, v] of Object.entries(configFile))
|
||||
for (let r of regex)
|
||||
if (r.test(k)) {
|
||||
clientConfig[k] = v;
|
||||
break;
|
||||
}
|
||||
|
||||
return clientConfig;
|
||||
return Object.assign({}, config, {
|
||||
server: null,
|
||||
auth: null,
|
||||
permissions: null
|
||||
});
|
||||
}
|
||||
|
||||
exports.get = get;
|
||||
exports.getClientConfig = getClientConfig;
|
||||
function loadConfig(conf, root = {}) {
|
||||
for (const [key, value] of Object.entries(conf)) {
|
||||
const keys = key.split(".");
|
||||
let ref = root;
|
||||
while (keys.length > 1) {
|
||||
const k = keys.shift();
|
||||
if (typeof ref[k] !== "object") ref[k] = {};
|
||||
ref = ref[k];
|
||||
}
|
||||
|
||||
if (typeof value === "object") {
|
||||
if (typeof ref[keys[0]] !== "object") ref[keys[0]] = {};
|
||||
loadConfig(value, ref[keys[0]]);
|
||||
} else {
|
||||
ref[keys[0]] = value;
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
config = loadConfig(configFile);
|
||||
|
||||
module.exports = Object.assign({}, config, { getClientConfig });
|
||||
Reference in new issue
Block a user