From 0b331ec6c6c5645d13d384a33f16e72b007bf696 Mon Sep 17 00:00:00 2001 From: ditatompel Date: Thu, 30 May 2024 12:46:33 +0700 Subject: [PATCH] Lowercase & upperase initialism acronyms See https://google.github.io/styleguide/go/decisions#initialisms --- cmd/server/probers.go | 2 +- handler/middlewares.go | 2 +- internal/geo/ip.go | 44 ++++++++++++++++++++------------------- internal/monero/monero.go | 6 +++--- internal/monero/prober.go | 8 +++---- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/cmd/server/probers.go b/cmd/server/probers.go index 73dbc8d..5afd4f2 100644 --- a/cmd/server/probers.go +++ b/cmd/server/probers.go @@ -66,7 +66,7 @@ xmr-nodes probers list -s last_submit_ts -d asc sin1`, fmt.Fprintf(w, "ID\t| Name\t| Last Submit\t| API Key\n") for _, prober := range probers { fmt.Fprintf(w, "%d\t| %s\t| %s\t| %s\n", - prober.Id, + prober.ID, prober.Name, time.Unix(prober.LastSubmitTs, 0).Format(time.RFC3339), prober.ApiKey, diff --git a/handler/middlewares.go b/handler/middlewares.go index 93a58ee..3597275 100644 --- a/handler/middlewares.go +++ b/handler/middlewares.go @@ -25,6 +25,6 @@ func CheckProber(c *fiber.Ctx) error { }) } - c.Locals("prober_id", prober.Id) + c.Locals("prober_id", prober.ID) return c.Next() } diff --git a/internal/geo/ip.go b/internal/geo/ip.go index a6dbd3a..d4272a2 100644 --- a/internal/geo/ip.go +++ b/internal/geo/ip.go @@ -7,8 +7,9 @@ import ( "github.com/oschwald/geoip2-golang" ) +// IPInfo represents IP address information from Maxmind's GeoLite2 database type IPInfo struct { - Ip string `json:"ip"` + IP string `json:"ip"` IsAnonymousProxy bool `json:"is_anonymous_proxy"` IsSatelliteProvider bool `json:"is_satellite_provider"` City string `json:"city"` @@ -21,11 +22,12 @@ type IPInfo struct { Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` AccuracyRadius uint16 `json:"accuracy_radius"` - AsnOrg string `json:"asn_org"` - Asn uint `json:"asn"` + ASNOrg string `json:"asn_org"` + ASN uint `json:"asn"` } -func IpInfo(ipAddr string) (*IPInfo, error) { +// Info returns GeoIP information from given IP address +func Info(ipAddr string) (*IPInfo, error) { ip := net.ParseIP(ipAddr) if ip == nil { return nil, errors.New("Invalid IP address") @@ -42,32 +44,32 @@ func IpInfo(ipAddr string) (*IPInfo, error) { } defer dbAsn.Close() - cityRecord, err := dbCity.City(ip) + city, err := dbCity.City(ip) if err != nil { return nil, errors.New("Cannot read GeoIP City database") } - asnRecord, err := dbAsn.ASN(ip) + asn, err := dbAsn.ASN(ip) if err != nil { return nil, errors.New("Cannot read GeoIP ASN database") } qip := IPInfo{ - Ip: ipAddr, - IsAnonymousProxy: cityRecord.Traits.IsAnonymousProxy, - IsSatelliteProvider: cityRecord.Traits.IsSatelliteProvider, - City: cityRecord.City.Names["en"], - ContinentName: cityRecord.Continent.Names["en"], - ContinentCode: cityRecord.Continent.Code, - IsInEuropeanUnion: cityRecord.Country.IsInEuropeanUnion, - CountryName: cityRecord.Country.Names["en"], - CountryCode: cityRecord.Country.IsoCode, - TimeZone: cityRecord.Location.TimeZone, - Latitude: cityRecord.Location.Latitude, - Longitude: cityRecord.Location.Longitude, - AccuracyRadius: cityRecord.Location.AccuracyRadius, - AsnOrg: asnRecord.AutonomousSystemOrganization, - Asn: asnRecord.AutonomousSystemNumber, + IP: ipAddr, + IsAnonymousProxy: city.Traits.IsAnonymousProxy, + IsSatelliteProvider: city.Traits.IsSatelliteProvider, + City: city.City.Names["en"], + ContinentName: city.Continent.Names["en"], + ContinentCode: city.Continent.Code, + IsInEuropeanUnion: city.Country.IsInEuropeanUnion, + CountryName: city.Country.Names["en"], + CountryCode: city.Country.IsoCode, + TimeZone: city.Location.TimeZone, + Latitude: city.Location.Latitude, + Longitude: city.Location.Longitude, + AccuracyRadius: city.Location.AccuracyRadius, + ASNOrg: asn.AutonomousSystemOrganization, + ASN: asn.AutonomousSystemNumber, } return &qip, nil diff --git a/internal/monero/monero.go b/internal/monero/monero.go index 29f591b..6ebb905 100644 --- a/internal/monero/monero.go +++ b/internal/monero/monero.go @@ -658,11 +658,11 @@ func (repo *MoneroRepo) ProcessJob(report ProbeReport, proberId int64) error { // recheck IP if report.NodeInfo.IP != "" { - if ipInfo, errGeoIp := geo.IpInfo(report.NodeInfo.IP); errGeoIp != nil { + if ipInfo, errGeoIp := geo.Info(report.NodeInfo.IP); errGeoIp != nil { fmt.Println("WARN:", errGeoIp.Error()) } else { - report.NodeInfo.ASN = ipInfo.Asn - report.NodeInfo.ASNName = ipInfo.AsnOrg + report.NodeInfo.ASN = ipInfo.ASN + report.NodeInfo.ASNName = ipInfo.ASNOrg report.NodeInfo.CountryCode = ipInfo.CountryCode report.NodeInfo.CountryName = ipInfo.CountryName report.NodeInfo.City = ipInfo.City diff --git a/internal/monero/prober.go b/internal/monero/prober.go index e150da4..8925f29 100644 --- a/internal/monero/prober.go +++ b/internal/monero/prober.go @@ -12,7 +12,7 @@ import ( type ProberRepository interface { Add(name string) (Prober, error) Edit(id int, name string) error - Probers(q QueryProbers) ([]Prober, error) + Probers(QueryProbers) ([]Prober, error) CheckApi(key string) (Prober, error) Delete(id int) error } @@ -22,7 +22,7 @@ type ProberRepo struct { } type Prober struct { - Id int64 `json:"id" db:"id"` + ID int64 `json:"id" db:"id"` Name string `json:"name" db:"name"` ApiKey uuid.UUID `json:"api_key" db:"api_key"` LastSubmitTs int64 `json:"last_submit_ts" db:"last_submit_ts"` @@ -140,7 +140,7 @@ func (repo *ProberRepo) Probers(q QueryProbers) ([]Prober, error) { for row.Next() { var p Prober - err = row.Scan(&p.Id, &p.Name, &p.ApiKey, &p.LastSubmitTs) + err = row.Scan(&p.ID, &p.Name, &p.ApiKey, &p.LastSubmitTs) if err != nil { return probers, err } @@ -162,6 +162,6 @@ func (repo *ProberRepo) CheckApi(key string) (Prober, error) { WHERE api_key = ? LIMIT 1` - err := repo.db.QueryRow(query, key).Scan(&p.Id, &p.Name, &p.ApiKey, &p.LastSubmitTs) + err := repo.db.QueryRow(query, key).Scan(&p.ID, &p.Name, &p.ApiKey, &p.LastSubmitTs) return p, err }