Spaces:
Paused
Paused
Update server.js
Browse files60 unmodified lines616263646566676869707172737475767778798081828384853 unmodified lines89909192939495111 unmodified lines20720820921021121214 unmodified lines227228229230231232233234235236237221 unmodified lines45946046146246346450 unmodified lines515516517518519520521146 unmodified lines66866967067167267367467567667767867968068168268368468560 unmodified lines}invalidate() {this.lastUpdate = 0; // 强制使缓存失效console.log('缓存被强制失效');}}const spaceCache = new SpaceCache();// 用于获取 Spaces 数据的函数,带有重试机制async function fetchSpacesWithRetry(username, token, maxRetries = 5, retryDelay = 3000) {let retries = 0;while (retries < maxRetries) {try {const headers = token ? { 'Authorization': `Bearer ${token}` } : {};const response = await axios.get(`https://huggingface.co/api/spaces?author=${username}`, {headers,timeout: 10000 // 设置 10 秒超时});const spaces = response.data;console.log(`获取到 ${spaces.length} 个 Spaces for ${username} (尝试 ${retries + 1}/${maxRetries})`);return spaces;} catch (error) {retries++;3 unmodified lines} else if (error.request) {errorDetail += ', No response received (possible network issue)';}console.error(`获取 Spaces 列表失败 for ${username} (尝试 ${retries}/${maxRetries}): ${errorDetail}`);if (retries < maxRetries) {console.log(`等待 ${retryDelay/1000} 秒后重试...`);await new Promise(resolve => setTimeout(resolve, retryDelay));111 unmodified linesconst spaces = await fetchSpacesWithRetry(username, token);for (const space of spaces) {try {const headers = token ? { 'Authorization': `Bearer ${token}` } : {};const spaceInfoResponse = await axios.get(`https://huggingface.co/api/spaces/${space.id}`, { headers });const spaceInfo = spaceInfoResponse.data;14 unmodified linesapp_port: spaceInfo.cardData?.app_port || 'unknown'});} catch (error) {console.error(`处理 Space ${space.id} 失败:`, error.message);}}} catch (error) {console.error(`获取 Spaces 列表失败 for ${username}:`, error.message);}}221 unmodified linesconst instanceId = repoId.split('/')[1];const url = `https://api.hf.space/v1/${username}/${instanceId}/live-metrics/sse`;const headers = token ? {'Authorization': `Bearer ${token}`,'Accept': 'text/event-stream',50 unmodified lines});this.connections.set(repoId, stream);console.log(`已建立监控连接 (${repoId})`);return stream;} catch (error) {console.error(`无法连接到监控端点 (${repoId}):`, error.message);146 unmodified linesres.json({ success: true, message: '订阅列表已更新' });});// 强制刷新缓存的接口app.post('/api/proxy/refresh-spaces', async (req, res) => {try {console.log('前端请求强制刷新 Spaces 缓存');spaceCache.invalidate(); // 使缓存失效res.json({ message: '缓存已失效,数据将被重新获取' });} catch (error) {console.error('强制刷新缓存失败:', error.message);res.status(500).json({ error: '强制刷新缓存失败', details: error.message });}});// 处理其他请求,重定向到 index.htmlapp.get('*', (req, res) => {res.sendFile(path.join(__dirname, 'public', 'index.html'));60 unmodified lines616263646566676869707172737475767778798081828384853 unmodified lines89909192939495111 unmodified lines20720820921021121221314 unmodified lines228229230231232233234235236237238221 unmodified lines46046146246346446546650 unmodified lines517518519520521522523146 unmodified lines67067167267367467560 unmodified lines}invalidate() {this.lastUpdate = null;}}const spaceCache = new SpaceCache();// 用于获取 Spaces 数据的函数,带有重试机制async function fetchSpacesWithRetry(username, token, maxRetries = 3, retryDelay = 2000) {let retries = 0;while (retries < maxRetries) {try {// 仅在 token 存在时添加 Authorization 头const headers = token ? { 'Authorization': `Bearer ${token}` } : {};const response = await axios.get(`https://huggingface.co/api/spaces?author=${username}`, {headers,timeout: 10000 // 设置 10 秒超时});const spaces = response.data;console.log(`获取到 ${spaces.length} 个 Spaces for ${username} (尝试 ${retries + 1}/${maxRetries}),使用 ${token ? 'Token 认证' : '无认证'}`);return spaces;} catch (error) {retries++;3 unmodified lines} else if (error.request) {errorDetail += ', No response received (possible network issue)';}console.error(`获取 Spaces 列表失败 for ${username} (尝试 ${retries}/${maxRetries}): ${errorDetail},使用 ${token ? 'Token 认证' : '无认证'}`);if (retries < maxRetries) {console.log(`等待 ${retryDelay/1000} 秒后重试...`);await new Promise(resolve => setTimeout(resolve, retryDelay));111 unmodified linesconst spaces = await fetchSpacesWithRetry(username, token);for (const space of spaces) {try {// 仅在 token 存在时添加 Authorization 头const headers = token ? { 'Authorization': `Bearer ${token}` } : {};const spaceInfoResponse = await axios.get(`https://huggingface.co/api/spaces/${space.id}`, { headers });const spaceInfo = spaceInfoResponse.data;14 unmodified linesapp_port: spaceInfo.cardData?.app_port || 'unknown'});} catch (error) {console.error(`处理 Space ${space.id} 失败:`, error.message, `使用 ${token ? 'Token 认证' : '无认证'}`);}}} catch (error) {console.error(`获取 Spaces 列表失败 for ${username}:`, error.message, `使用 ${token ? 'Token 认证' : '无认证'}`);}}221 unmodified linesconst instanceId = repoId.split('/')[1];const url = `https://api.hf.space/v1/${username}/${instanceId}/live-metrics/sse`;// 仅在 token 存在且非空时添加 Authorization 头const headers = token ? {'Authorization': `Bearer ${token}`,'Accept': 'text/event-stream',50 unmodified lines});this.connections.set(repoId, stream);console.log(`已建立监控连接 (${repoId}),使用 ${token ? 'Token 认证' : '无认证'}`);return stream;} catch (error) {console.error(`无法连接到监控端点 (${repoId}):`, error.message);146 unmodified linesres.json({ success: true, message: '订阅列表已更新' });});// 处理其他请求,重定向到 index.htmlapp.get('*', (req, res) => {res.sendFile(path.join(__dirname, 'public', 'index.html'));
server.js+10-20