Ayeantics commited on
Commit
d5b575b
·
verified ·
1 Parent(s): 177a238

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +37 -35
server.js CHANGED
@@ -11,50 +11,52 @@ let requestDetails = [];
11
  let ipRequestCounts = {};
12
 
13
  app.use('/api', (req, res, next) => {
14
- const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
15
- const currentCount = ipRequestCounts[ip] || 0;
16
-
17
- if (currentCount >= 2) {
18
- return res.status(429).send('You have reached your request limit of 10 messages. Please contact support for more details.');
19
- }
20
-
21
- ipRequestCounts[ip] = currentCount + 1;
22
- const timestamp = new Date().toISOString();
23
- const requestData = {
24
- requestNumber: ipRequestCounts[ip],
25
- ip: ip,
26
- timestamp: timestamp,
27
- text: req.method + ' ' + req.url
28
- };
29
- requestDetails.push(requestData);
30
- console.log(`Request ${requestData.requestNumber} from IP ${requestData.ip} on ${requestData.timestamp}`);
31
-
32
- next();
 
33
  }, proxy(targetUrl, {
34
- proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
35
- proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
36
- return proxyReqOpts;
37
- },
38
- userResDecorator: (proxyRes, proxyResData, userReq, userRes) => {
39
- if (proxyRes.statusCode === 429) {
40
- // Return a custom message for the 429 error from the proxy attempt
41
- return Buffer.from('Request limit reached on the proxied service. Please try again later.');
 
 
 
42
  }
43
- return proxyResData; // Return original data if response is not 429
44
- }
45
  }));
46
 
47
  app.get("/", (req, res) => {
48
- let requestsInfo = requestDetails.map(detail =>
49
- `Request ${detail.requestNumber} from IP ${detail.ip} on ${detail.timestamp}`).join('<br>');
50
- res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>${requestsInfo}`);
51
  });
52
 
53
  function getExternalUrl(spaceId) {
54
- const [username, spacename] = spaceId.split("/");
55
- return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
56
  }
57
 
58
  app.listen(port, () => {
59
- console.log(`Reverse proxy server running on ${baseUrl}.`);
60
  });
 
11
  let ipRequestCounts = {};
12
 
13
  app.use('/api', (req, res, next) => {
14
+ const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
15
+ const currentCount = ipRequestCounts[ip] || 0;
16
+
17
+ if (currentCount >= 1) {
18
+ res.status(429).send('You have reached your request limit of 10 messages. Please contact support for more details.');
19
+ return;
20
+ }
21
+
22
+ ipRequestCounts[ip] = currentCount + 1;
23
+ const timestamp = new Date().toISOString();
24
+ const requestData = {
25
+ requestNumber: ipRequestCounts[ip],
26
+ ip: ip,
27
+ timestamp: timestamp,
28
+ text: req.method + ' ' + req.url
29
+ };
30
+ requestDetails.push(requestData);
31
+ console.log(`Request ${requestData.requestNumber} from IP ${requestData.ip} on ${requestData.timestamp}`);
32
+
33
+ next();
34
  }, proxy(targetUrl, {
35
+ proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
36
+ proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
37
+ return proxyReqOpts;
38
+ },
39
+ userResDecorator: (proxyRes, proxyResData, userReq, userRes) => {
40
+ if (proxyRes.statusCode === 429) {
41
+ return JSON.stringify({
42
+ message: 'Request limit reached on the proxied service. Please try again later.'
43
+ });
44
+ }
45
+ return proxyResData; // Return the original response body if not a 429 error
46
  }
 
 
47
  }));
48
 
49
  app.get("/", (req, res) => {
50
+ let requestsInfo = requestDetails.map(detail =>
51
+ `Request ${detail.requestNumber} from IP ${detail.ip} on ${detail.timestamp}`).join('<br>');
52
+ res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>${requestsInfo}`);
53
  });
54
 
55
  function getExternalUrl(spaceId) {
56
+ const [username, spacename] = spaceId.split("/");
57
+ return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
58
  }
59
 
60
  app.listen(port, () => {
61
+ console.log(`Reverse proxy server running on ${baseUrl}.`);
62
  });