@jschmold/kor
Version:
`npm i mocha ts-node typedoc typescript --global`
83 lines (82 loc) • 3.33 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var module_1 = require("./module");
var logging_1 = require("./logging");
var settings_1 = require("./settings");
var fs_1 = require("fs");
var path_1 = require("path");
var js_yaml_1 = require("js-yaml");
var express = require("express");
var KorApplication = (function () {
function KorApplication(arg) {
this.settings = {
mode: 'development',
port: 8080,
dataConfigs: 'databases',
verbosity: 'warn',
secret: ''
};
this.router = express();
this.dataConnections = new Map();
this.settings = __assign({}, this.settings, arg);
verifySettings(this);
logging_1.restoreLogging();
logging_1.initializeLogging(this.settings);
for (var _i = 0, _a = this.settings.dataSources; _i < _a.length; _i++) {
var source = _a[_i];
var path = path_1.join(this.settings.dataConfigs, "" + (source.file.includes('yaml') ? source.file : source.file + '.yaml'));
var yaml = js_yaml_1.safeLoad(fs_1.readFileSync(path, 'utf8'));
if (yaml == null)
throw new Error("Unable to load " + path + " for data config");
this.dataConnections.set(source.name, yaml);
}
}
KorApplication.prototype.confIncludesSource = function (key) {
if (this.settings.dataSources == null)
throw new Error('dataSources is undefined');
return this.settings.dataSources.map(function (obj) { return obj.name; }).indexOf(key) > -1;
};
KorApplication.prototype.listen = function () {
var port = this.settings.port || 8080;
console.debug('Pre-listen, intended port ', port);
this.router.listen(this.settings, function () {
console.info('Server running on port', port);
});
};
KorApplication.prototype.includesSource = function (key) {
return this.dataConnections.has(key);
};
KorApplication.prototype.getConnectionDetails = function (source) {
return this.dataConnections.get(source);
};
KorApplication.prototype.loadModules = function (mods) {
mods.forEach(module_1.LoadModule);
};
return KorApplication;
}());
exports.KorApplication = KorApplication;
function verifySettings(app) {
if (settings_1.verifyMode(app.settings.mode) !== true) {
throw new Error("Invalid mode " + app.settings.mode);
}
if (settings_1.verifyPort(app.settings.port) !== true) {
throw new Error("Invalid port " + app.settings.port);
}
if (settings_1.verifySecret(app.settings.secret) !== true) {
throw new Error("Invalid secret " + app.settings.secret + ". Needs to be >48 characters");
}
if (settings_1.verifyVerbosity(app.settings.verbosity) !== true) {
throw new Error("Invalid verbosity " + app.settings.verbosity);
}
}