Update server.js
Browse files
server.js
CHANGED
@@ -8,20 +8,34 @@ const port = 7860;
|
|
8 |
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
9 |
|
10 |
let requestCount = 0; // Counter to keep track of the number of proxied requests
|
|
|
11 |
|
12 |
app.use('/api', (req, res, next) => {
|
13 |
requestCount++; // Increment request count for every request passed to /api
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
next();
|
15 |
}, proxy(targetUrl, {
|
16 |
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
17 |
-
// Modify the request headers if necessary
|
18 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
19 |
return proxyReqOpts;
|
20 |
},
|
21 |
}));
|
22 |
|
23 |
app.get("/", (req, res) => {
|
24 |
-
|
|
|
25 |
});
|
26 |
|
27 |
function getExternalUrl(spaceId) {
|
@@ -34,5 +48,5 @@ function getExternalUrl(spaceId) {
|
|
34 |
}
|
35 |
|
36 |
app.listen(port, () => {
|
37 |
-
console.log(`Reverse proxy server running on ${baseUrl}
|
38 |
});
|
|
|
8 |
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
9 |
|
10 |
let requestCount = 0; // Counter to keep track of the number of proxied requests
|
11 |
+
let requestDetails = []; // Array to store details of each request
|
12 |
|
13 |
app.use('/api', (req, res, next) => {
|
14 |
requestCount++; // Increment request count for every request passed to /api
|
15 |
+
|
16 |
+
// Capture the request details
|
17 |
+
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
18 |
+
const requestData = {
|
19 |
+
requestNumber: requestCount,
|
20 |
+
ip: ip,
|
21 |
+
text: req.method + ' ' + req.url // Example: captures request method and path
|
22 |
+
};
|
23 |
+
|
24 |
+
// Push details to the array
|
25 |
+
requestDetails.push(requestData);
|
26 |
+
|
27 |
+
console.log(`Request ${requestData.requestNumber} from IP ${requestData.ip} with text "${requestData.text}"`);
|
28 |
next();
|
29 |
}, proxy(targetUrl, {
|
30 |
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
|
|
31 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
32 |
return proxyReqOpts;
|
33 |
},
|
34 |
}));
|
35 |
|
36 |
app.get("/", (req, res) => {
|
37 |
+
let requestsInfo = requestDetails.map(detail => `Request ${detail.requestNumber} from IP ${detail.ip} with text "${detail.text}"`).join('<br>');
|
38 |
+
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>${requestsInfo}`);
|
39 |
});
|
40 |
|
41 |
function getExternalUrl(spaceId) {
|
|
|
48 |
}
|
49 |
|
50 |
app.listen(port, () => {
|
51 |
+
console.log(`Reverse proxy server running on ${baseUrl}.`);
|
52 |
});
|