|
const express = require('express'); |
|
const proxy = require('express-http-proxy'); |
|
|
|
const app = express(); |
|
|
|
const targetUrl = 'https://api.openai.com'; |
|
|
|
const openai_key = process.env.OPENAI_KEY |
|
|
|
app.use('/', proxy(targetUrl, { |
|
proxyReqOptDecorator: (proxyReqOpts, srcReq) => { |
|
|
|
proxyReqOpts.headers['Authorization'] = 'Bearer '+openai_key; |
|
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}`); |
|
}); |