From 2d330a99b79dd802e05828be4e3b464aebacdb96 Mon Sep 17 00:00:00 2001 From: Daniel Carrillo Date: Tue, 30 Nov 2021 17:21:01 +0100 Subject: [PATCH] Add CI and go-report badges. Add comments to make golint happy --- README.md | 3 +++ internal/core/version.go | 1 + internal/httputils/http.go | 5 +++-- internal/setting/app.go | 4 ++++ models/geo.go | 7 +++++++ router/generic.go | 1 + router/setup.go | 2 ++ service/geo.go | 3 +++ 8 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1d8d4d7..dde3b8f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # What is my IP address +[![CI](https://github.com/dcarrillo/whatismyip/workflows/CI/badge.svg)](https://github.com/dcarrillo/whatismyip/actions) +[![Go Report Card](https://goreportcard.com/badge/github.com/dcarrillo/whatismyip)](https://goreportcard.com/report/github.com/dcarrillo/whatismyip) + - [What is my IP address](#what-is-my-ip-address) - [Features](#features) - [Endpoints](#endpoints) diff --git a/internal/core/version.go b/internal/core/version.go index 2ee25bc..509e625 100644 --- a/internal/core/version.go +++ b/internal/core/version.go @@ -1,3 +1,4 @@ package core +// Version to be defined on build time var Version = "tobedefined" diff --git a/internal/httputils/http.go b/internal/httputils/http.go index f698156..6bfda70 100644 --- a/internal/httputils/http.go +++ b/internal/httputils/http.go @@ -9,6 +9,7 @@ import ( "github.com/gin-gonic/gin" ) +// HeadersToSortedString shorts and dumps http.Header to a string separated by \n func HeadersToSortedString(headers http.Header) string { var output string @@ -31,6 +32,7 @@ func HeadersToSortedString(headers http.Header) string { return output } +// GetLogFormatter returns our custom log format func GetLogFormatter(param gin.LogFormatterParams) string { return fmt.Sprintf("%s - [%s] \"%s %s %s\" %d %d %d %s \"%s\" \"%s\" \"%s\"\n", param.ClientIP, @@ -57,9 +59,8 @@ func normalizeLog(log interface{}) interface{} { case []string: if len(v) == 0 { return "-" - } else { - return strings.Join(v, ", ") } + return strings.Join(v, ", ") } return log diff --git a/internal/setting/app.go b/internal/setting/app.go index 1d454c1..0386bf4 100644 --- a/internal/setting/app.go +++ b/internal/setting/app.go @@ -33,7 +33,10 @@ type settings struct { const defaultAddress = ":8080" +// ErrVersion is the custom error triggered when -version flag is passed var ErrVersion = errors.New("setting: version requested") + +// App is the var with the parsed settings var App = settings{ // hard-coded for the time being Server: serverSettings{ @@ -42,6 +45,7 @@ var App = settings{ }, } +// Setup initializes the App object parsing the flags func Setup(args []string) (output string, err error) { flags := flag.NewFlagSet("whatismyip", flag.ContinueOnError) var buf bytes.Buffer diff --git a/models/geo.go b/models/geo.go index cd1a573..a9049ec 100644 --- a/models/geo.go +++ b/models/geo.go @@ -7,10 +7,12 @@ import ( "github.com/oschwald/maxminddb-golang" ) +// Record is the interface to be implemented for record operations type Record interface { LookUp(ip net.IP) } +// GeoRecord is the model for City database type GeoRecord struct { Country struct { ISOCode string `maxminddb:"iso_code"` @@ -29,6 +31,7 @@ type GeoRecord struct { } `maxminddb:"postal"` } +// ASNRecord is the model for ASN database type ASNRecord struct { AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"` AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"` @@ -51,11 +54,13 @@ func openMMDB(path string) *maxminddb.Reader { return db } +// Setup opens all Geolite2 databases func Setup(cityPath string, asnPath string) { db.city = openMMDB(cityPath) db.asn = openMMDB(asnPath) } +// CloseDBs unmaps from memory and frees resources to the filesystem func CloseDBs() { log.Printf("Closing dbs...") if err := db.city.Close(); err != nil { @@ -66,6 +71,7 @@ func CloseDBs() { } } +// LookUp an IP and get city data func (record *GeoRecord) LookUp(ip net.IP) error { err := db.city.Lookup(ip, record) if err != nil { @@ -75,6 +81,7 @@ func (record *GeoRecord) LookUp(ip net.IP) error { return nil } +// LookUp an IP and get ASN data func (record *ASNRecord) LookUp(ip net.IP) error { err := db.asn.Lookup(ip, record) if err != nil { diff --git a/router/generic.go b/router/generic.go index 417bf31..5b36cf6 100644 --- a/router/generic.go +++ b/router/generic.go @@ -14,6 +14,7 @@ import ( const userAgentPattern = `curl|wget|libwww-perl|python|ansible-httpget|HTTPie|WindowsPowerShell|http_request|Go-http-client|^$` +// JSONResponse maps data as json type JSONResponse struct { IP string `json:"ip"` IPVersion byte `json:"ip_version"` diff --git a/router/setup.go b/router/setup.go index b08692b..b1b10cc 100644 --- a/router/setup.go +++ b/router/setup.go @@ -8,6 +8,7 @@ import ( "github.com/gin-gonic/gin" ) +// SetupTemplate reads and parses a template from file func SetupTemplate(r *gin.Engine) { if setting.App.TemplatePath == "" { t, _ := template.New("home").Parse(home) @@ -18,6 +19,7 @@ func SetupTemplate(r *gin.Engine) { } } +// Setup defines the endpoints func Setup(r *gin.Engine) { r.GET("/", getRoot) r.GET("/client-port", getClientPortAsString) diff --git a/service/geo.go b/service/geo.go index 8d63ea7..c8a5df0 100644 --- a/service/geo.go +++ b/service/geo.go @@ -7,10 +7,12 @@ import ( "github.com/dcarrillo/whatismyip/models" ) +// Geo defines a base type for lookups type Geo struct { IP net.IP } +// LookUpCity queries the database for city data related to the given IP func (g *Geo) LookUpCity() *models.GeoRecord { record := &models.GeoRecord{} err := record.LookUp(g.IP) @@ -22,6 +24,7 @@ func (g *Geo) LookUpCity() *models.GeoRecord { return record } +// LookUpASN queries the database for ASN data related to the given IP func (g *Geo) LookUpASN() *models.ASNRecord { record := &models.ASNRecord{} err := record.LookUp(g.IP)