fix: 优化应用启动配置和Docker部署设置

- 更新Dockerfile端口配置和启动路径
  - 修改暴露端口从3003改为3000
  - 更新健康检查端口配置
  - 修正应用启动路径为dist/src/main

- 增强应用启动错误处理
  - 添加完整的try-catch错误处理机制
  - 优化启动日志输出和错误信息
  - 确保进程异常退出机制

- 完善健康检查接口类型定义
  - 修复health接口返回类型注解
  - 支持null返回值的类型安全
This commit is contained in:
imeepos 2025-09-26 15:31:29 +08:00
parent 54a1bf141b
commit 573b43f171
3 changed files with 58 additions and 48 deletions

View File

@ -47,9 +47,9 @@ COPY --from=build --chown=nestjs:nodejs /app/dist ./dist
# 切换到非root用户 # 切换到非root用户
USER nestjs USER nestjs
# 暴露端口 # 暴露端口
EXPOSE 3003 EXPOSE 3000
# 健康检查 # 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \ 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"]

View File

@ -16,7 +16,7 @@ export class AppController {
) {} ) {}
@Get('health') @Get('health')
async health(): Promise<ApiResponse<{ status: string; database: string; timestamp: string }>> { async health(): Promise<ApiResponse<{ status: string; database: string; timestamp: string } | null>> {
try { try {
// 检查数据库连接 // 检查数据库连接
await this.dataSource.query('SELECT 1'); await this.dataSource.query('SELECT 1');

View File

@ -5,6 +5,7 @@ import { setupSwagger } from './config/swagger.config';
import { GlobalExceptionFilter } from './common/filters'; import { GlobalExceptionFilter } from './common/filters';
async function bootstrap() { async function bootstrap() {
try {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
// 启用全局验证管道 // 启用全局验证管道
@ -27,9 +28,9 @@ async function bootstrap() {
console.error('❌ Swagger setup failed:', error.message); console.error('❌ Swagger setup failed:', error.message);
} }
// 设置API前缀排除docs路径) // 设置API前缀排除docs和health路径)
app.setGlobalPrefix('api/v1', { app.setGlobalPrefix('api/v1', {
exclude: ['docs', 'docs/(.*)'], exclude: ['docs', 'docs/(.*)', 'health'],
}); });
// 启用 CORS 支持多平台访问 // 启用 CORS 支持多平台访问
@ -41,15 +42,24 @@ async function bootstrap() {
}); });
const port = process.env.PORT ?? 3003; const port = process.env.PORT ?? 3003;
await app.listen(port); await app.listen(port, '0.0.0.0'); // 监听所有接口,适配云函数环境
console.log(`🚀 多平台小程序后台服务启动成功!`); console.log(`🚀 多平台小程序后台服务启动成功!`);
console.log(`📡 服务地址: http://localhost:${port}`); console.log(`📡 服务地址: http://localhost:${port}`);
console.log(`💚 健康检查地址: http://localhost:${port}/health`);
console.log(`📖 API文档地址: http://localhost:${port}/docs`); console.log(`📖 API文档地址: http://localhost:${port}/docs`);
console.log(`📋 模板管理 API: http://localhost:${port}/api/v1/templates`); console.log(`📋 模板管理 API: http://localhost:${port}/api/v1/templates`);
console.log( console.log(
`🔄 增强版 API: http://localhost:${port}/api/v1/enhanced/templates`, `🔄 增强版 API: http://localhost:${port}/api/v1/enhanced/templates`,
); );
console.log(`🎯 统一异步架构已启用!支持同步和异步审核模式`); console.log(`🎯 统一异步架构已启用!支持同步和异步审核模式`);
} catch (error) {
console.error('❌ 应用启动失败:', error);
process.exit(1);
} }
bootstrap(); }
bootstrap().catch((error) => {
console.error('❌ Bootstrap 函数执行失败:', error);
process.exit(1);
});