Move UI configs to DB

This commit is contained in:
Shweelan Samson authored and Zaid Abdulla committed 2019-01-16 05:22:14 -08:00
1 parent 192ed6b034
commit 1cceba3f8b
22 files changed
+337 -448

No files matched your search

+63 -4
View File
@@ -307,8 +307,30 @@ function refresh(callback) {
db.getConfig((err, conf) => {
if (err) return void reject(err);
conf.sort((a, b) => (a.id > b.id ? 1 : a.id < b.id ? -1 : 0));
const _config = {};
for (const c of conf) _config[c.id] = c.value;
for (const c of conf) {
if (
c.id.startsWith("auth.") ||
c.id.startsWith("permissions.") ||
c.id.startsWith("ui.")
) {
const keys = c.id.split(".");
let ref = _config;
while (keys.length > 1) {
const k = keys.shift();
if (typeof ref[k] !== "object") ref[k] = {};
ref = ref[k];
}
// Evaluate expressions to simplify them
ref[keys[0]] = expression.evaluate(c.value);
} else {
// Evaluate expressions to simplify them
_config[c.id] = expression.evaluate(c.value);
}
}
resolve(_config);
});
@@ -348,9 +370,22 @@ function refresh(callback) {
}
function getCurrentSnapshot(callback) {
if (Date.now() < nextRefresh) return void callback(null, currentSnapshot);
refresh(err => {
callback(err, currentSnapshot);
if (Date.now() < nextRefresh) {
if (callback) return callback(null, currentSnapshot);
return Promise.resolve(currentSnapshot);
}
if (callback) {
return refresh(err => {
callback(err, currentSnapshot);
});
}
return new Promise((resolve, reject) => {
refresh(err => {
if (err) return void reject(err);
resolve(currentSnapshot);
});
});
}
@@ -410,6 +445,27 @@ function getConfig(snapshotKey, key, context, now) {
return expression.evaluate(snapshot.config[key], context, now || Date.now());
}
function getAuthConfig(snapshotKey) {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
return snapshot.config.auth;
}
function getPermissionsConfig(snapshotKey) {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
return snapshot.config.permissions;
}
function getUiConfig(snapshotKey) {
const snapshot = snapshots.get(snapshotKey);
if (!snapshot) throw new Error("Cache snapshot does not exist");
return snapshot.config.ui;
}
exports.getCurrentSnapshot = getCurrentSnapshot;
exports.hasSnapshot = hasSnapshot;
exports.getPresets = getPresets;
@@ -417,3 +473,6 @@ exports.getProvisions = getProvisions;
exports.getVirtualParameters = getVirtualParameters;
exports.getFiles = getFiles;
exports.getConfig = getConfig;
exports.getAuthConfig = getAuthConfig;
exports.getPermissionsConfig = getPermissionsConfig;
exports.getUiConfig = getUiConfig;