Commit
·
6ac828b
1
Parent(s):
b2dda28
Create server.js
Browse files
server.js
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const proxy = require('express-http-proxy');
|
3 |
+
const app = express();
|
4 |
+
const targetUrl = process.env.OPENAI_URL;
|
5 |
+
const port = 7860;
|
6 |
+
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
7 |
+
|
8 |
+
app.use('/api', function(req, res, next) {
|
9 |
+
// Get openaiKey from the request headers
|
10 |
+
const openaiKey = req.headers['authorization'];
|
11 |
+
|
12 |
+
// Proxy request to targetUrl with modified headers
|
13 |
+
proxy(targetUrl, {
|
14 |
+
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
15 |
+
// Modify the request headers if necessary
|
16 |
+
proxyReqOpts.headers['Authorization'] = openaiKey;
|
17 |
+
return proxyReqOpts;
|
18 |
+
}
|
19 |
+
})(req, res, next);
|
20 |
+
});
|
21 |
+
|
22 |
+
app.get("/", (req, res) => {
|
23 |
+
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`);
|
24 |
+
});
|
25 |
+
|
26 |
+
function getExternalUrl(spaceId) {
|
27 |
+
try {
|
28 |
+
const [username, spacename] = spaceId.split("/");
|
29 |
+
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
|
30 |
+
} catch (e) {
|
31 |
+
return "";
|
32 |
+
}
|
33 |
+
}
|
34 |
+
|
35 |
+
app.listen(port, () => {
|
36 |
+
console.log(`Reverse proxy server running on ${baseUrl}`);
|
37 |
+
});
|