diff --git a/Dockerfile b/Dockerfile index fd1b7e3..887d716 100644 --- a/Dockerfile +++ b/Dockerfile @@ -47,9 +47,9 @@ COPY --from=build --chown=nestjs:nodejs /app/dist ./dist # 切换到非root用户 USER nestjs # 暴露端口 -EXPOSE 3003 +EXPOSE 3000 # 健康检查 HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \ - CMD wget --no-verbose --tries=1 --spider http://localhost:3003/health || exit 1 + CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1 # 启动应用 -CMD ["node", "dist/main"] +CMD ["node", "dist/src/main"] diff --git a/src/app.controller.ts b/src/app.controller.ts index 18ca8f9..f10a7ad 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -16,7 +16,7 @@ export class AppController { ) {} @Get('health') - async health(): Promise> { + async health(): Promise> { try { // 检查数据库连接 await this.dataSource.query('SELECT 1'); diff --git a/src/main.ts b/src/main.ts index 3a9f537..5c0a41b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,51 +5,61 @@ import { setupSwagger } from './config/swagger.config'; import { GlobalExceptionFilter } from './common/filters'; async function bootstrap() { - const app = await NestFactory.create(AppModule); - - // 启用全局验证管道 - app.useGlobalPipes( - new ValidationPipe({ - whitelist: true, - forbidNonWhitelisted: true, - transform: true, - }), - ); - - // 启用全局异常过滤器 - app.useGlobalFilters(new GlobalExceptionFilter()); - - // 设置Swagger文档(在全局前缀之前) try { - setupSwagger(app); - console.log('✅ Swagger setup completed successfully'); + const app = await NestFactory.create(AppModule); + + // 启用全局验证管道 + app.useGlobalPipes( + new ValidationPipe({ + whitelist: true, + forbidNonWhitelisted: true, + transform: true, + }), + ); + + // 启用全局异常过滤器 + app.useGlobalFilters(new GlobalExceptionFilter()); + + // 设置Swagger文档(在全局前缀之前) + try { + setupSwagger(app); + console.log('✅ Swagger setup completed successfully'); + } catch (error) { + console.error('❌ Swagger setup failed:', error.message); + } + + // 设置API前缀(排除docs和health路径) + app.setGlobalPrefix('api/v1', { + exclude: ['docs', 'docs/(.*)', 'health'], + }); + + // 启用 CORS 支持多平台访问 + app.enableCors({ + origin: true, + methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], + allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'], + credentials: true, + }); + + const port = process.env.PORT ?? 3003; + await app.listen(port, '0.0.0.0'); // 监听所有接口,适配云函数环境 + + console.log(`🚀 多平台小程序后台服务启动成功!`); + console.log(`📡 服务地址: http://localhost:${port}`); + console.log(`💚 健康检查地址: http://localhost:${port}/health`); + console.log(`📖 API文档地址: http://localhost:${port}/docs`); + console.log(`📋 模板管理 API: http://localhost:${port}/api/v1/templates`); + console.log( + `🔄 增强版 API: http://localhost:${port}/api/v1/enhanced/templates`, + ); + console.log(`🎯 统一异步架构已启用!支持同步和异步审核模式`); } catch (error) { - console.error('❌ Swagger setup failed:', error.message); + console.error('❌ 应用启动失败:', error); + process.exit(1); } - - // 设置API前缀(排除docs路径) - app.setGlobalPrefix('api/v1', { - exclude: ['docs', 'docs/(.*)'], - }); - - // 启用 CORS 支持多平台访问 - app.enableCors({ - origin: true, - methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], - allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'], - credentials: true, - }); - - const port = process.env.PORT ?? 3003; - await app.listen(port); - - console.log(`🚀 多平台小程序后台服务启动成功!`); - console.log(`📡 服务地址: http://localhost:${port}`); - console.log(`📖 API文档地址: http://localhost:${port}/docs`); - console.log(`📋 模板管理 API: http://localhost:${port}/api/v1/templates`); - console.log( - `🔄 增强版 API: http://localhost:${port}/api/v1/enhanced/templates`, - ); - console.log(`🎯 统一异步架构已启用!支持同步和异步审核模式`); } -bootstrap(); + +bootstrap().catch((error) => { + console.error('❌ Bootstrap 函数执行失败:', error); + process.exit(1); +});