UNPKG

radius-read

Version:

Realtime Dashboard

211 lines 9.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExpressService = void 0; const tslib_1 = require("tslib"); const body_parser_1 = tslib_1.__importDefault(require("body-parser")); const cors_1 = tslib_1.__importDefault(require("cors")); const dns = tslib_1.__importStar(require("dns")); const express_1 = tslib_1.__importDefault(require("express")); const useragent = tslib_1.__importStar(require("express-useragent")); const fs = tslib_1.__importStar(require("fs")); const http = tslib_1.__importStar(require("http")); const https = tslib_1.__importStar(require("https")); const path = tslib_1.__importStar(require("path")); const serve_index_1 = tslib_1.__importDefault(require("serve-index")); const result_model_1 = require("./../models/result.model"); const webapp_service_1 = require("./webapp.service"); const http_enum_1 = require("./../enums/http.enum"); /** * ExpressService is a service class that initializes and manages an Express application. */ class ExpressService { /** * Creates an instance of ExpressService. * @param appConfig * @param webAppConfigs */ constructor(appConfig, logger) { this.logger = logger; this.appConfig = appConfig; } /** * Initializes the Express application with the provided API routers and starts the server. * @param apiRouters * @returns */ init() { this.app = this.initMiddleware(); this._router = this.getRouter(); this.serveLogFiles(); } /** * Initializes the Express application with the provided API routers and starts the server. * @param apiRouters */ initApiRouters(apiRouters) { this.initAPIs(apiRouters); this.initRouter(); } /** * Initializes web application services with the provided web application configurations. * @param webAppConfigs */ intiWebAppServices(webAppConfigs) { var _a; this._webAppConfigs = webAppConfigs; (_a = this._webAppConfigs) === null || _a === void 0 ? void 0 : _a.forEach((webAppConfig) => { var _a; var webAppService = new webapp_service_1.WebAppService(webAppConfig); webAppService.init(this.app, this.logger); (_a = this.webAppServices) === null || _a === void 0 ? void 0 : _a.push(webAppService); }); } /** * Initializes the middleware for the Express application. * @returns */ initMiddleware() { var _a, _b, _c, _d; (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug("ExpressService >>> Initializing middleware..."); var app = (0, express_1.default)(); (_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug("ExpressService >>> Initializing middleware..."); app.use(useragent.express()); (_c = this.logger) === null || _c === void 0 ? void 0 : _c.debug("ExpressService >>> Initializing CORS and body parser..."); app.use((0, cors_1.default)()); (_d = this.logger) === null || _d === void 0 ? void 0 : _d.debug("ExpressService >>> Initializing body parser..."); app.use(body_parser_1.default.json()); return app; } /** * Serves log files from the specified directory. * @param app */ serveLogFiles() { var _a; (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug("ExpressService >>> Serving log files...", `/${this.appConfig.name}/logs`); this.app.use(`/${this.appConfig.name}/logs`, express_1.default.static(path.resolve(process.cwd(), "logs")), (0, serve_index_1.default)(path.resolve(process.cwd(), "logs"), { 'icons': true })); } /** * Initializes the router for the Express application. * @param app * @param router */ initRouter() { var _a; (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug("ExpressService >>> Initializing router...", this.appConfig.name); this.app.use(`/${this.appConfig.name}`, this._router); } /** * Gets the SSL certificate for the application. * @returns */ getCertificate() { var _a, _b; var privateKey = fs.readFileSync(this.appConfig.certificate.keyFilePath, "utf8"); var certificate = fs.readFileSync(this.appConfig.certificate.crtFilePath, "utf8"); (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug("ExpressService >>> SSL certificate loaded successfully.", this.appConfig.certificate.keyFilePath, this.appConfig.certificate.crtFilePath); (_b = this.logger) === null || _b === void 0 ? void 0 : _b.debug("ExpressService >>> Private key and certificate loaded successfully.", privateKey, certificate); return { key: privateKey, cert: certificate }; } /** * Starts the Express application server with the provided application and certificate. * @param app * @param certificate * @returns */ start() { return tslib_1.__awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; var certificate = this.getCertificate(); var instance; if (this.appConfig.isSSL && certificate) { (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug("ExpressService >>> Starting HTTPS server..."); instance = https.createServer(certificate, this.app); var result = yield this.startServer(instance); if (result.ResultType === result_model_1.EntityResultType.Success) { (_b = this.logger) === null || _b === void 0 ? void 0 : _b.info(`ExpressService >>> ${result.Response}`); } else { (_c = this.logger) === null || _c === void 0 ? void 0 : _c.error(`ExpressService >>> Failed to start server: ${result.Response}`); } } else { (_d = this.logger) === null || _d === void 0 ? void 0 : _d.debug("ExpressService >>> Starting HTTP server..."); instance = http.createServer(this.app); var result = yield this.startServer(instance); if (result.ResultType === result_model_1.EntityResultType.Success) { (_e = this.logger) === null || _e === void 0 ? void 0 : _e.info(`ExpressService >>> ${result.Response}`); } else { (_f = this.logger) === null || _f === void 0 ? void 0 : _f.error(`ExpressService >>> Failed to start server: ${result.Response}`); } } return instance; }); } /** * Starts the server and listens on the specified port. * @param server * @returns */ startServer(server) { return new Promise((resolve) => { var result = new result_model_1.EntityResult(); try { dns.lookup(require("os").hostname(), (err, address, fam) => { server.listen(this.appConfig.port, '0.0.0.0', (e) => { result.ResultType = result_model_1.EntityResultType.Success; result.Response = `App listening at *:${this.appConfig.port} [${this.appConfig.isSSL === true ? 'HTTPS' : 'HTTP'}]`; resolve(result); }); }); } catch (ex) { result.ResultType = result_model_1.EntityResultType.Failed; result.Response = ex; resolve(result); } }); } /** * Initializes API routes with the provided ApiRouter instances. * @param apiRouters */ initAPIs(apiRouters) { apiRouters.forEach((apiRouter) => { var _a, _b, _c; (_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(`ExpressService >>> Initializing API: ${apiRouter.method} ${apiRouter.path}`); if (!this._router) return (_b = this.logger) === null || _b === void 0 ? void 0 : _b.error("ExpressService >>> Failed to get router instance"); switch (apiRouter.method) { case http_enum_1.HTTP_METHOD.GET: this._router.get(apiRouter.path, apiRouter.callback); break; case http_enum_1.HTTP_METHOD.DELETE: this._router.delete(apiRouter.path, apiRouter.callback); break; case http_enum_1.HTTP_METHOD.PATCH: this._router.patch(apiRouter.path, apiRouter.callback); break; case http_enum_1.HTTP_METHOD.POST: this._router.post(apiRouter.path, apiRouter.callback); break; case http_enum_1.HTTP_METHOD.PUT: this._router.put(apiRouter.path, apiRouter.callback); break; default: (_c = this.logger) === null || _c === void 0 ? void 0 : _c.warn(`ExpressService >>> Unsupported method: ${apiRouter.method}`); break; } }); } /** * Gets a new Express router instance. * @returns */ getRouter() { return express_1.default.Router(); } } exports.ExpressService = ExpressService; //# sourceMappingURL=express.service.js.map