-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
260 lines (233 loc) · 8.24 KB
/
server.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const express = require('express');
const fs = require('fs').promises;
const path = require('path');
const cors = require('cors');
const app = express();
const PORT = 3000;
const WEBHOOKS_FILE = path.join(__dirname, 'webhooks.json');
const LINKS_FILE = path.join(__dirname, 'links.json');
const WEBHOOK_STORAGE_FILE = path.join(__dirname, 'webhook-storage.json');
app.use(express.json());
app.use(cors());
app.use(express.static('public'));
// 确保webhooks.json文件存在
async function ensureWebhooksFile() {
try {
await fs.access(WEBHOOKS_FILE);
} catch {
await fs.writeFile(WEBHOOKS_FILE, '[]');
}
}
// 确保links.json文件存在
async function ensureLinksFile() {
try {
await fs.access(LINKS_FILE);
} catch {
await fs.writeFile(LINKS_FILE, '[]');
}
}
// 确保webhook存储文件存在
async function ensureWebhookStorageFile() {
try {
await fs.access(WEBHOOK_STORAGE_FILE);
} catch {
await fs.writeFile(WEBHOOK_STORAGE_FILE, '[]');
}
}
// 获取所有webhook
app.get('/api/webhooks', async (req, res) => {
try {
await ensureWebhooksFile();
const data = await fs.readFile(WEBHOOKS_FILE, 'utf8');
res.json(JSON.parse(data));
} catch (error) {
res.status(500).json({ error: '读取webhook失败' });
}
});
// 获取所有链接
app.get('/api/links', async (req, res) => {
try {
await ensureLinksFile();
const data = await fs.readFile(LINKS_FILE, 'utf8');
res.json(JSON.parse(data));
} catch (error) {
res.status(500).json({ error: '读取链接失败' });
}
});
// 获取存储的webhook列表
app.get('/api/webhook-storage', async (req, res) => {
try {
await ensureWebhookStorageFile();
const data = await fs.readFile(WEBHOOK_STORAGE_FILE, 'utf8');
res.json(JSON.parse(data));
} catch (error) {
res.status(500).json({ error: '读取webhook存储列表失败' });
}
});
// 添加新webhook
app.post('/api/webhooks', async (req, res) => {
try {
await ensureWebhooksFile();
const { name, url } = req.body;
const data = JSON.parse(await fs.readFile(WEBHOOKS_FILE, 'utf8'));
data.push({ name, url });
await fs.writeFile(WEBHOOKS_FILE, JSON.stringify(data, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '添加webhook失败' });
}
});
// 添加新链接
app.post('/api/links', async (req, res) => {
try {
await ensureLinksFile();
const { name, url } = req.body;
const data = JSON.parse(await fs.readFile(LINKS_FILE, 'utf8'));
data.push({ name, url });
await fs.writeFile(LINKS_FILE, JSON.stringify(data, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '添加链接失败' });
}
});
// 添加webhook到存储
app.post('/api/webhook-storage', async (req, res) => {
try {
await ensureWebhookStorageFile();
const { name, url } = req.body;
const data = JSON.parse(await fs.readFile(WEBHOOK_STORAGE_FILE, 'utf8'));
data.push({ name, url });
await fs.writeFile(WEBHOOK_STORAGE_FILE, JSON.stringify(data, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '添加到存储列表失败' });
}
});
// 删除webhook
app.delete('/api/webhooks/:index', async (req, res) => {
try {
const index = parseInt(req.params.index);
const data = JSON.parse(await fs.readFile(WEBHOOKS_FILE, 'utf8'));
data.splice(index, 1);
await fs.writeFile(WEBHOOKS_FILE, JSON.stringify(data, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '删除webhook失败' });
}
});
// 删除链接
app.delete('/api/links/:index', async (req, res) => {
try {
const index = parseInt(req.params.index);
const data = JSON.parse(await fs.readFile(LINKS_FILE, 'utf8'));
data.splice(index, 1);
await fs.writeFile(LINKS_FILE, JSON.stringify(data, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '删除链接失败' });
}
});
// 从存储中删除webhook
app.delete('/api/webhook-storage/:index', async (req, res) => {
try {
const index = parseInt(req.params.index);
const data = JSON.parse(await fs.readFile(WEBHOOK_STORAGE_FILE, 'utf8'));
data.splice(index, 1);
await fs.writeFile(WEBHOOK_STORAGE_FILE, JSON.stringify(data, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '从存储列表删除失败' });
}
});
// 更新webhook顺序
app.put('/api/webhooks', async (req, res) => {
try {
const webhooks = req.body;
await fs.writeFile(WEBHOOKS_FILE, JSON.stringify(webhooks, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '更新webhook顺序失败' });
}
});
// 添加新的路由
// 添加新分组
app.post('/api/webhook-storage/groups', async (req, res) => {
try {
const newGroup = req.body;
const data = JSON.parse(await fs.readFile(WEBHOOK_STORAGE_FILE, 'utf8'));
data.groups.push(newGroup);
await fs.writeFile(WEBHOOK_STORAGE_FILE, JSON.stringify(data, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '添加分组失败' });
}
});
// 删除分组
app.delete('/api/webhook-storage/groups/:groupId', async (req, res) => {
try {
const groupId = req.params.groupId;
const data = JSON.parse(await fs.readFile(WEBHOOK_STORAGE_FILE, 'utf8'));
data.groups = data.groups.filter(group => group.id !== groupId);
await fs.writeFile(WEBHOOK_STORAGE_FILE, JSON.stringify(data, null, 2));
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '删除分组失败' });
}
});
// 切换分组展开状态
app.put('/api/webhook-storage/groups/:groupId/toggle', async (req, res) => {
try {
const groupId = req.params.groupId;
const data = JSON.parse(await fs.readFile(WEBHOOK_STORAGE_FILE, 'utf8'));
const group = data.groups.find(g => g.id === groupId);
if (group) {
group.expanded = !group.expanded;
await fs.writeFile(WEBHOOK_STORAGE_FILE, JSON.stringify(data, null, 2));
}
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '更新分组状态失败' });
}
});
// 修改添加webhook到分组的路由
app.post('/api/webhook-storage/webhooks', async (req, res) => {
try {
const { groupId, webhook } = req.body;
const data = JSON.parse(await fs.readFile(WEBHOOK_STORAGE_FILE, 'utf8'));
// 查找目标分组
const group = data.groups.find(g => g.id === groupId);
if (!group) {
throw new Error('分组不存在');
}
// 确保 group.webhooks 是数组
if (!Array.isArray(group.webhooks)) {
group.webhooks = [];
}
// 添加 webhook 到分组
group.webhooks.push(webhook);
// 保存更新后的数据
await fs.writeFile(WEBHOOK_STORAGE_FILE, JSON.stringify(data, null, 2));
res.json({ success: true });
} catch (error) {
console.error('添加webhook失败:', error);
res.status(500).json({ error: '添加webhook失败: ' + error.message });
}
});
// 从分组中删除webhook
app.delete('/api/webhook-storage/groups/:groupId/webhooks/:index', async (req, res) => {
try {
const { groupId, index } = req.params;
const data = JSON.parse(await fs.readFile(WEBHOOK_STORAGE_FILE, 'utf8'));
const group = data.groups.find(g => g.id === groupId);
if (group) {
group.webhooks.splice(parseInt(index), 1);
await fs.writeFile(WEBHOOK_STORAGE_FILE, JSON.stringify(data, null, 2));
}
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: '删除webhook失败' });
}
});
app.listen(PORT, () => {
console.log(`服务器运行在 http://localhost:${PORT}`);
});