Move UI configs to DB
This commit is contained in:
1 parent
192ed6b034
commit
1cceba3f8b
22 files changed
+337
-448
No files matched your search
+19
-9
@@ -4,7 +4,7 @@ const http = require("http");
|
||||
const https = require("https");
|
||||
const url = require("url");
|
||||
|
||||
const config = require("./config");
|
||||
const config = require("../config");
|
||||
const db = require("./db");
|
||||
const mongodbFunctions = require("./mongodb-functions");
|
||||
|
||||
@@ -71,7 +71,9 @@ function deleteResource(resource, id) {
|
||||
const resourceUrl =
|
||||
resource === "virtualParameters" ? "virtual_parameters" : resource;
|
||||
const options = url.parse(
|
||||
`${config.server.nbi}${resourceUrl}/${encodeURIComponent(id)}`
|
||||
`http://127.0.0.1:${config.get(
|
||||
"NBI_PORT"
|
||||
)}/${resourceUrl}/${encodeURIComponent(id)}`
|
||||
);
|
||||
|
||||
options.method = "DELETE";
|
||||
@@ -91,9 +93,9 @@ function deleteResource(resource, id) {
|
||||
|
||||
function postTask(task, connectionRequest, callback) {
|
||||
const options = url.parse(
|
||||
`${config.server.nbi}devices/${encodeURIComponent(task.device)}/tasks${
|
||||
connectionRequest ? "?connection_request" : ""
|
||||
}`
|
||||
`http://127.0.0.1:${config.get("NBI_PORT")}/devices/${encodeURIComponent(
|
||||
task.device
|
||||
)}/tasks${connectionRequest ? "?connection_request" : ""}`
|
||||
);
|
||||
options.method = "POST";
|
||||
const _http = options.protocol === "https:" ? https : http;
|
||||
@@ -210,7 +212,9 @@ function updateTags(deviceId, tags) {
|
||||
([tag, onOff]) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const options = url.parse(
|
||||
`${config.server.nbi}devices/${encodeURIComponent(
|
||||
`http://127.0.0.1:${config.get(
|
||||
"NBI_PORT"
|
||||
)}/devices/${encodeURIComponent(
|
||||
deviceId
|
||||
)}/tags/${encodeURIComponent(tag)}`
|
||||
);
|
||||
@@ -234,7 +238,9 @@ function updateTags(deviceId, tags) {
|
||||
function ping(host) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = url.parse(
|
||||
`${config.server.nbi}ping/${encodeURIComponent(host)}`
|
||||
`http://127.0.0.1:${config.get("NBI_PORT")}/ping/${encodeURIComponent(
|
||||
host
|
||||
)}`
|
||||
);
|
||||
|
||||
const _http = options.protocol === "https:" ? https : http;
|
||||
@@ -294,7 +300,9 @@ function putResource(resource, id, data) {
|
||||
const resourceUrl =
|
||||
resource === "virtualParameters" ? "virtual_parameters" : resource;
|
||||
const options = url.parse(
|
||||
`${config.server.nbi}${resourceUrl}/${encodeURIComponent(id)}`
|
||||
`http://127.0.0.1:${config.get(
|
||||
"NBI_PORT"
|
||||
)}/${resourceUrl}/${encodeURIComponent(id)}`
|
||||
);
|
||||
|
||||
options.method = "PUT";
|
||||
@@ -343,7 +351,9 @@ function putResource(resource, id, data) {
|
||||
function putFile(id, metadata, fileStream) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = url.parse(
|
||||
`${config.server.nbi}files/${encodeURIComponent(id)}`
|
||||
`http://127.0.0.1:${config.get("NBI_PORT")}/files/${encodeURIComponent(
|
||||
id
|
||||
)}`
|
||||
);
|
||||
|
||||
options.method = "PUT";
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const configFile = JSON.parse(fs.readFileSync("./config/config-ui.json"));
|
||||
|
||||
const config = loadConfig(configFile);
|
||||
|
||||
function getClientConfig() {
|
||||
return Object.assign({}, config, {
|
||||
server: null,
|
||||
auth: null,
|
||||
permissions: null
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
module.exports = Object.assign({}, config, { getClientConfig });
|
||||
+6
-17
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
const mongodb = require("mongodb");
|
||||
const config = require("./config");
|
||||
const config = require("../config");
|
||||
const mongodbFunctions = require("./mongodb-functions");
|
||||
const expression = require("../common/expression");
|
||||
|
||||
@@ -9,24 +9,13 @@ const CACHE_TTL = 300000;
|
||||
|
||||
let _clientPromise = null;
|
||||
|
||||
const RESOURCE_DB = {
|
||||
devices: "genieacs",
|
||||
faults: "genieacs",
|
||||
tasks: "genieacs",
|
||||
presets: "genieacs",
|
||||
provisions: "genieacs",
|
||||
virtualParameters: "genieacs",
|
||||
files: "genieacs",
|
||||
cache: "genieacs-ui"
|
||||
};
|
||||
|
||||
const RESOURCE_COLLECTION = {
|
||||
files: "fs.files"
|
||||
};
|
||||
|
||||
function ensureIndexes(client) {
|
||||
client
|
||||
.db(RESOURCE_DB["cache"])
|
||||
.db()
|
||||
.collection("cache")
|
||||
.createIndex({ expire: 1 }, { expireAfterSeconds: 0 });
|
||||
}
|
||||
@@ -34,7 +23,7 @@ function ensureIndexes(client) {
|
||||
function getClient() {
|
||||
if (!_clientPromise) {
|
||||
_clientPromise = new Promise((resolve, reject) => {
|
||||
const CONNECTION_URL = config.server.mongodbConnectionUrl;
|
||||
const CONNECTION_URL = config.get("MONGODB_CONNECTION_URL");
|
||||
mongodb.MongoClient.connect(
|
||||
CONNECTION_URL,
|
||||
{ useNewUrlParser: true },
|
||||
@@ -54,7 +43,7 @@ function cache(key, valueGetter, ttl) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getClient()
|
||||
.then(client => {
|
||||
const collection = client.db(RESOURCE_DB["cache"]).collection("cache");
|
||||
const collection = client.db().collection("cache");
|
||||
collection.findOne({ _id: key }, (err, doc) => {
|
||||
if (err) return void reject(err);
|
||||
if (doc != null) return void resolve(JSON.parse(doc.value));
|
||||
@@ -103,7 +92,7 @@ function query(resource, filter, options, callback) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getClient().then(client => {
|
||||
const collection = client
|
||||
.db(RESOURCE_DB[resource])
|
||||
.db()
|
||||
.collection(RESOURCE_COLLECTION[resource] || resource);
|
||||
const cursor = collection.find(q);
|
||||
if (options.projection) {
|
||||
@@ -188,7 +177,7 @@ function count(resource, filter) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getClient().then(client => {
|
||||
const collection = client
|
||||
.db(RESOURCE_DB[resource])
|
||||
.db()
|
||||
.collection(RESOURCE_COLLECTION[resource] || resource);
|
||||
collection.find(q).count((err, c) => {
|
||||
if (err) reject(err);
|
||||
|
||||
Reference in new issue
Block a user