support any requests
All checks were successful
Publish Docker Images / docker (push) Successful in 1m56s
All checks were successful
Publish Docker Images / docker (push) Successful in 1m56s
This commit is contained in:
parent
c32b0cfd05
commit
57d3aa3266
@ -27,11 +27,7 @@ export default class ProxyRoute extends Route {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(url, {
|
const response = await axios.get(url);
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const data = response.data;
|
const data = response.data;
|
||||||
const headers = response.headers;
|
const headers = response.headers;
|
||||||
|
|
||||||
@ -69,7 +65,7 @@ export default class ProxyRoute extends Route {
|
|||||||
headers["access-control-allow-methods"] = "*";
|
headers["access-control-allow-methods"] = "*";
|
||||||
|
|
||||||
// Return the JSON response
|
// Return the JSON response
|
||||||
res.status(response.status).set(headers).json(data);
|
res.status(response.status).set(headers).send(data);
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
res
|
res
|
||||||
.status(500)
|
.status(500)
|
||||||
|
@ -122,14 +122,6 @@ export default class ProxyRoute extends Route {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const node = nodeManager.getRandomNode();
|
|
||||||
if (!node) {
|
|
||||||
res
|
|
||||||
.status(500)
|
|
||||||
.json(RouteMessages.internalServerError("No nodes available"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const cachedRequest = cache.get<CachedRequest>(url);
|
const cachedRequest = cache.get<CachedRequest>(url);
|
||||||
if (cachedRequest) {
|
if (cachedRequest) {
|
||||||
@ -147,6 +139,14 @@ export default class ProxyRoute extends Route {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const node = nodeManager.getRandomNode();
|
||||||
|
if (!node) {
|
||||||
|
res
|
||||||
|
.status(500)
|
||||||
|
.json(RouteMessages.internalServerError("No nodes available"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const before = Date.now();
|
const before = Date.now();
|
||||||
const response = await node.fetch(url);
|
const response = await node.fetch(url);
|
||||||
const nodeId = response.headers["x-proxy-node"];
|
const nodeId = response.headers["x-proxy-node"];
|
||||||
@ -158,21 +158,21 @@ export default class ProxyRoute extends Route {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log(nodeId, url, response.status, Date.now() - before);
|
log(nodeId, url, response.status, Date.now() - before);
|
||||||
|
|
||||||
// Return the JSON response
|
|
||||||
res.status(response.status).set(response.headers).json(data);
|
|
||||||
cache.set(url, {
|
|
||||||
nodeId: nodeId,
|
|
||||||
status: response.status,
|
|
||||||
headers: response.headers,
|
|
||||||
data: data,
|
|
||||||
} as CachedRequest);
|
|
||||||
logRequestToDatabase({
|
logRequestToDatabase({
|
||||||
nodeId,
|
nodeId,
|
||||||
url,
|
url,
|
||||||
status: response.status,
|
status: response.status,
|
||||||
time: Date.now() - before,
|
time: Date.now() - before,
|
||||||
});
|
});
|
||||||
|
cache.set(url, {
|
||||||
|
nodeId: nodeId,
|
||||||
|
status: response.status,
|
||||||
|
headers: response.headers,
|
||||||
|
data: data,
|
||||||
|
} as CachedRequest);
|
||||||
|
|
||||||
|
// Send the response to the client
|
||||||
|
res.status(response.status).set(response.headers).send(data);
|
||||||
} catch (ex: any) {
|
} catch (ex: any) {
|
||||||
res.status(500).json(RouteMessages.internalServerError(ex.message || ex));
|
res.status(500).json(RouteMessages.internalServerError(ex.message || ex));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user