forked from ChatGPTNextWeb/NextChat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent.js.standalone.bak
65 lines (52 loc) · 1.73 KB
/
agent.js.standalone.bak
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
const express = require('express');
const fs = require('fs');
const path = require('path');
const bodyParser = require('body-parser');
// npm install express parser
// 从环境变量中获取目录和端口
const QUOTA_PATH = process.env.QUOTA_PATH;
const PORT = process.env.PORT || 3000;
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
// 查询余额接口
app.get('/balance', (req, res) => {
const userName = req.query.userName;
if (!userName) {
res.status(400).send({ error: 'Missing userName parameter' });
return;
}
const filePath = path.join(QUOTA_PATH, userName);
// 读取用户余额
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
res.status(404).send({ error: 'User not found' });
} else {
res.status(500).send({ error: 'File read error', details: err.message });
}
} else {
res.send({ balance: data.trim() });
}
});
});
// 更新余额接口
app.post('/balance', (req, res) => {
const userName = req.body.userName;
const newQuota = req.body.newQuota;
if (!userName || !newQuota || isNaN(Number(newQuota))) {
res.status(400).send({ error: 'Missing or invalid parameters' });
return;
}
const filePath = path.join(QUOTA_PATH, userName);
// 写入新余额
fs.writeFile(filePath, newQuota, (err) => {
if (err) {
res.status(500).send({ error: 'File write error', details: err.message });
} else {
res.send({ message: 'Balance updated successfully' });
}
});
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});