-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathsystemController.js
More file actions
205 lines (198 loc) · 6.39 KB
/
systemController.js
File metadata and controls
205 lines (198 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* 系统管理控制器
* 提供健康检查、服务重启等系统级功能
*/
import os from 'os';
import { exec } from 'child_process';
import util from 'util';
import path from 'path';
import fs from '../../utils/fsWrapper.js';
const execPromise = util.promisify(exec);
// 健康检查
export async function getHealth(req, reply) {
try {
const uptime = process.uptime();
const memory = process.memoryUsage();
const packageJson = await fs.readJson(path.join(process.cwd(), 'package.json'));
return reply.send({
status: 'ok',
uptime: Math.floor(uptime),
memory: {
used: Math.round(memory.heapUsed / 1024 / 1024),
total: Math.round(memory.heapTotal / 1024 / 1024),
rss: Math.round(memory.rss / 1024 / 1024)
},
version: packageJson.version,
platform: {
arch: os.arch(),
platform: os.platform(),
nodeVersion: process.version
},
timestamp: Date.now()
});
} catch (e) {
reply.code(500).send({
status: 'error',
error: e.message
});
}
}
// 服务重启
export async function restartService(req, reply) {
try {
// 检查是否在 PM2 环境运行
try {
await execPromise('pm2 restart drpys');
return reply.send({
success: true,
message: '服务已通过 PM2 重启'
});
} catch (pm2Error) {
return reply.send({
success: false,
message: '当前未使用 PM2 运行。请在终端中手动重启服务:\n1. 按 Ctrl+C 停止当前服务\n2. 运行 npm run dev 重新启动'
});
}
} catch (e) {
reply.code(500).send({
success: false,
error: e.message
});
}
}
// API 文档
export async function getApiDocs(req, reply) {
const apiDocs = [
{
category: "系统管理",
endpoints: [
{
path: "/api/admin/health",
method: "GET",
description: "获取系统健康状态,包括内存使用、运行时间、版本等信息"
},
{
path: "/api/admin/restart",
method: "POST",
description: "尝试重启服务(仅在 PM2 环境下有效)"
},
{
path: "/api/admin/docs",
method: "GET",
description: "获取 API 文档列表"
}
]
},
{
category: "日志管理",
endpoints: [
{
path: "/api/admin/logs",
method: "GET",
description: "获取最近的系统日志"
},
{
path: "/ws",
method: "WS",
description: "WebSocket 实时日志流连接端点(全局)"
}
]
},
{
category: "配置管理",
endpoints: [
{
path: "/api/admin/config",
method: "GET",
description: "获取系统配置,支持通过 key 参数获取特定配置项"
},
{
path: "/api/admin/config",
method: "POST",
description: "更新系统配置"
},
{
path: "/api/admin/env",
method: "GET",
description: "获取关键环境变量配置"
}
]
},
{
category: "源管理",
endpoints: [
{
path: "/api/admin/sources",
method: "GET",
description: "获取所有 JS 和 CatVod 源列表"
},
{
path: "/api/admin/sources/validate",
method: "POST",
description: "验证源文件的格式和必要字段"
},
{
path: "/api/admin/sources/syntax",
method: "POST",
description: "检查源文件的语法正确性"
},
{
path: "/api/admin/sources/template",
method: "GET",
description: "获取标准源文件模板"
},
{
path: "/api/admin/sources/libs",
method: "GET",
description: "获取爬虫相关库函数和解析规则说明"
}
]
},
{
category: "文件管理",
endpoints: [
{
path: "/api/admin/files/list",
method: "GET",
description: "列出指定目录下的文件和文件夹"
},
{
path: "/api/admin/files/read",
method: "GET",
description: "读取指定文件的内容"
},
{
path: "/api/admin/files/write",
method: "POST",
description: "写入内容到指定文件"
},
{
path: "/api/admin/files/delete",
method: "DELETE",
description: "删除指定的文件"
}
]
},
{
category: "数据库管理",
endpoints: [
{
path: "/api/admin/db/query",
method: "POST",
description: "执行 SQL 查询语句"
},
{
path: "/api/admin/db/tables",
method: "GET",
description: "获取数据库所有表名"
},
{
path: "/api/admin/db/tables/:table/schema",
method: "GET",
description: "获取指定表的结构定义"
}
]
}
];
return reply.send(apiDocs);
}