Skip to content

Commit

Permalink
Swagger and simple configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfrac committed Feb 3, 2023
1 parent a9bcda9 commit ad991c0
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ lerna-debug.log*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/extensions.json

/configuration.ts
6 changes: 4 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"singleQuote": true,
"trailingComma": "all"
}
"trailingComma": "all",
"printWidth": 120,
"useTabs": false
}
19 changes: 19 additions & 0 deletions configuration.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface IAppConfig {
port: number;
apiPrefix: string;
useOpenApi: boolean;
dbHost: string;
dbPort: number;
dbUsername: string;
dbPassword: string;
dbName: string;
}

export default (): Partial<IAppConfig> => ({
port: 4100,
apiPrefix: '/api',
useOpenApi: true,
dbHost: 'localhost',
dbPort: 5432,
dbName: 'cat-cafe',
});
147 changes: 137 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
},
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.3.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/swagger": "^6.1.4",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0"
},
Expand Down
8 changes: 7 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import configuration from '../configuration';

@Module({
imports: [],
imports: [
ConfigModule.forRoot({
load: [configuration],
}),
],
controllers: [AppController],
providers: [AppService],
})
Expand Down
23 changes: 22 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import * as packageInfo from '../package.json';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);

const configService = app.get(ConfigService);
const apiPrefix = configService.get<string>('apiPrefix', 'api');
const useOpenApi = configService.get<boolean>('useOpenApi', false);

if (useOpenApi) {
const options = new DocumentBuilder()
.setTitle(packageInfo.name)
.setDescription(`${packageInfo.name} API`)
.setVersion(packageInfo.version)
.build();

const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup(`${apiPrefix}/openapi`, app, document);
}

app.setGlobalPrefix(apiPrefix);
await app.listen(configService.get<number>('port') || 3000);
}

bootstrap();
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
"noFallthroughCasesInSwitch": false,
"resolveJsonModule": true
}
}

0 comments on commit ad991c0

Please sign in to comment.