File size: 1,284 Bytes
83cc97f
 
badcbfa
 
 
83cc97f
 
 
 
81ad077
12d3c5f
bcdee9c
0d246ed
12d3c5f
0d246ed
1d4cccd
0d246ed
 
81ad077
0d246ed
81ad077
 
 
 
83cc97f
4038d15
81ad077
83cc97f
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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}`);
});