Spaces:
Sleeping
Sleeping
Commit
•
5ceb06b
1
Parent(s):
082bd8c
Create server.js
Browse files
server.js
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const proxy = require('express-http-proxy');
|
3 |
+
const app = express();
|
4 |
+
const targetUrl = 'https://api.openai.com';
|
5 |
+
const openaiKey = process.env.OPENAI_KEY
|
6 |
+
const port = 7860;
|
7 |
+
|
8 |
+
app.use('/', proxy(targetUrl, {
|
9 |
+
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
10 |
+
// Modify the request headers if necessary
|
11 |
+
proxyReqOpts.headers['Authorization'] = 'Bearer '+openaiKey;
|
12 |
+
return proxyReqOpts;
|
13 |
+
},
|
14 |
+
}));
|
15 |
+
|
16 |
+
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
17 |
+
|
18 |
+
function getExternalUrl(spaceId) {
|
19 |
+
try {
|
20 |
+
const [username, spacename] = spaceId.split("/");
|
21 |
+
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/v1`;
|
22 |
+
} catch (e) {
|
23 |
+
return "";
|
24 |
+
}
|
25 |
+
}
|
26 |
+
|
27 |
+
app.listen(port, () => {
|
28 |
+
console.log(`Reverse proxy server running on ${baseUrl}`);
|