diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index 7837c2a..515fec7 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -18,7 +18,7 @@ jobs: # this assumes that you have created a personal access token # (PAT) and configured it as a GitHub action secret named # `MY_RELEASE_PLEASE_TOKEN` (this secret name is not important). - token: ${{ secrets.REALEASE_TOKEN }} + token: ${{ secrets.RELEASE_PLEASE_TOKEN }} # this is a built-in strategy in release-please, see "Action Inputs" # for more options release-type: node diff --git a/src/main.ts b/src/main.ts index abad4c7..2f79896 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,107 +1,53 @@ -/** - * This is not a production server yet! - * This is only a minimal backend to get started. - */ - -import { - BadRequestException, - INestApplication, - Logger, - ValidationPipe, -} from '@nestjs/common'; -import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; -import { NestFactory, Reflector } from '@nestjs/core'; +import { INestApplication, Logger } from '@nestjs/common'; +import { Reflector } from '@nestjs/core'; +import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; -import { ErrorFilter } from './common/filters/error.filter'; import { AppConfigService } from './config/config.service'; - -process.env.TZ = 'UTC'; +import { ErrorFilter } from './common/filters/error.filter'; +import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; async function bootstrap() { - let app: INestApplication; - if (process.env.node_env == 'production') { - app = await NestFactory.create(AppModule, { - cors: true, - }); - } else { - // repl for debugging! - app = await NestFactory.create(AppModule, { - cors: true, - }); - } - const globalPrefix = 'api'; - app.setGlobalPrefix(globalPrefix); - app.enableVersioning(); - app.useGlobalFilters(new ErrorFilter()); - app.useGlobalPipes( - new ValidationPipe({ - whitelist: true, - exceptionFactory: (errors) => { - const flatMessages: { p: string; msg: string }[] = [].concat( - errors.map((e) => ({ - p: e.property, - msg: generateErrorMessage(e), - })), - ); - return new BadRequestException({ - message: 'Bad Request', - displayMessage: flatMessages.map((fm) => `${fm.msg}`).join('
'), - }); - }, - }), - ); + const app = await NestFactory.create(AppModule, { + cors: true, + logger: ['log', 'error', 'warn', 'debug', 'verbose'], + }); - const appConfig = app.get(AppConfigService); + const appConfig: AppConfigService = app.get(AppConfigService); + app.setGlobalPrefix('api/v1'); + app.useGlobalFilters(new ErrorFilter()); app.useGlobalInterceptors(app.get(Reflector)); bootstrapSwagger(app, appConfig); + await app.startAllMicroservices(); - const port = appConfig.appPort || 3000; - await app.listen(port); + await app.listen(appConfig.appPort); Logger.log( - `🚀 Application is running on: http://localhost:${port}/${globalPrefix}`, + `🚀 Energy-householding server running on http://localhost:${appConfig.appPort}/api/v1 🚀`, ); } -function generateErrorMessage(e): string { - let ret = ''; - ret += e.constraints ? Object.values(e.constraints).join('
') : ''; - if (e.children) { - (e.children as any[]).forEach((child, i) => { - if (e.constraints || i > 0) ret += '
'; - ret += generateErrorMessage(child); - }); - } - return ret; -} - async function bootstrapSwagger( app: INestApplication, appConfig: AppConfigService, ) { - const port = appConfig.appPort || 3000; - const url: string = `localhost:${port}`; const config = new DocumentBuilder() - .setTitle('Modbus Api') - .setDescription('The Modbus Reader REST Api description') + .setTitle('energy-householder API') + .setDescription('BingusBoingus REST API Documentation') .setVersion('1.0') .setBasePath('api/v1') - .addBearerAuth() - .addServer(url, 'Default Ingress') .build(); const document = SwaggerModule.createDocument(app, config, { - ignoreGlobalPrefix: true, + ignoreGlobalPrefix: false, }); SwaggerModule.setup('api/v1/doc', app, document, { - swaggerOptions: { - persistAuthorization: true, - urls: [url], - }, customSiteTitle: 'API Docs', explorer: true, } as unknown as any); + Logger.log( + `🚀 Swagger API Documentation is available at http://localhost:${appConfig.appPort}/api/v1/doc 🚀`, + ); } bootstrap();