Add GeoIP support for unique IP aggregation
Publish telemt-api gateway Docker image / test (push) Failing after 17s
Publish telemt-api gateway Docker image / build-and-push (push) Has been skipped

- Introduced GeoIP configuration options in config.example.yaml to enable geolocation lookups for the /api/agg/unique-ips endpoint.
- Updated the aggregate handler to include optional GeoIP data in responses, enriching unique IP information with country and city details, as well as ASN data if available.
- Enhanced documentation in AGGREGATE.md and README.md to reflect the new GeoIP functionality and its usage.
- Added a dependency on the geoip2-golang library in go.mod for GeoIP lookups.
- Modified tests to accommodate the new GeoIP integration in the aggregate handler.
This commit is contained in:
Denozordec
2026-03-30 01:47:08 +07:00
parent 4a4f15bd0b
commit d7a63f0da3
15 changed files with 449 additions and 12 deletions
+41
View File
@@ -0,0 +1,41 @@
package aggregate
import (
"github.com/telemt/telemt-api/internal/geoip"
)
// EnrichUniqueIPsGeo fills country/city on each IP when lookup succeeds.
func EnrichUniqueIPsGeo(rows []UniqueIPsRow, g *geoip.Service) {
if g == nil || len(rows) == 0 {
return
}
for i := range rows {
for j := range rows[i].IPs {
ip := rows[i].IPs[j].IP
res, ok := g.Lookup(ip)
if !ok {
continue
}
if res.CountryCode != "" {
s := res.CountryCode
rows[i].IPs[j].CountryCode = &s
}
if res.CountryName != "" {
s := res.CountryName
rows[i].IPs[j].CountryName = &s
}
if res.CityName != "" {
s := res.CityName
rows[i].IPs[j].CityName = &s
}
if res.ASN != 0 {
a := res.ASN
rows[i].IPs[j].ASN = &a
}
if res.ASOrg != "" {
o := res.ASOrg
rows[i].IPs[j].ASOrganization = &o
}
}
}
}