Add discord endpoint

This commit is contained in:
Christopher Cookman 2024-12-15 09:27:01 -07:00
parent 8261a31328
commit f9ddc3513f

View file

@ -491,6 +491,28 @@ app.get("/api/v1/directory", (req, res) => {
}); });
}); });
// Other public endpoints that need special handling
discordInviteCache = {time: 0, url: ""};
app.get("/discord", (req, res) => {
// fetch from process.env.WIDGET_URL, get json body, redirect to body.instant_invite. Cache url for 5 minutes
if (Date.now() - discordInviteCache.time < 300000) {
res.redirect(discordInviteCache.url);
return;
}
fetch(process.env.WIDGET_URL)
.then(response => response.json())
.then(data => {
discordInviteCache.time = Date.now();
discordInviteCache.url = data.instant_invite;
res.redirect(data.instant_invite);
})
.catch(error => {
console.error('Error fetching discord invite:', error);
res.status(500).send('Internal server error');
});
});
// Query to get a route // Query to get a route
app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => { app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
const apiKey = req.params.apiKey; const apiKey = req.params.apiKey;