Spaces:
Paused
Paused
Download server.js from advins/openai-reverse-proxy: direct link, hf CLI and curl.
- Browser
- Download file 903 Bytes
-
https://huggingface.co/spaces/advins/openai-reverse-proxy/resolve/main/server.js
- Command line
-
hf download hf://spaces/advins/openai-reverse-proxy/server.js
-
curl -L -o server.js https://huggingface.co/spaces/advins/openai-reverse-proxy/resolve/main/server.js
903 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; | |
| const baseUrl = getExternalUrl(process.env.SPACE_ID); | |
| app.use('/api', proxy(targetUrl, { | |
| proxyReqOptDecorator: (proxyReqOpts, srcReq) => { | |
| // Modify the request headers if necessary | |
| proxyReqOpts.headers['Authorization'] = 'Bearer '+openaiKey; | |
| return proxyReqOpts; | |
| }, | |
| })); | |
| app.get("/", (req, res) => { | |
| res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`); | |
| }); | |
| 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}`); | |
| }); |