File size: 1,094 Bytes
6ac828b e987042 6ac828b e987042 6ac828b e987042 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();
const targetUrl = process.env.OPENAI_URL;
const port = 7860;
const baseUrl = getExternalUrl(process.env.SPACE_ID);
app.use('/api', function(req, res, next) {
// Get openaiKey from the request headers
const openaiKey = req.headers['authorization'];
// Proxy request to targetUrl with modified headers
proxy(targetUrl, {
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
// Modify the request headers if necessary
proxyReqOpts.headers['Authorization'] = openaiKey;
return proxyReqOpts;
}
})(req, res, next);
});
app.use(express.static('public'));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
function getExternalUrl(spaceId) {
try {
const [username, spacename] = spaceId.split("/");
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
} catch (e) {
return "";
}
}
app.listen(port, () => {
console.log(`Reverse proxy server running on ${baseUrl}`);
});
|