Skip to content

Commit

Permalink
feat: adds request location (back again)
Browse files Browse the repository at this point in the history
  • Loading branch information
migtarx committed Mar 5, 2024
1 parent 15ee608 commit 58a530f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ morgan.token('accepted-cookies', function(req) {
return Boolean(req.cookies['cookie-accepted']);
});
app.use(async function getLocation(req, res, next) {
// const ip = global.exec_mode == "pro" ? req.headers['x-real-ip'] : req.ip;
// const response = await fetch(`https://api.iplocation.net/?ip=${ip}`);
// const data = await response.json();
req.location = "unknown"//data.country_name;
let data;
try {
const ip = global.exec_mode == "pro" ? req.headers['x-real-ip'] : req.ip;
const response = await fetch(`https://api.iplocation.net/?ip=${ip}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
data = await response.json();
} catch (error) {
console.error('Error fetching IP location:', error);
data = { country_name: 'Unknown || Error fetching' };
}
req.location = data.country_name;
next();
});

Expand Down

0 comments on commit 58a530f

Please sign in to comment.