feat: 初始化 Claude Code 管理平台项目
实现了基于 Web 的 Claude Code 实例管理功能: ## 功能特性 - 🔐 用户认证:JWT 登录验证 - 🚀 实例管理:创建、启动、停止、删除 Claude Code 实例 - 📊 实时监控:CPU、内存、运行时间统计 - 📝 日志管理:Winston 日志系统 - 🔌 PM2 集成:稳定的进程管理 - 🌐 Web 界面:Vue 3 + Element Plus ## 技术栈 - 后端:Node.js + Express + TypeScript + PM2 - 前端:Vue 3 + TypeScript + Vite + Element Plus - 认证:JWT - 存储:文件系统(JSON) ## 项目结构 - Monorepo 结构,前后端分离 - 完整的类型定义 - 模块化设计 - 详细的开发文档 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
commit
ed626e74dc
46 files changed
+11882
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { config } from '../config';
|
||||
import type { JwtPayload } from '../types';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
// 扩展 Express Request 类型
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface Request {
|
||||
user?: JwtPayload;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function authMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||
const authHeader = req.headers.authorization;
|
||||
|
||||
if (!authHeader) {
|
||||
return res.status(401).json({ error: '未提供认证令牌' });
|
||||
}
|
||||
|
||||
const token = authHeader.replace('Bearer ', '');
|
||||
|
||||
try {
|
||||
const decoded = jwt.verify(token, config.jwtSecret) as JwtPayload;
|
||||
req.user = decoded;
|
||||
next();
|
||||
} catch (error: any) {
|
||||
logger.error('Token 验证失败', error);
|
||||
return res.status(401).json({ error: '无效的认证令牌' });
|
||||
}
|
||||
}
|
||||
|
||||
// 可选认证(不强制要求登录)
|
||||
export function optionalAuth(req: Request, res: Response, next: NextFunction) {
|
||||
const authHeader = req.headers.authorization;
|
||||
|
||||
if (!authHeader) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const token = authHeader.replace('Bearer ', '');
|
||||
|
||||
try {
|
||||
const decoded = jwt.verify(token, config.jwtSecret) as JwtPayload;
|
||||
req.user = decoded;
|
||||
} catch (error) {
|
||||
// 忽略错误,继续处理请求
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
export function errorMiddleware(
|
||||
error: Error,
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
logger.error('请求处理错误', {
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
path: req.path,
|
||||
method: req.method,
|
||||
});
|
||||
|
||||
res.status(500).json({
|
||||
error: '服务器内部错误',
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
|
||||
export function notFoundMiddleware(req: Request, res: Response) {
|
||||
res.status(404).json({
|
||||
error: '未找到',
|
||||
message: `路径 ${req.path} 不存在`,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { AnyZodObject, ZodError } from 'zod';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
export function validate(schema: AnyZodObject) {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
await schema.parseAsync({
|
||||
body: req.body,
|
||||
query: req.query,
|
||||
params: req.params,
|
||||
});
|
||||
next();
|
||||
} catch (error) {
|
||||
if (error instanceof ZodError) {
|
||||
logger.error('验证失败', { errors: error.errors });
|
||||
return res.status(400).json({
|
||||
error: '请求数据验证失败',
|
||||
details: error.errors,
|
||||
});
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user