| | 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) { |
| | |
| | const openaiKey = req.headers['authorization']; |
| | |
| | |
| | proxy(targetUrl, { |
| | proxyReqOptDecorator: (proxyReqOpts, srcReq) => { |
| | |
| | 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}`); |
| | }); |
| |
|