-
Notifications
You must be signed in to change notification settings - Fork 1
/
gyconfig.js
106 lines (80 loc) · 2.85 KB
/
gyconfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
require('console-stamp')(console, {
format: ':date(yyyy-mm-dd HH:MM:ss.l)::label:'
});
const fs = require('fs');
const moment = require('moment');
const {safetypeof} = require("./gyutil.js");
let gyconfig;
function initGlobalConfig(printcfg = true)
{
const env = process.env;
let cstr = '{\n\t';
gyconfig = {};
if (!env.CFG_SHYAMA_HOSTS) {
console.error(`Invalid Alert Action Config : Mandatory Environment Config 'CFG_SHYAMA_HOSTS' not found : Please set CFG_SHYAMA_HOSTS value in .env file`);
process.exit(1);
}
if (env.CFG_SHYAMA_HOSTS[0] !== '[') {
console.error(`Invalid Alert Action Config : Mandatory Environment Config CFG_SHYAMA_HOSTS=${env.CFG_SHYAMA_HOSTS} not of JSON Array format`);
process.exit(1);
}
cstr += `"ShyamaHostArr" : ${env.CFG_SHYAMA_HOSTS},\n\t`;
if (!env.CFG_SHYAMA_PORTS) {
console.error(`Invalid Alert Action Config : Mandatory Environment Config 'CFG_SHYAMA_PORTS' not found : Please set CFG_SHYAMA_PORTS value in .env file`);
process.exit(1);
}
if (env.CFG_SHYAMA_PORTS[0] !== '[') {
console.error(`Invalid Alert Action Config : Mandatory Environment Config CFG_SHYAMA_PORTS=${env.CFG_SHYAMA_PORTS} not of JSON Array format`);
process.exit(1);
}
cstr += `"ShyamaPortArr" : ${env.CFG_SHYAMA_PORTS},\n\t`;
if (env.CFG_LOGFILE) {
if (env.CFG_LOGFILE[0] !== '"') {
cstr += `"logFile" : "${env.CFG_LOGFILE}",\n\t`;
}
else {
cstr += `"logFile" : ${env.CFG_LOGFILE},\n\t`;
}
}
cstr += `"tinit" : "${moment().format()}"\n}`;
if (printcfg) {
console.info(`Alert Action Config options : \n${cstr}`);
}
try {
gyconfig = JSON.parse(cstr);
}
catch(e) {
if (!printcfg) {
console.info(`Alert Action Config options : \n${cstr}`);
}
console.error(`[ERROR]: Alert Action Config not in JSON format : ${e}\n`);
process.exit(1);
}
if (!Array.isArray(gyconfig.ShyamaHostArr)) {
console.error(`Invalid Alert Action Config : Mandatory Environment Config CFG_SHYAMA_HOSTS=${env.CFG_SHYAMA_HOSTS} not in JSON Array format`);
process.exit(1);
}
if (!Array.isArray(gyconfig.ShyamaPortArr)) {
console.error(`Invalid Alert Action Config : Mandatory Environment Config CFG_SHYAMA_PORTS=${env.CFG_SHYAMA_PORTS} not in JSON Array format`);
process.exit(1);
}
if (gyconfig.ShyamaHostArr.length !== gyconfig.ShyamaPortArr.length) {
console.error(`Invalid Alert Action Config : CFG_SHYAMA_HOSTS and CFG_SHYAMA_PORTS Array lengths differ in size`);
process.exit(1);
}
gyconfig.projectName = 'Gyeeta';
gyconfig.NodeHostname = require('os').hostname();
if (process.env.NODE_ENV !== "production") {
console.log('NOTE : Using Development Node Settings. Please set .env NODE_ENV=production if Production settings needed');
}
return gyconfig;
}
function getGlobalConfig()
{
return gyconfig;
}
module.exports = {
initGlobalConfig,
getGlobalConfig,
};