diff --git a/src/app.js b/src/app.js index b297ea6..51d641e 100644 --- a/src/app.js +++ b/src/app.js @@ -7,12 +7,14 @@ const morgan = require('morgan'); const helmet = require('helmet'); const app = express(); const { assignId, skipStatics, skipStatusServer } = require('./middlewares/morgan'); +const { locator } = require('./middlewares/locator'); const BootService = require('./services/boot.service'); const execMode = BootService.getExecutionMode(); const PORT = process.env.PORT || 3000; app.use(express.json()) app.use(cookieParser()); +app.use(locator); app.use(helmet({ contentSecurityPolicy: false })); @@ -23,18 +25,14 @@ morgan.token('accepted-cookies', function(req) { return Boolean(req.cookies['cookie-accepted']); }); morgan.token('location', async function(req) { - const ip = execMode == "pro" ? req.headers['x-real-ip'] : req.ip; - const response = await fetch(`https://api.iplocation.net/?ip=${ip}`); - const data = await response.json(); - const countryName = data.country_name; - return countryName; + return req.location; }); morgan.token('id', function getId(req) { return req.id; }); app.use(assignId); app.use( - morgan('{ \n Request ID: :id \n Request IP: :user-ip \n Request location: :location \n Method: :method \n Route: :url \n Status code: :status \n Response time: :response-time \n Accepted Cookies: :accepted-cookies \n}', { + morgan('{ \n Request ID: :id \n Request IP: :user-ip \n Request location: :location \n Method: :method \n Route: :url \n Status code: :status \n Response time: :response-time \n Accepted Cookies: :accepted-cookies \n}', { skip: function (req, res) { return skipStatics(req, res) || skipStatusServer(req, res); } diff --git a/src/middlewares/locator.js b/src/middlewares/locator.js new file mode 100644 index 0000000..ad283c7 --- /dev/null +++ b/src/middlewares/locator.js @@ -0,0 +1,12 @@ +async function locator(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(); + const countryName = data.country_name; + req.location = countryName; + next(); +} + +module.exports = { + locator +} \ No newline at end of file