Build MerchantFlow onboarding CRM foundation
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import 'reflect-metadata';
|
||||
import {
|
||||
Module,
|
||||
Controller,
|
||||
Get,
|
||||
Inject,
|
||||
Injectable,
|
||||
type OnApplicationShutdown,
|
||||
} from '@nestjs/common';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import { createDatabase, PrismaClient } from '@merchantflow/database';
|
||||
import { AuthController, AuthService, SessionGuard } from './auth.js';
|
||||
import { CrmController } from './crm.js';
|
||||
import { DATABASE, ApiError, ErrorFilter } from './shared.js';
|
||||
import { env } from './env.js';
|
||||
@Controller('health')
|
||||
class HealthController {
|
||||
constructor(@Inject(DATABASE) private readonly db: PrismaClient) {}
|
||||
@Get() async health() {
|
||||
try {
|
||||
await this.db.$queryRaw`SELECT 1`;
|
||||
return { status: 'ok', database: 'connected' };
|
||||
} catch {
|
||||
throw new ApiError('DATABASE_UNAVAILABLE', 503);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Injectable()
|
||||
class DatabaseLifecycle implements OnApplicationShutdown {
|
||||
constructor(@Inject(DATABASE) private readonly db: PrismaClient) {}
|
||||
async onApplicationShutdown() {
|
||||
await this.db.$disconnect();
|
||||
}
|
||||
}
|
||||
@Module({
|
||||
controllers: [HealthController, AuthController, CrmController],
|
||||
providers: [
|
||||
{ provide: DATABASE, useFactory: () => createDatabase(env.DATABASE_URL) },
|
||||
DatabaseLifecycle,
|
||||
AuthService,
|
||||
SessionGuard,
|
||||
],
|
||||
})
|
||||
class AppModule {}
|
||||
export async function createApp() {
|
||||
const app = await NestFactory.create(AppModule, { logger: ['error', 'warn', 'log'] });
|
||||
app.setGlobalPrefix('api');
|
||||
app.use(cookieParser());
|
||||
app.use(
|
||||
(_req: unknown, res: { setHeader: (key: string, value: string) => void }, next: () => void) => {
|
||||
res.setHeader('Cache-Control', 'no-store');
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff');
|
||||
next();
|
||||
},
|
||||
);
|
||||
app.useGlobalFilters(new ErrorFilter());
|
||||
app.enableShutdownHooks();
|
||||
return app;
|
||||
}
|
||||
Reference in New Issue
Block a user