mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
fix(admin): name the configured geo provider in the system snapshot
The admin system snapshot hardcoded "ipwho.is" for the offline-geo-off state, so an operator who points IP_GEO_LOOKUP_URL at another provider still read the wrong host. Resolve the actual provider host from the env and surface it through /api/version, so the snapshot reads "Online (ip-api.com)" — the host it really queries.
This commit is contained in:
1 parent
6fe552ffdf
commit
45ca6ff24b
13 files changed
+98
-18
No files matched your search
@@ -425,3 +425,34 @@ describe("lookupIpGeo carrier resolution (v1.25.8)", () => {
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveGeoProviderHost (task #162)", () => {
|
||||
it("returns the default ipwho.is host when IP_GEO_LOOKUP_URL is unset", async () => {
|
||||
const { resolveGeoProviderHost } = await import("../geo");
|
||||
expect(resolveGeoProviderHost()).toBe("ipwho.is");
|
||||
});
|
||||
|
||||
it("returns the configured provider host", async () => {
|
||||
process.env.IP_GEO_LOOKUP_URL = "http://ip-api.com/json";
|
||||
const { resolveGeoProviderHost } = await import("../geo");
|
||||
expect(resolveGeoProviderHost()).toBe("ip-api.com");
|
||||
});
|
||||
|
||||
it("preserves an explicit port in the host", async () => {
|
||||
process.env.IP_GEO_LOOKUP_URL = "https://geo.example.com:8443/lookup";
|
||||
const { resolveGeoProviderHost } = await import("../geo");
|
||||
expect(resolveGeoProviderHost()).toBe("geo.example.com:8443");
|
||||
});
|
||||
|
||||
it("falls back to the default host for a blank env value", async () => {
|
||||
process.env.IP_GEO_LOOKUP_URL = " ";
|
||||
const { resolveGeoProviderHost } = await import("../geo");
|
||||
expect(resolveGeoProviderHost()).toBe("ipwho.is");
|
||||
});
|
||||
|
||||
it("falls back to the default host for an unparseable env value", async () => {
|
||||
process.env.IP_GEO_LOOKUP_URL = "not a url";
|
||||
const { resolveGeoProviderHost } = await import("../geo");
|
||||
expect(resolveGeoProviderHost()).toBe("ipwho.is");
|
||||
});
|
||||
});
|
||||
@@ -121,6 +121,32 @@ const PRIVATE_IP =
|
||||
// online lookup misses, and skipped entirely when the databases are absent.
|
||||
const DEFAULT_GEO_URL = "https://ipwho.is";
|
||||
|
||||
// The host portion of DEFAULT_GEO_URL — the provider name the admin status
|
||||
// surface shows when the offline tier is absent and the runtime resolves
|
||||
// locations online.
|
||||
const DEFAULT_GEO_HOST = "ipwho.is";
|
||||
|
||||
/**
|
||||
* Resolve the online geo provider HOST the runtime actually queries.
|
||||
*
|
||||
* Reads `IP_GEO_LOOKUP_URL` and returns its hostname (e.g. `ip-api.com`),
|
||||
* falling back to the default `ipwho.is` when the env is unset, blank, or
|
||||
* not a parseable URL. Pure and safe — never throws.
|
||||
*
|
||||
* `/api/version` surfaces this so the admin System-Snapshot can name the
|
||||
* real provider instead of a hardcoded "ipwho.is": an operator who points
|
||||
* `IP_GEO_LOOKUP_URL` at another provider sees that provider's host.
|
||||
*/
|
||||
export function resolveGeoProviderHost(): string {
|
||||
const raw = process.env.IP_GEO_LOOKUP_URL?.trim();
|
||||
if (!raw) return DEFAULT_GEO_HOST;
|
||||
try {
|
||||
return new URL(raw).host || DEFAULT_GEO_HOST;
|
||||
} catch {
|
||||
return DEFAULT_GEO_HOST;
|
||||
}
|
||||
}
|
||||
|
||||
function geoLiteDir(): string {
|
||||
return process.env.GEOLITE2_DIR ?? "/opt/geolite2";
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user