Spaces:
Paused
Paused
| const express = require('express'); | |
| const proxy = require('express-http-proxy'); | |
| const app = express(); | |
| const bodyParser = require('body-parser'); | |
| const targetUrl = 'https://api.openai.com'; | |
| const openaiKey = process.env.OPENAI_KEY; | |
| const proxyKey = process.env.PROXY_KEY; // Your secret proxy key | |
| const port = 7860; | |
| const baseUrl = getExternalUrl(process.env.SPACE_ID); | |
| app.get('/getYour', (req, res) => { | |
| const providedProxyKey = req.headers['x-proxy-key']; // Assuming the proxy key is sent in the 'x-proxy-key' header | |
| // Check if both OpenAI API key and valid proxy key are provided and match | |
| if (providedProxyKey === proxyKey) { | |
| // Return the OpenAI API key as a JSON response | |
| res.json({ apiKey: openaiKey }); | |
| } else { | |
| // If either key is missing or incorrect, return an error response | |
| res.status(401).json({ error: 'Unauthorized' }); | |
| } | |
| }); | |
| 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}`); | |
| }); |