UNPKG

@jrosadob/nestjs-sophia-framework

Version:

Franework para microservicios en NestJS

111 lines 5.61 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.bootstrap = bootstrap; const core_1 = require("@nestjs/core"); const common_1 = require("@nestjs/common"); const logger_1 = require("./core/logger/logger"); const config_service_1 = require("./core/config/config.service"); const swagger_1 = require("@nestjs/swagger"); const yaml = __importStar(require("js-yaml")); const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const interceptors_1 = require("./core/interceptors"); const common_2 = require("./core/common"); const isMain = require.main === module; async function bootstrap() { var _a, _b, _c, _d, _e; let appModule; const basePath = isMain ? '.' : './../../../dist'; try { const configData = yaml.load((0, fs_1.readFileSync)('bootstrap.yml', 'utf8')); const configService = new config_service_1.ConfigService(configData); const logstashUrl = (_a = configService.get('logger.http.host')) !== null && _a !== void 0 ? _a : ''; const logger = new logger_1.Logger(configService); logger.setContext('Bootstrap'); common_1.Logger.overrideLogger(logger); appModule = basePath + ((_b = configService.get('app.module')) !== null && _b !== void 0 ? _b : '/app.module'); const appModulePath = path_1.default.resolve(__dirname, appModule); const { AppModule } = await Promise.resolve(`${appModulePath}`).then(s => __importStar(require(s))); const app = await core_1.NestFactory.create(AppModule); logger_1.Logger.setApplicationContext(app); logger_1.Logger.setLogstashUrl(logstashUrl); logger.setContext('Bootstrap'); logger_1.Logger.log(`Loading app module from ${appModulePath}...`); app.useLogger(logger); const appPrefix = (_c = configService.get('app.prefix')) !== null && _c !== void 0 ? _c : 'api'; app.setGlobalPrefix(appPrefix !== null && appPrefix !== void 0 ? appPrefix : 'api'); app.useGlobalInterceptors(new interceptors_1.HeadersInterceptor()); app.useGlobalInterceptors(new interceptors_1.LoggingInterceptor(configService)); app.useGlobalPipes(new common_2.AppValidationPipe()); if (configService.get('swagger.active') === true) { addSwagger(app, configService); } const port = (_d = configService.get('app.port')) !== null && _d !== void 0 ? _d : 3000; await app.listen(port); const appUrl = (await app.getUrl()).replace('[::1]', 'localhost'); const swaggerPrefix = (_e = configService.get('swagger.prefix')) !== null && _e !== void 0 ? _e : '/swagger'; logger_1.Logger.log(`Application is running on: ${appUrl}/${appPrefix}`, 'Bootstrap'); logger.log(`Swagger is running on: ${appUrl}/${swaggerPrefix}`, 'Bootstrap'); } catch (error) { if (error.code === 'MODULE_NOT_FOUND') { logger_1.Logger.error(`Error in the bootstrap method. Cannot find module ${appModule}\nStack trace: ${error.stack}`); } else { logger_1.Logger.error(`Error in the bootstrap method. Terminating the application.\nStack trace: ${error.stack}`); } process.exit(1); } } bootstrap(); function addSwagger(app, configService) { var _a, _b, _c, _d; const config = new swagger_1.DocumentBuilder() .setTitle((_a = configService.get('swagger.title')) !== null && _a !== void 0 ? _a : 'Undefined title API') .setDescription((_b = configService.get('swagger.description')) !== null && _b !== void 0 ? _b : 'Undefined description API') .setVersion((_c = configService.get('swagger.version')) !== null && _c !== void 0 ? _c : 'Undefined version API') .build(); const documentFactory = () => swagger_1.SwaggerModule.createDocument(app, config); swagger_1.SwaggerModule.setup((_d = configService.get('swagger.basePath')) !== null && _d !== void 0 ? _d : '/swagger', app, documentFactory, { swaggerOptions: { tagsSorter: 'alpha', }, }); } //# sourceMappingURL=main.js.map