@breautek/storm
Version:
Object-Oriented REST API framework
259 lines (256 loc) • 10.5 kB
JavaScript
"use strict";
/* eslint-disable @typescript-eslint/no-require-imports */
/// <reference path="./defs/merge-change.d.ts" />
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigLoader = void 0;
const tslib_1 = require("tslib");
/*
Copyright 2017-2021 Norman Breau
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const instance_1 = require("./instance");
const Path = tslib_1.__importStar(require("path"));
const ajv_1 = tslib_1.__importDefault(require("ajv"));
const merge_change_1 = tslib_1.__importDefault(require("@breautek/merge-change"));
const InvalidConfigError_1 = require("./InvalidConfigError");
const MissingConfigError_1 = require("./MissingConfigError");
const TAG = 'ConfigLoader';
class ConfigLoader {
constructor(app) {
this.$app = app;
this.$logger = app.getLogger();
}
async load(configFile, localConfigFile) {
let logger = this.$logger;
logger.trace(TAG, 'Configuration loaded.');
let config = {};
let cPath = configFile;
let lPath = localConfigFile;
let c;
let l;
let defaults = this.$getDefaults();
logger.trace(TAG, `Main Config Path:\t ${cPath}`);
logger.trace(TAG, `Local Config Path:\t ${lPath}`);
c = this.$getMainConfig(cPath);
l = this.$getLocalConfig(lPath);
if (l) {
config = merge_change_1.default.merge(defaults, c, l);
}
else {
config = merge_change_1.default.merge(defaults, c);
}
logger.trace(TAG, 'Reading command line arguments...');
// This pulls IConfig rules into the base config as expected
config = merge_change_1.default.merge(config, this.$getCmdLineArgs());
if (!config.customConfig) {
config.customConfig = {};
}
// This will pull all custom configurations into customConfig
config.customConfig = merge_change_1.default.merge(config.customConfig, this.$app.getConfigFromCLIArgs(this.$app.getCmdLineArgs().custom));
if (config.log.level === null) {
config.log.level = defaults.log.level;
}
logger.trace(TAG, 'Configurations merged.');
logger.trace(TAG, config);
await this.$validateSchema(config);
return config;
}
/**
* @deprecated Instantiate ConfigLoader with the proper params
* @param path
* @returns
*/
static async load(path) {
let loader = new ConfigLoader((0, instance_1.getInstance)());
return await loader.load(Path.resolve(path, 'bt-config.json'), Path.resolve('bt-local-config.json'));
}
$getLocalConfig(path) {
let config = null;
this.$logger.trace(TAG, 'Loading optional local config.');
try {
config = require(path);
this.$logger.trace(TAG, 'Local config loaded.');
}
catch (ex) {
this.$logger.trace(TAG, 'Local config could not be loaded.');
this.$logger.trace(TAG, ex);
}
return config;
}
$getMainConfig(path) {
this.$logger.trace(TAG, 'Loading main confing...');
let c = null;
try {
c = require(path);
this.$logger.trace(TAG, 'Main config loaded.');
}
catch (ex) {
throw new MissingConfigError_1.MissingConfigError(path);
}
return c;
}
$getDefaults() {
this.$logger.trace(TAG, 'Loading configuration defaults.');
return require(Path.resolve(__dirname, '../bt-config-defaults.json'));
}
async $validateSchema(config) {
let ajv = new ajv_1.default({
allErrors: true
});
let validate = ajv.compile({
type: 'object',
additionalProperties: false,
properties: {
bind: { type: ['string', 'null'] },
port: { type: ['number', 'null'] },
shard: { type: ['number', 'null'] },
authentication_header: { type: ['string', 'null'] },
backend_authentication_header: { type: ['string', 'null'] },
backend_authentication_secret: { type: ['string', 'null'] },
request_size_limit: { type: ['number', 'null'] },
prometheus: {
type: ['object', 'null'],
additionalProperties: false,
properties: {
enabled: {
type: ['boolean', 'null'],
default: false
},
bind: {
type: ['string', 'null'],
default: '127.0.0.1'
},
port: {
type: ['number', 'null']
}
}
},
log: {
type: ['object', 'null'],
additionalProperties: false,
properties: {
level: {
type: ['string', 'null'],
enum: [
'silly',
'debug',
'verbose',
'http',
'info',
'warn',
'error',
null
]
},
cloudwatch: {
type: ['object', 'null'],
additionalProperties: false,
properties: {
region: { type: 'string' },
credentials: {
type: 'object',
additionalProperties: false,
properties: {
accessKeyId: { type: 'string' },
secretAccessKey: { type: 'string' }
}
},
stream: {
type: 'object',
additionalProperties: false,
properties: {
group: { type: 'string' },
name: { type: 'string' }
}
}
}
},
filters: {
type: ['array', 'null'],
items: { type: 'string' }
}
}
},
database: {
type: ['object', 'null'],
additionalProperties: false,
properties: {
query_timeout: { type: ['integer', 'null'], minimum: 0 },
main: {
type: ['object', 'null'],
additionalProperties: false,
properties: {
name: { type: 'string', const: "MASTER" },
host: { type: 'string' },
port: { type: 'integer', minimum: 0 },
database: { type: 'string' },
user: { type: 'string' },
password: { type: 'string' }
}
},
replicationNodes: {
type: ['array', 'null'],
items: {
type: 'object',
additionalProperties: false,
properties: {
name: { type: 'string' },
host: { type: 'string' },
port: { type: 'integer', minimum: 0 },
database: { type: 'string' },
user: { type: 'string' },
password: { type: 'string' }
}
}
}
}
},
enableMySQL2BreakingChanges: { type: ['boolean', 'null'] },
customConfig: {
type: 'object',
additionalProperties: true,
properties: {}
}
}
});
let isValid = validate(config);
if (!isValid) {
throw new InvalidConfigError_1.InvalidConfigError(config, validate.errors);
}
}
$getCmdLineArgs() {
let cliArgs = this.$app.getCmdLineArgs();
let out = {
prometheus: {}
};
if (cliArgs.bind !== undefined) {
out.bind = cliArgs.bind;
}
if (cliArgs.port !== undefined) {
out.port = cliArgs.port;
}
if (cliArgs.authentication_header !== undefined) {
out.authentication_header = cliArgs.authentication_header;
}
if (cliArgs.custom?.shard !== undefined) {
out.shard = parseInt(cliArgs.custom.shard);
}
if (cliArgs.custom?.prometheusBind !== undefined) {
out.prometheus.bind = cliArgs.custom.prometheusBind;
}
if (cliArgs.custom?.prometheusPort !== undefined) {
out.prometheus.port = parseInt(cliArgs.custom.prometheusPort);
}
return out;
}
}
exports.ConfigLoader = ConfigLoader;
//# sourceMappingURL=ConfigLoader.js.map