yuoop commited on
Commit
d71e176
·
verified ·
1 Parent(s): 3c7faf3

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +42 -82
index.js CHANGED
@@ -1,108 +1,68 @@
1
- // Import required packages for Hugging Face
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; // Hugging Face uses port 7860 by default
8
 
9
- // Target API URLs remain the same
10
- const TARGET_API_URLS = [
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', 'OPTIONS'],
22
- allowedHeaders: ['Content-Type', 'Authorization', 'OpenAI-Organization', 'User-Agent']
23
  }));
24
 
25
- // Handler for completions endpoint
26
- async function handleCompletions(req, res) {
27
- try {
28
- const requestBody = req.body;
29
-
30
- // Iterate through the target API URLs
31
- for (const apiUrl of TARGET_API_URLS) {
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
- const response = await fetch(`${apiUrl}/v1/chat/completions`, {
46
- method: 'POST',
47
- headers: headers,
48
- body: JSON.stringify(requestBody)
49
- });
 
 
 
 
 
 
 
 
50
 
51
- // Check if the target API returned an error
52
- if (!response.ok) {
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
- // Get the response from the target API
62
- const responseBody = await response.json();
 
 
 
63
 
64
- // Check if the response has content and it's not empty
65
- if (
66
- responseBody &&
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
- // If all APIs failed, return an error
86
- return res.status(500).send('All APIs failed to return valid content.');
87
  } catch (error) {
88
- console.error('Error handling /v1/chat/completions:', error);
89
- return res.status(500).send('Internal Server Error');
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 the server
102
  app.listen(PORT, () => {
103
- console.log(`Server is running on port ${PORT}`);
104
  });
105
 
106
- // For Hugging Face deployment
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;