openai-proxy / server.js
Rooni's picture
Update server.js
9ee6c89 verified
raw
history blame contribute delete
800 Bytes
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();
const targetUrl = 'https://api.openai.com';
const openaiKey = process.env.OPENAI_KEY
const port = 7860;
app.use('/', proxy(targetUrl, {
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
// Modify the request headers if necessary
// proxyReqOpts.headers['Authorization'] = 'Bearer '+openaiKey;
return proxyReqOpts;
},
}));
const baseUrl = getExternalUrl(process.env.SPACE_ID);
function getExternalUrl(spaceId) {
try {
const [username, spacename] = spaceId.split("/");
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/v1`;
} catch (e) {
return "";
}
}
app.listen(port, () => {
console.log(`Reverse proxy server running on ${baseUrl}`);
});