Use temporary fix

This commit is contained in:
Volodymyr Kravchenko 2025-02-06 00:37:35 +02:00
parent 01da057b3e
commit c6373adcb1

View file

@ -14,7 +14,17 @@ import {
} from './constants';
import { NextApiRequestCollect } from 'pages/api/send';
let lookup;
let lookupPromise: any = null;
// This function returns the open DB. If it's already opening/open, just return the existing promise.
export function getLookup() {
if (!lookupPromise) {
console.log('debug: loading GeoLite2-City.mmdb');
const dir = path.join(process.cwd(), 'geo');
lookupPromise = maxmind.open(path.resolve(dir, 'GeoLite2-City.mmdb'));
}
return lookupPromise;
}
export function getIpAddress(req: NextApiRequestCollect) {
const customHeader = String(process.env.CLIENT_IP_HEADER).toLowerCase();
@ -108,16 +118,11 @@ export async function getLocation(ip: string, req: NextApiRequestCollect) {
}
// Database lookup
if (!lookup) {
// eslint-disable-next-line no-console
console.log('debug: loading GeoLite2-City.mmdb');
const dir = path.join(process.cwd(), 'geo');
lookup = await maxmind.open(path.resolve(dir, 'GeoLite2-City.mmdb'));
}
const lookup = await getLookup();
const result = lookup.get(ip);
console.log('debug_result:', result);
if (result) {
const country = result.country?.iso_code ?? result?.registered_country?.iso_code;
const subdivision1 = result.subdivisions?.[0]?.iso_code;