mission.api
Version:
mission api
110 lines • 4.24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const fs = require("fs");
const helmet = require("helmet");
const http = require("http");
const methodOverride = require("method-override");
const morgan = require("morgan");
const qs = require("qs");
const https = require("spdy");
const middleware_1 = require("../middleware");
const vendor_1 = require("../vendor");
const config_interface_1 = require("./config.interface");
class WebServer {
constructor(config, logger) {
this.config = config;
this.logger = logger;
this.app = vendor_1.express();
process.on('SIGINT', () => {
this.logger.warn(`Container received a shutdown signal. Application shutdown gracefully.`);
process.exit(0);
});
}
init() {
const c = this.config;
this.app.set('port', c.apiPort);
const arrayLimit = Number(process.env.QUERY_PARSER_ARRAY_LIMIT);
this.app.set('query parser', (queryString) => qs.parse(queryString, { arrayLimit }));
this.app.options('*', cors(c.corsOptions));
this.app.use(cors(c.corsOptions));
this.app.use(morgan(config_interface_1.morganType, config_interface_1.morganOption));
this.app.use(cookieParser());
this.app.use(bodyParser.json(c.optionsJson));
this.app.use(bodyParser.urlencoded(c.urlencodedOptions || { extended: false }));
this.app.use(methodOverride('X-HTTP-Method-Override'));
this.app.use(middleware_1.SetFilterMiddleware);
this.app.use(helmet());
return this;
}
addStaticFileRouting(route, path, config) {
this.logger.info(`Route: ${route} added. Path: ${path}`);
this.app.use(route, vendor_1.express.static(path, config));
}
addApiRouting(route, router) {
this.logger.info(`Route: ${route} added`);
this.app.use(route, router);
}
start() {
this.registerModules();
if (this.config.isHttpsEnabled) {
this.logger.info('HTTPS enabled');
const privateKey = fs.readFileSync(this.config.httpsKeypath, 'utf8');
const certificate = fs.readFileSync(this.config.httpsCertificatepath, 'utf8');
const credentials = { key: privateKey, cert: certificate };
this.server = https.createServer(credentials, this.app);
}
else {
this.logger.warn('Https disabled. Please enable HTTPS for data security.');
this.server = http.createServer(this.app);
}
this.logger.info('Server instance created');
this.server.listen(this.config.apiPort, null, (this.listenerCallback).bind(this));
return this;
}
stop(callback) {
this.server.close(callback);
this.logger.info('Express server stoped');
return this;
}
// public configure(): void {
// this.app.configure('development', () => {
// //TODO:
// });
// this.app.configure('testing', () => {
// //TODO:
// });
// this.app.configure('production', () => {
// //TODO:
// });
// }
handlerFor404(req, res, next) {
const err = new Error(`Resource not found ${req.method}: ${req.url}`);
res.status(404).json(err);
}
errorHandler(err, req, res, next) {
this.logger.error('Unhandled Error', err);
res.status(500).json(err);
}
registerModules() {
this.app.use(this.handlerFor404);
this.app.use((err, req, res, next) => {
if (req.transaction) {
req.transaction.rollback();
}
if (this.errorHandler) {
this.errorHandler(err, req, res, next);
}
});
}
listenerCallback() {
const port = this.app.get('port');
this.logger.info('Express server listening on port :' + port);
this.logger.info('Application Path :' + __dirname);
this.logger.info('Evironment :' + process.env.NODE_ENV);
}
}
exports.WebServer = WebServer;
//# sourceMappingURL=web-server.js.map