86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
import { NestFactory } from '@nestjs/core';
|
|
import { ValidationPipe } from '@nestjs/common';
|
|
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
import { join } from 'node:path';
|
|
import { config as loadEnv } from 'dotenv';
|
|
import { AppModule } from './app.module.js';
|
|
import {
|
|
databaseConfigFromEnv,
|
|
ensureDatabaseExists,
|
|
} from './common/services/database-bootstrap.js';
|
|
import { seedFirstDeveloper } from './common/services/seed-developer.js';
|
|
|
|
async function bootstrap() {
|
|
loadEnv();
|
|
|
|
try {
|
|
await ensureDatabaseExists(databaseConfigFromEnv());
|
|
} catch (error) {
|
|
// eslint-disable-next-line no-console
|
|
console.error(
|
|
'[db] No se pudo verificar/crear la base de datos. Revisá .env (DB_*).',
|
|
error instanceof Error ? error.message : error,
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
const app = await NestFactory.create<NestExpressApplication>(AppModule);
|
|
|
|
// CORS para la web (Flutter web corre en otro origen — p.ej. localhost —
|
|
// y sin esto el navegador bloquea todos los fetch y la app muestra
|
|
// "Sin conexión con el servidor"). Mismo criterio que el gateway socket.io
|
|
// (origin '*'): la API ya se protege con JWT, no con CORS.
|
|
app.enableCors();
|
|
|
|
// Seed del primer Developer (idempotente) — requiere tablas ya sincronizadas por TypeORM
|
|
try {
|
|
await seedFirstDeveloper();
|
|
} catch (error) {
|
|
// eslint-disable-next-line no-console
|
|
console.error(
|
|
'[seed] No se pudo crear el Developer inicial.',
|
|
error instanceof Error ? error.message : error,
|
|
);
|
|
}
|
|
|
|
app.setGlobalPrefix('v1', {
|
|
exclude: ['docs', 'docs-json', 'docs/{*path}'],
|
|
});
|
|
|
|
app.useGlobalPipes(
|
|
new ValidationPipe({
|
|
whitelist: true,
|
|
forbidNonWhitelisted: true,
|
|
transform: true,
|
|
}),
|
|
);
|
|
|
|
app.useStaticAssets(join(process.cwd(), 'uploads'), {
|
|
prefix: '/uploads/',
|
|
});
|
|
|
|
const config = new DocumentBuilder()
|
|
.setTitle('min-ser API')
|
|
.setDescription('API del sistema de ministerio min-ser')
|
|
.setVersion('1.0')
|
|
.addBearerAuth()
|
|
.build();
|
|
|
|
const document = SwaggerModule.createDocument(app, config);
|
|
SwaggerModule.setup('docs', app, document, {
|
|
swaggerOptions: {
|
|
persistAuthorization: true,
|
|
},
|
|
});
|
|
|
|
const port = process.env.PORT ?? 3000;
|
|
await app.listen(port, '0.0.0.0');
|
|
// eslint-disable-next-line no-console
|
|
console.log(`min-ser API en http://localhost:${port}/docs`);
|
|
// eslint-disable-next-line no-console
|
|
console.log(
|
|
`WebSocket: ws://localhost:${port}/min-ser (auth: { token: JWT } via socket.io)`,
|
|
);
|
|
}
|
|
await bootstrap();
|