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