UNPKG

vgnotification

Version:

Notification Sample

160 lines (131 loc) 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Bootstrap = void 0; var _util = require("util"); var _express = _interopRequireDefault(require("express")); var appConfig = _interopRequireWildcard(require("../config/application.config")); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var http = require("http"); var Bootstrap = /*#__PURE__*/ function () { function Bootstrap(config, app) { _classCallCheck(this, Bootstrap); try { this.validateConfig(config, app); this.app = null; } catch (err) { console.log(err); } } _createClass(Bootstrap, [{ key: "validateConfig", value: function validateConfig(config, app) { try { if (config.DB == undefined || config.DB == "" || (0, _util.isNull)(config.DB)) { console.log("Error in Congif Init"); return Error("Invalid Config. Please pass the correct config parameter"); } if (app == null) { this.app = (0, _express["default"])(); } else { this.app = app; } this.port = this.normalizePort(appConfig.PORT); this.server = http.createServer(this.app); this.server.listen(this.port); this.server.on("error", this.onError); this.server.on("listening", this.onListening); this.io = require("socket.io")(this.server); } catch (err) { console.log(err); return Error("Error while APP init."); } } /** * Loads and applies required Middlewares * @param {app Reference} app */ }, { key: "configureMiddlewares", value: function configureMiddlewares(app) { app.set("io", this.io); } /** * Returns Port and Server used in HTTP server setup * @param {} */ }, { key: "getHttpServerDetail", value: function getHttpServerDetail() { return { port: this.port, server: this.server }; } /** * Noemalize Port Value * @param { Port Number} val */ }, { key: "normalizePort", value: function normalizePort(val) { var port = parseInt(val, 10); if (isNaN(port)) { // named pipe return val; } if (port >= 0) { // port number return port; } console.error("Invalid Port", val); process.exit(); } /** * Handle Error on HTTP server * @param { Error object from HTTP Server Module} error */ }, { key: "onError", value: function onError(error) { if (error.syscall !== "listen") { throw error; } var bind = typeof port === "string" ? "Pipe " + port : "Port " + port; // handle specific listen errors with friendly messages switch (error.code) { case "EACCES": console.error(bind + " requires elevated privileges"); process.exit(1); break; case "EADDRINUSE": console.error(bind + " is already in use"); process.exit(1); break; default: throw error; } } /** * */ }, { key: "onListening", value: function onListening() { console.log("onListening"); var addr = this.address(); var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port; console.log(""); console.log("Notification App is listening on " + bind); console.log("Use (Ctrl-C) to shutdown the application.."); } }]); return Bootstrap; }(); exports.Bootstrap = Bootstrap;