实现灵活的进程管理模式,支持开发和生产环境切换: ## 新增功能 - 创建开发模式进程管理服务(dev-process.service.ts) - 使用 Node.js child_process 直接管理 Claude Code 进程 - 添加 USE_PM2 环境变量控制进程管理模式 ## 后端更改 - 新增 dev-process.service.ts:开发模式进程管理 - 更新 instance.service.ts:根据 USE_PM2 选择进程管理方式 - 更新 config:添加 usePM2 配置项 - 修复 TypeScript 类型错误 ## 配置更新 - backend/.env:设置 USE_PM2=false(开发模式) - .env.example:添加 USE_PM2 配置说明 ## 模式对比 **开发模式**(USE_PM2=false): - 使用 child_process 管理进程 - 不需要安装 PM2 - 简化的监控功能 - 适合本地开发 **生产模式**(USE_PM2=true): - 使用 PM2 管理进程 - 完整的监控和自动重启 - 需要安装 PM2 - 适合生产部署 ## 文档更新 - README.md:添加开发/生产模式对比说明 - 更新环境变量表格 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
64 lines
1.5 KiB
TypeScript
64 lines
1.5 KiB
TypeScript
import { Request, Response, NextFunction } from 'express';
|
|
import jwt from 'jsonwebtoken';
|
|
import { config } from '../config';
|
|
import type { SessionPayload } from '../types';
|
|
import logger from '../utils/logger';
|
|
|
|
// 扩展 Express Request 类型
|
|
declare global {
|
|
namespace Express {
|
|
interface Request {
|
|
session?: SessionPayload;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function authMiddleware(req: Request, res: Response, next: NextFunction): void {
|
|
const authHeader = req.headers.authorization;
|
|
|
|
if (!authHeader) {
|
|
res.status(401).json({ error: '未提供认证令牌' });
|
|
return;
|
|
}
|
|
|
|
const token = authHeader.replace('Bearer ', '');
|
|
|
|
try {
|
|
const decoded = jwt.verify(token, config.accessPassword) as SessionPayload;
|
|
|
|
if (!decoded.authenticated) {
|
|
res.status(401).json({ error: '无效的认证令牌' });
|
|
return;
|
|
}
|
|
|
|
req.session = decoded;
|
|
next();
|
|
} catch (error: any) {
|
|
logger.error('Token 验证失败', error);
|
|
res.status(401).json({ error: '无效的认证令牌' });
|
|
}
|
|
}
|
|
|
|
// 可选认证(不强制要求登录)
|
|
export function optionalAuth(req: Request, _res: Response, next: NextFunction): void {
|
|
const authHeader = req.headers.authorization;
|
|
|
|
if (!authHeader) {
|
|
next();
|
|
return;
|
|
}
|
|
|
|
const token = authHeader.replace('Bearer ', '');
|
|
|
|
try {
|
|
const decoded = jwt.verify(token, config.accessPassword) as SessionPayload;
|
|
if (decoded.authenticated) {
|
|
req.session = decoded;
|
|
}
|
|
} catch (error) {
|
|
// 忽略错误,继续处理请求
|
|
}
|
|
|
|
next();
|
|
}
|