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用户
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"]

View File

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

View File

@ -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);
});