jsev
Version:
Environment for building Web API's.
40 lines • 1.57 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const koa_1 = __importDefault(require("koa"));
const koa_router_1 = __importDefault(require("koa-router"));
require("reflect-metadata");
const configuration_1 = require("./configuration");
const errors_1 = require("./errors");
const logging_1 = require("./logging");
const middlewares_1 = require("./middlewares");
class Environment {
constructor(rootPath) {
this.rootPath = rootPath;
this.app = new koa_1.default();
this.app.context.env = this;
this.router = new koa_router_1.default();
this.initPromise = this.init(rootPath);
}
async run() {
await this.initPromise;
if (!this.cfg) {
throw new errors_1.InvalidOperationError("Unable to run without initializing the configuration.");
}
await middlewares_1.applyMiddlewares(this);
this.app.listen(this.cfg.port);
this.log.info(`${this.cfg.name} listening on port ${this.cfg.port} (${this.cfg.env})`);
}
async init(rootPath) {
this.cfg = await configuration_1.loadConfiguration(rootPath);
this.log = logging_1.createLogger(this);
this.app.on("error", (err) => {
this.log.fatal(err);
});
this.middlewares = await middlewares_1.loadMiddlewares(rootPath);
}
}
exports.Environment = Environment;
//# sourceMappingURL=Environment.js.map