|
| 1 | +/** |
| 2 | + * Admin Controller - 后台管理界面控制器 |
| 3 | + * 提供 admin 面板所需的 API 接口和静态文件服务 |
| 4 | + */ |
| 5 | + |
| 6 | +import path from 'path'; |
| 7 | +import fs from 'fs'; |
| 8 | +import fastifyStatic from '@fastify/static'; |
| 9 | + |
| 10 | +// 配置相关 |
| 11 | +const CONFIG_PATH = path.join(process.cwd(), 'config/env.json'); |
| 12 | + |
| 13 | +// 获取配置 |
| 14 | +async function getConfig(req, reply) { |
| 15 | + try { |
| 16 | + const { key } = req.query; |
| 17 | + |
| 18 | + if (!fs.existsSync(CONFIG_PATH)) { |
| 19 | + return reply.send({}); |
| 20 | + } |
| 21 | + |
| 22 | + const configContent = fs.readFileSync(CONFIG_PATH, 'utf-8'); |
| 23 | + const config = JSON.parse(configContent); |
| 24 | + |
| 25 | + if (key) { |
| 26 | + const keys = key.split('.'); |
| 27 | + let value = config; |
| 28 | + for (const k of keys) { |
| 29 | + value = value?.[k]; |
| 30 | + } |
| 31 | + return reply.send(value !== undefined ? value : null); |
| 32 | + } |
| 33 | + |
| 34 | + return reply.send(config); |
| 35 | + } catch (e) { |
| 36 | + reply.code(500).send({ error: e.message }); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// 更新配置 |
| 41 | +async function updateConfig(req, reply) { |
| 42 | + try { |
| 43 | + const { action, key, value } = req.body; |
| 44 | + |
| 45 | + if (action === 'set') { |
| 46 | + const systemTools = await import('../drpy-node-mcp/tools/systemTools.js'); |
| 47 | + const result = await systemTools.manage_config({ action, key, value: String(value) }); |
| 48 | + if (result.isError) { |
| 49 | + return reply.code(400).send({ error: result.content[0].text }); |
| 50 | + } |
| 51 | + return reply.send({ success: true, message: result.content[0].text }); |
| 52 | + } |
| 53 | + |
| 54 | + reply.code(400).send({ error: 'Invalid action' }); |
| 55 | + } catch (e) { |
| 56 | + reply.code(500).send({ error: e.message }); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// MCP 工具调用接口 |
| 61 | +async function callMCP(req, reply) { |
| 62 | + try { |
| 63 | + const { name, arguments: args } = req.body; |
| 64 | + |
| 65 | + let handler; |
| 66 | + switch (name) { |
| 67 | + case 'read_logs': |
| 68 | + const systemTools = await import('../drpy-node-mcp/tools/systemTools.js'); |
| 69 | + handler = systemTools.read_logs; |
| 70 | + break; |
| 71 | + case 'restart_service': |
| 72 | + const systemTools2 = await import('../drpy-node-mcp/tools/systemTools.js'); |
| 73 | + handler = systemTools2.restart_service; |
| 74 | + break; |
| 75 | + case 'list_sources': |
| 76 | + const spiderTools = await import('../drpy-node-mcp/tools/spiderTools.js'); |
| 77 | + handler = spiderTools.list_sources; |
| 78 | + break; |
| 79 | + case 'get_routes_info': |
| 80 | + const spiderTools2 = await import('../drpy-node-mcp/tools/spiderTools.js'); |
| 81 | + handler = spiderTools2.get_routes_info; |
| 82 | + break; |
| 83 | + case 'get_drpy_api_list': |
| 84 | + const apiTools = await import('../drpy-node-mcp/tools/apiTools.js'); |
| 85 | + handler = apiTools.get_drpy_api_list; |
| 86 | + break; |
| 87 | + case 'validate_spider': |
| 88 | + case 'check_syntax': |
| 89 | + case 'get_spider_template': |
| 90 | + case 'debug_spider_rule': |
| 91 | + const spiderTools3 = await import('../drpy-node-mcp/tools/spiderTools.js'); |
| 92 | + handler = spiderTools3[name]; |
| 93 | + break; |
| 94 | + case 'sql_query': |
| 95 | + const dbTools = await import('../drpy-node-mcp/tools/dbTools.js'); |
| 96 | + handler = dbTools.sql_query; |
| 97 | + break; |
| 98 | + case 'list_directory': |
| 99 | + case 'read_file': |
| 100 | + const fsTools = await import('../drpy-node-mcp/tools/fsTools.js'); |
| 101 | + handler = fsTools[name]; |
| 102 | + break; |
| 103 | + default: |
| 104 | + return reply.code(404).send({ error: 'Tool not found' }); |
| 105 | + } |
| 106 | + |
| 107 | + if (!handler) { |
| 108 | + return reply.code(404).send({ error: 'Tool not found' }); |
| 109 | + } |
| 110 | + |
| 111 | + const result = await handler(args || {}); |
| 112 | + |
| 113 | + if (result.isError) { |
| 114 | + return reply.code(400).send({ error: result.content[0].text }); |
| 115 | + } |
| 116 | + |
| 117 | + const content = result.content[0].text; |
| 118 | + try { |
| 119 | + return reply.send(JSON.parse(content)); |
| 120 | + } catch { |
| 121 | + return reply.send(content); |
| 122 | + } |
| 123 | + } catch (e) { |
| 124 | + reply.code(500).send({ error: e.message }); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// 导出路由配置 - 使用标准控制器模式 |
| 129 | +export default (fastify, options, done) => { |
| 130 | + // Admin 面板静态文件目录 |
| 131 | + const adminDistPath = path.join(process.cwd(), 'drpy-node-admin/dist'); |
| 132 | + |
| 133 | + if (fs.existsSync(adminDistPath)) { |
| 134 | + fastify.log.info('Serving admin panel from ' + adminDistPath); |
| 135 | + |
| 136 | + // 注册静态文件服务(在 API 路由之前注册,避免冲突) |
| 137 | + fastify.register(fastifyStatic, { |
| 138 | + root: adminDistPath, |
| 139 | + prefix: '/admin/', |
| 140 | + decorateReply: false, |
| 141 | + index: ['index.html'] |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + // API 路由(必须在静态文件服务之后注册,避免被静态文件拦截) |
| 146 | + fastify.get('/admin/config', getConfig); |
| 147 | + fastify.post('/admin/config', updateConfig); |
| 148 | + fastify.post('/admin/mcp', callMCP); |
| 149 | + |
| 150 | + done(); |
| 151 | +} |
0 commit comments