Files
cc-cloud/backend/src/controllers/instance.controller.ts
T
hughhuangandClaude Sonnet 4.5 ed626e74dc 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>
2026-01-02 22:57:33 +08:00

117 lines
3.3 KiB
TypeScript

import { Request, Response } from 'express';
import { z } from 'zod';
import {
createInstance,
startInstance,
stopInstance,
removeInstance,
getAllInstances,
getInstance,
} from '../services/instance.service';
import logger from '../utils/logger';
// 验证 schema
const createInstanceSchema = z.object({
body: z.object({
name: z.string().min(1, '实例名称不能为空'),
config: z.object({
model: z.string().min(1, '模型不能为空'),
apiKey: z.string().optional(),
workingDir: z.string().min(1, '工作目录不能为空'),
port: z.number().int().min(1).max(65535).optional(),
maxMemory: z.number().int().positive().optional(),
env: z.record(z.string()).optional(),
}),
}),
});
export async function createInstanceHandler(req: Request, res: Response) {
try {
const instance = await createInstance(req.body);
res.status(201).json({ instance });
} catch (error: any) {
logger.error('创建实例失败', error);
res.status(400).json({ error: error.message });
}
}
export async function startInstanceHandler(req: Request, res: Response) {
try {
const { instanceId } = req.params;
await startInstance(instanceId);
res.json({ success: true, message: '实例启动成功' });
} catch (error: any) {
logger.error('启动实例失败', error);
res.status(400).json({ error: error.message });
}
}
export async function stopInstanceHandler(req: Request, res: Response) {
try {
const { instanceId } = req.params;
await stopInstance(instanceId);
res.json({ success: true, message: '实例停止成功' });
} catch (error: any) {
logger.error('停止实例失败', error);
res.status(400).json({ error: error.message });
}
}
export async function deleteInstanceHandler(req: Request, res: Response) {
try {
const { instanceId } = req.params;
await removeInstance(instanceId);
res.json({ success: true, message: '实例删除成功' });
} catch (error: any) {
logger.error('删除实例失败', error);
res.status(400).json({ error: error.message });
}
}
export async function listInstancesHandler(req: Request, res: Response) {
try {
const instances = await getAllInstances();
res.json({ instances });
} catch (error: any) {
logger.error('获取实例列表失败', error);
res.status(500).json({ error: '获取实例列表失败' });
}
}
export async function getInstanceHandler(req: Request, res: Response) {
try {
const { instanceId } = req.params;
const instance = await getInstance(instanceId);
if (!instance) {
return res.status(404).json({ error: '实例不存在' });
}
res.json({ instance });
} catch (error: any) {
logger.error('获取实例失败', error);
res.status(500).json({ error: '获取实例失败' });
}
}
export async function getInstanceStatusHandler(req: Request, res: Response) {
try {
const { instanceId } = req.params;
const instance = await getInstance(instanceId);
if (!instance) {
return res.status(404).json({ error: '实例不存在' });
}
res.json({
status: instance.status,
stats: instance.stats,
});
} catch (error: any) {
logger.error('获取实例状态失败', error);
res.status(500).json({ error: '获取实例状态失败' });
}
}
export { createInstanceSchema };