Files
whatismyip/router/geo.go
T
dcarrillo 2e32a20f60 refactor: dependency injection instead of package globals (#57)
* refactor: export Settings type, Setup returns value

* refactor: resolver.Setup takes explicit Settings struct

* refactor: GetHeadersWithoutTrustedHeaders takes explicit header params

* refactor: server constructors take narrow config

* refactor: Router struct with handler methods, remove geoSvc global

* refactor: wire DI through main, remove setting.App references

* refactor: remove App global, use returned Settings

* refactor: update router tests for DI

* chore: fix lint issues — rename ServerTimeouts to Timeouts, fix shadowed variables
2026-07-21 18:37:21 +02:00

167 lines
3.5 KiB
Go

package router
import (
"fmt"
"net"
"net/http"
"sort"
"strings"
"github.com/dcarrillo/whatismyip/models"
"github.com/gin-gonic/gin"
)
type geoDataFormatter struct {
title string
format func(*models.GeoRecord) string
}
type asnDataFormatter struct {
title string
format func(*models.ASNRecord) string
}
var geoOutput = map[string]geoDataFormatter{
"country": {
title: "Country",
format: func(record *models.GeoRecord) string {
return record.Country.Names["en"]
},
},
"country_code": {
title: "Country Code",
format: func(record *models.GeoRecord) string {
return record.Country.ISOCode
},
},
"city": {
title: "City",
format: func(record *models.GeoRecord) string {
return record.City.Names["en"]
},
},
"latitude": {
title: "Latitude",
format: func(record *models.GeoRecord) string {
return fmt.Sprintf("%f", record.Location.Latitude)
},
},
"longitude": {
title: "Longitude",
format: func(record *models.GeoRecord) string {
return fmt.Sprintf("%f", record.Location.Longitude)
},
},
"postal_code": {
title: "Postal Code",
format: func(record *models.GeoRecord) string {
return record.Postal.Code
},
},
"time_zone": {
title: "Time Zone",
format: func(record *models.GeoRecord) string {
return record.Location.TimeZone
},
},
}
var asnOutput = map[string]asnDataFormatter{
"number": {
title: "ASN Number",
format: func(record *models.ASNRecord) string {
return fmt.Sprintf("%d", record.AutonomousSystemNumber)
},
},
"organization": {
title: "ASN Organization",
format: func(record *models.ASNRecord) string {
return record.AutonomousSystemOrganization
},
},
}
func (rt *Router) getGeoAsString(ctx *gin.Context) {
if rt.geo == nil {
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
return
}
record := rt.geo.LookUpCity(net.ParseIP(ctx.ClientIP()))
if record == nil {
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
return
}
field := strings.ToLower(ctx.Params.ByName("field"))
if field == "" {
ctx.String(http.StatusOK, geoCityRecordToString(record))
return
}
g, ok := geoOutput[field]
if !ok {
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
return
}
ctx.String(http.StatusOK, g.format(record))
}
func (rt *Router) getASNAsString(ctx *gin.Context) {
if rt.geo == nil {
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
return
}
record := rt.geo.LookUpASN(net.ParseIP(ctx.ClientIP()))
if record == nil {
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
return
}
field := strings.ToLower(ctx.Params.ByName("field"))
if field == "" {
ctx.String(http.StatusOK, geoASNRecordToString(record))
return
}
g, ok := asnOutput[field]
if !ok {
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
return
}
ctx.String(http.StatusOK, g.format(record))
}
func geoCityRecordToString(record *models.GeoRecord) string {
var output string
keys := make([]string, 0, len(geoOutput))
for k := range geoOutput {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
output += fmt.Sprintf("%s: %v\n", geoOutput[k].title, geoOutput[k].format(record))
}
return output
}
func geoASNRecordToString(record *models.ASNRecord) string {
var output string
keys := make([]string, 0, len(asnOutput))
for k := range asnOutput {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
output += fmt.Sprintf("%s: %v\n", asnOutput[k].title, asnOutput[k].format(record))
}
return output
}