Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,15 +1,106 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const path = require('path');
|
|
|
|
| 3 |
const app = express();
|
| 4 |
const port = process.env.PORT || 8080;
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
// 提供静态文件(前端文件)
|
| 7 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 8 |
|
| 9 |
// 提供配置信息的 API 接口
|
| 10 |
app.get('/api/config', (req, res) => {
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
});
|
| 14 |
|
| 15 |
// 处理其他请求,重定向到 index.html
|
|
@@ -19,5 +110,5 @@ app.get('*', (req, res) => {
|
|
| 19 |
|
| 20 |
app.listen(port, () => {
|
| 21 |
console.log(`Server running on port ${port}`);
|
| 22 |
-
console.log(`
|
| 23 |
});
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const path = require('path');
|
| 3 |
+
const axios = require('axios');
|
| 4 |
const app = express();
|
| 5 |
const port = process.env.PORT || 8080;
|
| 6 |
|
| 7 |
+
// 从环境变量 HF_USER 获取用户和对应的 API Token 映射
|
| 8 |
+
const userTokenMapping = {};
|
| 9 |
+
const usernames = [];
|
| 10 |
+
const hfUserConfig = process.env.HF_USER || '';
|
| 11 |
+
if (hfUserConfig) {
|
| 12 |
+
hfUserConfig.split(',').forEach(pair => {
|
| 13 |
+
const parts = pair.split(':').map(part => part.trim());
|
| 14 |
+
const username = parts[0];
|
| 15 |
+
const token = parts[1] || '';
|
| 16 |
+
if (username) {
|
| 17 |
+
usernames.push(username);
|
| 18 |
+
if (token) {
|
| 19 |
+
userTokenMapping[username] = token;
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
});
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
// 提供静态文件(前端文件)
|
| 26 |
app.use(express.static(path.join(__dirname, 'public')));
|
| 27 |
|
| 28 |
// 提供配置信息的 API 接口
|
| 29 |
app.get('/api/config', (req, res) => {
|
| 30 |
+
res.json({ usernames: usernames.join(',') });
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
// 代理 HuggingFace API:获取用户的 spaces 列表(包括私有)
|
| 34 |
+
app.get('/api/proxy/spaces', async (req, res) => {
|
| 35 |
+
try {
|
| 36 |
+
const { author } = req.query;
|
| 37 |
+
if (!author) {
|
| 38 |
+
return res.status(400).json({ error: '缺少 author 参数' });
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
const token = userTokenMapping[author];
|
| 42 |
+
let response;
|
| 43 |
+
if (!token) {
|
| 44 |
+
console.warn(`用户 ${author} 没有配置 API Token,将尝试无认证访问公开数据`);
|
| 45 |
+
// 如果没有 Token,仍然尝试获取公开数据
|
| 46 |
+
response = await axios.get(`https://huggingface.co/api/spaces?author=${author}`);
|
| 47 |
+
} else {
|
| 48 |
+
response = await axios.get(`https://huggingface.co/api/spaces?author=${author}`, {
|
| 49 |
+
headers: {
|
| 50 |
+
'Authorization': `Bearer ${token}`
|
| 51 |
+
}
|
| 52 |
+
});
|
| 53 |
+
}
|
| 54 |
+
res.json(response.data);
|
| 55 |
+
} catch (error) {
|
| 56 |
+
console.error(`代理获取 spaces 列表失败 (${req.query.author}):`, error.message);
|
| 57 |
+
res.status(error.response?.status || 500).json({ error: '获取 spaces 列表失败', details: error.message });
|
| 58 |
+
}
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
// 代理 HuggingFace API:获取实时监控数据(SSE 需要特殊处理)
|
| 62 |
+
app.get('/api/proxy/live-metrics/:username/:instanceId', async (req, res) => {
|
| 63 |
+
try {
|
| 64 |
+
const { username, instanceId } = req.params;
|
| 65 |
+
const url = `https://api.hf.space/v1/${username}/${instanceId}/live-metrics/sse`;
|
| 66 |
+
|
| 67 |
+
const token = userTokenMapping[username];
|
| 68 |
+
let headers = {};
|
| 69 |
+
if (token) {
|
| 70 |
+
headers = { 'Authorization': `Bearer ${token}` };
|
| 71 |
+
} else {
|
| 72 |
+
console.warn(`用户 ${username} 没有配置 API Token,将尝试无认证访问公开数据`);
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// 发起请求并设置流式响应
|
| 76 |
+
const response = await axios({
|
| 77 |
+
method: 'get',
|
| 78 |
+
url,
|
| 79 |
+
headers,
|
| 80 |
+
responseType: 'stream'
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
// 将响应流转发到客户端
|
| 84 |
+
res.set({
|
| 85 |
+
'Content-Type': 'text/event-stream',
|
| 86 |
+
'Cache-Control': 'no-cache',
|
| 87 |
+
'Connection': 'keep-alive'
|
| 88 |
+
});
|
| 89 |
+
response.data.pipe(res);
|
| 90 |
+
|
| 91 |
+
// 监听错误
|
| 92 |
+
response.data.on('error', (err) => {
|
| 93 |
+
console.error(`直播监控数据流错误 (${username}/${instanceId}):`, err);
|
| 94 |
+
res.status(500).end();
|
| 95 |
+
});
|
| 96 |
+
|
| 97 |
+
req.on('close', () => {
|
| 98 |
+
response.data.destroy();
|
| 99 |
+
});
|
| 100 |
+
} catch (error) {
|
| 101 |
+
console.error(`代理获取直播监控数据失败 (${req.params.username}/${req.params.instanceId}):`, error.message);
|
| 102 |
+
res.status(error.response?.status || 500).json({ error: '获取监控数据失败', details: error.message });
|
| 103 |
+
}
|
| 104 |
});
|
| 105 |
|
| 106 |
// 处理其他请求,重定向到 index.html
|
|
|
|
| 110 |
|
| 111 |
app.listen(port, () => {
|
| 112 |
console.log(`Server running on port ${port}`);
|
| 113 |
+
console.log(`User configurations:`, Object.keys(userTokenMapping).map(user => `${user}: ${userTokenMapping[user] ? 'Token Configured' : 'No Token'}`).join(', ') || 'None');
|
| 114 |
});
|