Skip to content

Commit

Permalink
Load lookup database only once.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Apr 26, 2021
1 parent 8307b46 commit 0cf115b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
MOBILE_SCREEN_WIDTH,
} from './constants';

let lookup;

export function getIpAddress(req) {
// Cloudflare
if (req.headers['cf-connecting-ip']) {
Expand Down Expand Up @@ -61,7 +63,9 @@ export async function getCountry(req, ip) {
}

// Database lookup
const lookup = await maxmind.open(path.resolve('./public/geo/GeoLite2-Country.mmdb'));
if (!lookup) {
lookup = await maxmind.open(path.resolve('./public/geo/GeoLite2-Country.mmdb'));
}

const result = lookup.get(ip);

Expand Down

0 comments on commit 0cf115b

Please sign in to comment.