import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { existsSync } from 'fs';
import { resolve } from 'path';
import { AppModule } from './app.module';
import { configureHttpBodyParsers } from './http-body-limit';

// 加载 .env（Node v20+ 内置 loadEnvFile）
// 优先 cwd/.env，其次 dist 上一级（编译运行时 cwd 可能在 dist/）
for (const candidate of [resolve(process.cwd(), '.env'), resolve(__dirname, '../../.env'), resolve(__dirname, '../.env')]) {
  if (existsSync(candidate) && typeof (process as any).loadEnvFile === 'function') {
    (process as any).loadEnvFile(candidate);
    break;
  }
}

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { bodyParser: false });
  configureHttpBodyParsers(app);
  app.setGlobalPrefix('api/v1');
  app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
  app.enableCors();
  await app.listen(3001);
}
bootstrap();
