Update index.js
Browse files
index.js
CHANGED
|
@@ -1,108 +1,68 @@
|
|
| 1 |
-
// Import required packages
|
| 2 |
import express from 'express';
|
| 3 |
import cors from 'cors';
|
| 4 |
import fetch from 'node-fetch';
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
-
const PORT = process.env.PORT || 7860;
|
| 8 |
|
| 9 |
-
// Target
|
| 10 |
-
const
|
| 11 |
-
"https://gpt4fc.deno.dev/zaiwen",
|
| 12 |
-
"https://gpt4fc2.deno.dev/zaiwen",
|
| 13 |
-
"https://gpt4fc3.deno.dev/zaiwen",
|
| 14 |
-
"https://gpt4fc4.deno.dev/zaiwen",
|
| 15 |
-
];
|
| 16 |
|
| 17 |
// Middleware setup
|
| 18 |
app.use(express.json());
|
| 19 |
app.use(cors({
|
| 20 |
origin: '*',
|
| 21 |
-
methods: ['POST'
|
| 22 |
-
allowedHeaders: ['Content-Type', 'Authorization'
|
| 23 |
}));
|
| 24 |
|
| 25 |
-
//
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
try {
|
| 33 |
-
// Forward the request to the target API
|
| 34 |
-
const headers = {
|
| 35 |
-
'Content-Type': 'application/json',
|
| 36 |
-
};
|
| 37 |
-
|
| 38 |
-
// Forward other relevant headers from the original request
|
| 39 |
-
['authorization', 'openai-organization', 'user-agent'].forEach(header => {
|
| 40 |
-
if (req.headers[header]) {
|
| 41 |
-
headers[header] = req.headers[header];
|
| 42 |
-
}
|
| 43 |
-
});
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
console.warn(
|
| 54 |
-
`Error from target API ${apiUrl}:`,
|
| 55 |
-
response.status,
|
| 56 |
-
response.statusText
|
| 57 |
-
);
|
| 58 |
-
continue; // Try the next API URL
|
| 59 |
-
}
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
responseBody.choices &&
|
| 68 |
-
responseBody.choices.length > 0 &&
|
| 69 |
-
responseBody.choices[0].message &&
|
| 70 |
-
responseBody.choices[0].message.content &&
|
| 71 |
-
responseBody.choices[0].message.content.trim() !== ""
|
| 72 |
-
) {
|
| 73 |
-
// Return the response to the client
|
| 74 |
-
return res.status(response.status).json(responseBody);
|
| 75 |
-
} else {
|
| 76 |
-
console.warn(`API ${apiUrl} returned empty or invalid content. Trying next API.`);
|
| 77 |
-
continue; // Try the next API URL
|
| 78 |
-
}
|
| 79 |
-
} catch (error) {
|
| 80 |
-
console.warn(`Error calling API ${apiUrl}:`, error);
|
| 81 |
-
continue; // Try the next API URL
|
| 82 |
-
}
|
| 83 |
-
}
|
| 84 |
|
| 85 |
-
|
| 86 |
-
return res.status(500).send('All APIs failed to return valid content.');
|
| 87 |
} catch (error) {
|
| 88 |
-
console.error('Error
|
| 89 |
-
return res.status(500).send('
|
| 90 |
}
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
// Setup routes
|
| 94 |
-
app.post('/v1/chat/completions', handleCompletions);
|
| 95 |
-
|
| 96 |
-
// Default route
|
| 97 |
-
app.get('/', (req, res) => {
|
| 98 |
-
res.send('API is running');
|
| 99 |
});
|
| 100 |
|
| 101 |
-
// Start
|
| 102 |
app.listen(PORT, () => {
|
| 103 |
-
console.log(`Server
|
| 104 |
});
|
| 105 |
|
| 106 |
-
// For
|
| 107 |
export default app;
|
| 108 |
-
|
|
|
|
| 1 |
+
// Import required packages
|
| 2 |
import express from 'express';
|
| 3 |
import cors from 'cors';
|
| 4 |
import fetch from 'node-fetch';
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
+
const PORT = process.env.PORT || 7860;
|
| 8 |
|
| 9 |
+
// Target server
|
| 10 |
+
const TARGET_DOMAIN = "https://aliyun.zaiwen.top";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
// Middleware setup
|
| 13 |
app.use(express.json());
|
| 14 |
app.use(cors({
|
| 15 |
origin: '*',
|
| 16 |
+
methods: ['POST'],
|
| 17 |
+
allowedHeaders: ['Content-Type', 'Authorization']
|
| 18 |
}));
|
| 19 |
|
| 20 |
+
// Only allow POST requests
|
| 21 |
+
app.all('*', (req, res, next) => {
|
| 22 |
+
if (req.method !== 'POST') {
|
| 23 |
+
return res.status(405).send('Method Not Allowed');
|
| 24 |
+
}
|
| 25 |
+
next();
|
| 26 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
// Proxy handler
|
| 29 |
+
app.post('*', async (req, res) => {
|
| 30 |
+
const targetUrl = `${TARGET_DOMAIN}${req.path}${req.url.includes('?') ? '?' + req.url.split('?')[1] : ''}`;
|
| 31 |
+
|
| 32 |
+
try {
|
| 33 |
+
const response = await fetch(targetUrl, {
|
| 34 |
+
method: 'POST',
|
| 35 |
+
headers: {
|
| 36 |
+
'Content-Type': 'application/json',
|
| 37 |
+
...req.headers
|
| 38 |
+
},
|
| 39 |
+
body: JSON.stringify(req.body)
|
| 40 |
+
});
|
| 41 |
|
| 42 |
+
const body = await response.text(); // 保持原样,前端自己决定如何解析
|
| 43 |
+
res.status(response.status);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
// 把目标服务器返回的 headers 也加回来(注意 CORS 要处理一下)
|
| 46 |
+
response.headers.forEach((value, key) => {
|
| 47 |
+
if (key.toLowerCase() === 'content-length') return; // 不转发 content-length
|
| 48 |
+
res.setHeader(key, value);
|
| 49 |
+
});
|
| 50 |
|
| 51 |
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 52 |
+
res.setHeader('Access-Control-Allow-Methods', 'POST');
|
| 53 |
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
return res.send(body);
|
|
|
|
| 56 |
} catch (error) {
|
| 57 |
+
console.error('Proxy Error:', error);
|
| 58 |
+
return res.status(500).send('Proxy Server Error');
|
| 59 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
});
|
| 61 |
|
| 62 |
+
// Start server
|
| 63 |
app.listen(PORT, () => {
|
| 64 |
+
console.log(`Server running on port ${PORT}`);
|
| 65 |
});
|
| 66 |
|
| 67 |
+
// For Huggingface deployment
|
| 68 |
export default app;
|
|
|