From 573b43f1718595bd7b3145d08fb2e9ae5023b09e Mon Sep 17 00:00:00 2001 From: imeepos Date: Fri, 26 Sep 2025 15:31:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E9=85=8D=E7=BD=AE=E5=92=8CDocker=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新Dockerfile端口配置和启动路径 - 修改暴露端口从3003改为3000 - 更新健康检查端口配置 - 修正应用启动路径为dist/src/main - 增强应用启动错误处理 - 添加完整的try-catch错误处理机制 - 优化启动日志输出和错误信息 - 确保进程异常退出机制 - 完善健康检查接口类型定义 - 修复health接口返回类型注解 - 支持null返回值的类型安全 --- Dockerfile | 6 +-- src/app.controller.ts | 2 +- src/main.ts | 98 ++++++++++++++++++++++++------------------- 3 files changed, 58 insertions(+), 48 deletions(-) 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); +});