Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Remove GeoLite2-City.mmdb database file and update geolocation.ts to adjust database path handling and improve error management for uninitialized reader.

Bill Yangdf26d8e8a969
diff --git a/server/src/db/geolocation/GeoLite2-City.mmdb b/server/GeoLite2-City.mmdb
similarity index 100%
rename from server/src/db/geolocation/GeoLite2-City.mmdb
rename to server/GeoLite2-City.mmdb
diff --git a/server/src/db/geolocation/geolocation.ts b/server/src/db/geolocation/geolocation.ts
index 45aa3fdcf..3646a9527 100644
--- a/server/src/db/geolocation/geolocation.ts
+++ b/server/src/db/geolocation/geolocation.ts
@@ -3,6 +3,9 @@ import { Reader } from "@maxmind/geoip2-node";
import { readFile } from "fs/promises";
import path from "path";
+// Adjust path to find the database relative to project root
+const dbPath = path.join(process.cwd(), "GeoLite2-City.mmdb");
+
interface LocationResponse {
city?: string;
country?: string;
@@ -14,29 +17,23 @@ interface LocationResponse {
subdivisions?: SubdivisionsRecord[];
}
-let reader: Reader;
+let reader: Reader | null = null;
// Extend the Reader type to include the city method
interface ExtendedReader extends Reader {
city(ip: string): City;
}
-async function loadDatabase(dbPath: string): Promise<void> {
- try {
- const dbBuffer = await readFile(dbPath);
- reader = Reader.openBuffer(dbBuffer);
- console.log("GeoIP database loaded successfully");
- } catch (error) {
- console.error("Failed to load GeoIP database:", error);
- throw error;
- }
+async function loadDatabase(dbPath: string) {
+ const dbBuffer = await readFile(dbPath);
+ reader = Reader.openBuffer(dbBuffer);
+ console.log("GeoIP database loaded successfully");
}
-await loadDatabase(path.join(process.cwd(), "GeoLite2-City.mmdb"));
+await loadDatabase(dbPath);
// Utility function to extract response data
function extractLocationData(response: City): LocationResponse {
- console.info(JSON.stringify(response, null, 2));
return {
city: response.city?.names?.en,
country: response.country?.names?.en,
@@ -49,6 +46,11 @@ function extractLocationData(response: City): LocationResponse {
}
export async function getLocation(ip: string): Promise<LocationResponse> {
+ // If reader is not initialized, return empty data
+ if (!reader) {
+ return { error: "GeoIP database not available" };
+ }
+
try {
const response = await (reader as ExtendedReader).city(ip);
return extractLocationData(response);