feat: implement core platform features including business directory, admin dashboard, authentication, and API infrastructure
Some checks failed
Build and Push App / build (push) Failing after 16m2s
Some checks failed
Build and Push App / build (push) Failing after 16m2s
This commit is contained in:
44
lib/geo.ts
Normal file
44
lib/geo.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
export async function getGeoInfo(ip: string, headers: Headers) {
|
||||
// 1. Try platform-specific headers first (Vercel, Cloudflare, etc.)
|
||||
const headerCountry =
|
||||
headers.get('x-vercel-ip-country') ||
|
||||
headers.get('cf-ipcountry') ||
|
||||
headers.get('x-country-code');
|
||||
|
||||
if (headerCountry && headerCountry !== 'XX') {
|
||||
// If we have a country code, we might still want to get the name
|
||||
// For now, let's keep it simple or use a lookup map
|
||||
return {
|
||||
country: headerCountry,
|
||||
city: headers.get('x-vercel-ip-city') || 'Unknown'
|
||||
};
|
||||
}
|
||||
|
||||
// 2. Handle localhost
|
||||
if (ip === '127.0.0.1' || ip === '::1' || ip.includes('127.0.0.1')) {
|
||||
return { country: 'Local / Dev', city: 'Localhost' };
|
||||
}
|
||||
|
||||
// 3. Fallback to external API
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 2000); // 2s timeout
|
||||
|
||||
const response = await fetch(`http://ip-api.com/json/${ip}`, { signal: controller.signal });
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
if (data.status === 'success') {
|
||||
return {
|
||||
country: data.country || 'Unknown',
|
||||
city: data.city || 'Unknown'
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('GeoIP lookup error:', error);
|
||||
}
|
||||
|
||||
return { country: 'Unknown', city: 'Unknown' };
|
||||
}
|
||||
Reference in New Issue
Block a user