UNPKG

@mvx/mvc

Version:

@mvx/mvc is mvc framework on server, base on koa, @tsdi frameworks

103 lines (101 loc) 3.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MvcServer = void 0; var tslib_1 = require("tslib"); var ioc_1 = require("@tsdi/ioc"); var boot_1 = require("@tsdi/boot"); var Router_1 = require("./router/Router"); var https = require("https"); /** * base mvc server. * * @export * @abstract * @class MvcServer * @implements {IMvcServer} */ var MvcServer = /** @class */ (function (_super) { tslib_1.__extends(MvcServer, _super); function MvcServer() { return _super !== null && _super.apply(this, arguments) || this; } /** * configure startup service. * * @param {IBootContext} [ctx] * @returns {(Promise<void>)} * @memberof IStartup */ MvcServer.prototype.configureService = function (ctx) { return tslib_1.__awaiter(this, void 0, void 0, function () { var config; return tslib_1.__generator(this, function (_a) { this.context = ctx; config = ctx.getConfiguration(); this.logger = ctx.getLogManager().getLogger(); this.port = config.port || parseInt(process.env.PORT || '0'); this.hostname = config.hostname; this.uri = (ctx.httpServer instanceof https.Server ? 'https' : 'http') + "://" + (this.hostname || '127.0.0.1') + ":" + this.port; return [2 /*return*/]; }); }); }; MvcServer.prototype.getContext = function () { return this.context; }; MvcServer.prototype.getConfig = function () { return this.context.getConfiguration(); }; MvcServer.prototype.getRouter = function () { return this.router; }; MvcServer.prototype.getHttpServer = function () { return this.getContext().httpServer; }; MvcServer.prototype.start = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var ctx, listener, server; return tslib_1.__generator(this, function (_a) { ctx = this.getContext(); listener = ctx.listener; server = this.getHttpServer(); if (this.hostname) { server.listen(this.port, this.hostname, listener); } else { server.listen(this.port, listener); } this.logger.info('mvc service start: ', this.uri); return [2 /*return*/]; }); }); }; MvcServer.prototype.stop = function () { return tslib_1.__awaiter(this, void 0, void 0, function () { var httpServer; return tslib_1.__generator(this, function (_a) { httpServer = this.getHttpServer(); if (httpServer) { this.logger.info('close mvc http(s) service'); httpServer.removeAllListeners(); httpServer.close(); } return [2 /*return*/]; }); }); }; MvcServer.ρAnn = function () { return { "name": "MvcServer", "params": { "configureService": ["ctx"] } }; }; tslib_1.__decorate([ ioc_1.Inject(), tslib_1.__metadata("design:type", Router_1.Router) ], MvcServer.prototype, "router", void 0); MvcServer = tslib_1.__decorate([ ioc_1.Injectable(), ioc_1.Refs('@MvcModule', boot_1.Startup) ], MvcServer); return MvcServer; }(boot_1.Service)); exports.MvcServer = MvcServer; //# sourceMappingURL=sourcemaps/MvcServer.js.map