add NestJS skeleton and GET /api/health-check.
Thin API bootstrap with config module and nullcart-style health route for Docker checks.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import appConfig from './config';
|
||||
import { HealthCheckModule } from './modules/healthCheck/HealthCheckModule';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
load: [appConfig]
|
||||
}),
|
||||
HealthCheckModule
|
||||
]
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { registerAs } from '@nestjs/config';
|
||||
import { AppConfig } from '../types/AppConfig';
|
||||
import { NodeEnv } from '../types/NodeEnv';
|
||||
|
||||
const env = (key: string): string => process.env[key] || '';
|
||||
|
||||
const envInt = (key: string): number => parseInt(env(key), 10);
|
||||
|
||||
export const getAppConfig = (): AppConfig => {
|
||||
const rawOrigins = env('CORS_ORIGINS');
|
||||
|
||||
return {
|
||||
port: envInt('BACKEND_PORT'),
|
||||
nodeEnv: env('NODE_ENV') as NodeEnv,
|
||||
corsOrigins: rawOrigins
|
||||
.split(',')
|
||||
.map(o => o.trim())
|
||||
.filter(Boolean)
|
||||
};
|
||||
};
|
||||
|
||||
export default registerAs('app', getAppConfig);
|
||||
@@ -0,0 +1,30 @@
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './AppModule';
|
||||
import { AppConfig } from './types/AppConfig';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
const configService = app.get(ConfigService);
|
||||
const { port, corsOrigins } = configService.get('app') as AppConfig;
|
||||
|
||||
app.setGlobalPrefix('api');
|
||||
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
transform: true,
|
||||
transformOptions: { enableImplicitConversion: true }
|
||||
})
|
||||
);
|
||||
|
||||
app.enableCors({
|
||||
origin: corsOrigins,
|
||||
credentials: true
|
||||
});
|
||||
|
||||
await app.listen(port);
|
||||
}
|
||||
|
||||
void bootstrap();
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HealthCheckController } from './controllers/HealthCheckController';
|
||||
|
||||
@Module({
|
||||
controllers: [HealthCheckController]
|
||||
})
|
||||
export class HealthCheckModule {}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Controller, Get, HttpStatus, Res } from '@nestjs/common';
|
||||
import type { Response } from 'express';
|
||||
|
||||
@Controller('/health-check')
|
||||
export class HealthCheckController {
|
||||
@Get('/')
|
||||
getStatus(@Res() res: Response) {
|
||||
return res.sendStatus(HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { NodeEnv } from './NodeEnv';
|
||||
|
||||
export type AppConfig = {
|
||||
port: number;
|
||||
nodeEnv: NodeEnv;
|
||||
corsOrigins: string[];
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
export enum NodeEnv {
|
||||
Development = 'development',
|
||||
Production = 'production'
|
||||
}
|
||||
Reference in New Issue
Block a user