ar4chnophobia
commited on
Commit
•
5151b05
1
Parent(s):
4e822c3
Create server.js
Browse files
server.js
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
8 |
+
|
9 |
+
app.use('/api', proxy(targetUrl, {
|
10 |
+
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
11 |
+
// Modify the request headers if necessary
|
12 |
+
proxyReqOpts.headers['Authorization'] = 'Bearer '+openaiKey;
|
13 |
+
return proxyReqOpts;
|
14 |
+
},
|
15 |
+
}));
|
16 |
+
|
17 |
+
app.get("/", (req, res) => {
|
18 |
+
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
|
19 |
+
});
|
20 |
+
|
21 |
+
function getExternalUrl(spaceId) {
|
22 |
+
try {
|
23 |
+
const [username, spacename] = spaceId.split("/");
|
24 |
+
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
|
25 |
+
} catch (e) {
|
26 |
+
return "";
|
27 |
+
}
|
28 |
+
}
|
29 |
+
|
30 |
+
app.listen(port, () => {
|
31 |
+
console.log(`Reverse proxy server running on ${baseUrl}`);
|
32 |
+
});
|