forked from GeographicaGS/urbo-pgsql-connector
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.js
306 lines (254 loc) · 8.94 KB
/
config.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2017 Telefónica Digital España S.L.
//
// This file is part of URBO PGSQL connector.
//
// URBO PGSQL connector is free software: you can redistribute it and/or
// modify it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// URBO PGSQL connector is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
// General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with URBO PGSQL connector. If not, see http://www.gnu.org/licenses/.
//
// For those usages not covered by this license please contact with
// iot_support at tid dot es
var yaml = require('js-yaml');
var fs = require('fs');
var _ = require('underscore');
/*
FIWARE NGSI APIv1
Context management using NGSI10 standard operations:
https://github.com/telefonicaid/fiware-orion/blob/develop/doc/manuals/user/walkthrough_apiv1.md#ngsi10-standard-operations
*/
var URL_AUTHTK = '/v3/auth/tokens';
var URL_QRY = '/v1/queryContext';
var URL_UDT = '/v1/updateContext';
var URL_SBC = '/v1/subscribeContext';
var URL_SBC_UPDATE = '/v1/updateContextSubscription';
var URL_SBC_DELETE = '/v1/unsubscribeContext';
// Logs params
var LOG_LEVELS = ['ALL', 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL', 'OFF'];
var LOG_OUTPUTS = ['console', 'file', 'dailyRotatingFile', 'sizeRotatingFile'];
// Log folders structure
var LOG_DEFAULT_DIR = './logs';
var LOG_DEFAULT_FILENAME = 'the_log';
var LOG_DEFAULT_MAX_SIZE = 20;
var LOG_DEFAULT_OLD_FILES = 5;
function Config() {
this._data = _(yaml.safeLoad(fs.readFileSync('config.yml', 'utf8')))
// Exclude from configuration keys started with "_". To be used
// to define auxiliary YAML reusable block references
.omit((v,k)=>k.startsWith('_'))
;
// Allow to define subscription attribute lists as a
// list of arrays containing attribute definitions
if (this._data.subscriptions){
for (let sub of this._data.subscriptions){
if (sub.attributes){
let attrsAsNestedArrays = _(sub.attributes.map((x)=>(x instanceof Array))).uniq();
if (attrsAsNestedArrays.length !== 1){
throw new Error(
`Subscription ${sub.id} can only be \
an array of attribute defs or an array \
of arrays of attribute defs.`
);
}
if (attrsAsNestedArrays[0]){
sub.attributes = _.flatten(sub.attributes)
}
}
}
}
// Set default params
var cdb = this._data.cartodb;
if (cdb) {
// Set default params for cartodb
if (!cdb.hasOwnProperty('active')){
cdb.active = true;
}
if (!cdb.hasOwnProperty('enterprise')){
cdb.enterprise = false;
}
}
this.getData = function(){
return this._data;
};
this.createFolderSync = function(folder){
fs.existsSync(folder) || fs.mkdirSync(folder);
};
this.getLogOpt = function() {
// Config vars
var _logging = this._data.logging;
var _file = _logging && _logging.file ? _logging.file : null;
var _access = _logging && _logging.access ? _logging.access : null;
// Filename vars
var fileDir = _file && _file.dir ? _file.dir : LOG_DEFAULT_DIR;
var suffixFilename = _file && _file.name ? _file.name : LOG_DEFAULT_FILENAME;
suffixFilename = `${ fileDir }/${ suffixFilename }`;
var errorFilename = `${ suffixFilename }-errors.log`;
var filename = `${ suffixFilename }.log`;
// Appenders definition
var fileErrorAppender = {
type: 'logLevelFilter',
level: 'ERROR',
appender: {
type: 'file',
filename: errorFilename
}
};
var logAppenderConsole = [
{
type: 'console'
}
];
var logAppenderFile = [
{
type: 'file',
filename: filename
}
];
var logAppenderDailyRotatingFile = [
{
type: 'dateFile',
filename: filename,
pattern: '.yyyy-MM-dd'
}
];
var logAppenderSizeRotatingFile = [
{
type: 'file',
filename: filename,
maxLogSize: Math.pow((_file && _file.maxSize ? _file.maxSize : LOG_DEFAULT_MAX_SIZE) * 1024, 2),
numBackups: _file && _file.oldFiles ? _file.oldFiles : LOG_DEFAULT_OLD_FILES
}
];
if (_file && _file.separateError) {
logAppenderFile.push(fileErrorAppender);
logAppenderDailyRotatingFile.push(fileErrorAppender);
logAppenderSizeRotatingFile.push(fileErrorAppender);
}
// log4js parameters definition
var logParams = {
output: LOG_OUTPUTS[0],
level: LOG_LEVELS[3],
access: { level: LOG_LEVELS[3] },
logappenders: logAppenderConsole
};
// Reading from config file
if (_logging) {
if (_logging.level && LOG_LEVELS.includes(_logging.level)) {
logParams.level = _logging.level;
}
if (_access && _access.level && LOG_LEVELS.includes(_access.level)) {
logParams.access.level = _access.level;
}
if (_access && _access.format) {
logParams.access.format = _access.format;
}
if (_access && _access.nolog) {
logParams.access.nolog = _access.nolog;
}
if (_logging.output && _logging.output.endsWith('ile')) {
this.createFolderSync(fileDir);
logParams.output = _logging.output;
if (_logging.output === 'file') {
logParams.logappenders = logAppenderFile;
} else if (_logging.output === 'dailyRotatingFile') {
logParams.logappenders = logAppenderDailyRotatingFile;
} else if (_logging.output === 'sizeRotatingFile') {
logParams.logappenders = logAppenderSizeRotatingFile;
}
// Creating a message for the console
errorFilename = _file && _file.separateError ? ' & ' + errorFilename : '';
logParams.consoleMessage = `Logging into files: ${ filename }${ errorFilename }`;
}
}
return logParams;
};
this.getSubs = function(){
return this._data.subscriptions;
};
this.cdbActiveFields = function(sub){
return _.contains(_.pluck(sub.attributes, 'cartodb'),true);
}
this.getSubServiceAuth = function(subserv){
var subServAuth = this.getSubService(subserv.subservice_id);
return {user: subServAuth.auth.user,
password: subServAuth.auth.password,
service: subServAuth.service
};
}
this.getSubService = function(id){
for (var i=0;i<this._data.subservices.length;i++){
var s = this._data.subservices[i];
if (s.id == id){
return s;
}
}
return null;
};
this.getCtxBrUrls = function(optype){
// Just because we need to handle different servers
var regex = /(^http?:\/\/[^\/]+(?=\/))(.*)/;
var urlCtxAuthBaseDomain = this._data.contextBrokerUrls.urlCtxAuthBase || this._data.contextBrokerUrls.urlCtxBrBase;
var urlCtxBrBaseDomain = this._data.contextBrokerUrls.urlCtxBrBase;
var urlCtxAuthBasePath = '';
var urlCtxBrBasePath = '';
var m;
if ( (m = regex.exec(urlCtxAuthBaseDomain)) !== null) {
urlCtxAuthBaseDomain = m[1];
urlCtxAuthBasePath = m[2];
}
if ( (m = regex.exec(urlCtxBrBaseDomain)) !== null) {
urlCtxBrBaseDomain = m[1];
urlCtxBrBasePath = m[2];
}
if (optype == 'authtk'){
if(!this._data.contextBrokerUrls.portAuthtk) return null;
var portAuthtk = this._data.contextBrokerUrls.portAuthtk;
return urlCtxAuthBaseDomain + ':' + portAuthtk + urlCtxAuthBasePath + URL_AUTHTK;
}
else{
var portCtxApi = this._data.contextBrokerUrls.portCtxApi;
var apiBase = urlCtxBrBaseDomain + ':' + portCtxApi + urlCtxBrBasePath;
if (optype === 'subscr')
return apiBase + URL_SBC;
else if (optype === 'updsbscr')
return apiBase + URL_SBC_UPDATE;
else if (optype === 'dltsbscr')
return apiBase + URL_SBC_DELETE;
else if (optype === 'update')
return apiBase + URL_UDT;
else if (optype === 'query')
return apiBase + URL_QRY;
else
return null
}
}
this.recreateSubscription = function(subscription) {
var rss = this._data.recreateSubscriptions || 'none';
var rs = subscription.recreateSubscription || false;
var _global = rss === 'global' ? true : false; // global is a reserverd word :(
var _single = rss === 'single' ? true : false; // _keeping the mood
rs = rs === true ? true : false; // Only accepting booleans here
if (_global || (_single && rs)) {
return true;
} else {
return false;
}
};
this.getFieldsForConstraint = function(subscription) {
var attributes = subscription.attributes.filter(function(attribute) {
return attribute.constraint;
});
return attributes.map(function(attribute) {
return attribute.namedb || attribute.name;
});
};
}
module.exports = new Config();