From d5fcc5cef9f974c79cc8aef5e1678dc6c004da89 Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Sat, 20 Nov 2021 23:06:40 +0500 Subject: [PATCH] Linter and Drone config fixes, code linting. --- .drone.yml | 3 +- .gitignore | 1 + .golangci.yml | 12 ++++-- cmd/giredorectl/main.go | 7 +--- cmd/giredored/main.go | 2 - domains/client/v1/config.go | 2 - domains/client/v1/exported.go | 9 +---- domains/client/v1/packages.go | 3 +- domains/server/v1/configapi.go | 18 ++++----- domains/server/v1/exported.go | 9 +---- domains/server/v1/goimports.go | 25 +++++++------ domains/server/v1/packagesapi.go | 51 ++++++++++++++++---------- internal/configuration/envconfig.go | 1 - internal/configuration/exported.go | 7 ++-- internal/configuration/fileconfig.go | 20 ++++++---- internal/httpserver/checkallowedips.go | 26 ++++++------- internal/httpserver/exported.go | 30 ++++++++++----- internal/httpserver/requestlogger.go | 18 ++++----- internal/httpserver/strictjson.go | 8 ++-- internal/logger/exported.go | 26 ++++++------- internal/requester/exported.go | 14 +++---- internal/requester/httpclient.go | 6 +-- 22 files changed, 152 insertions(+), 146 deletions(-) diff --git a/.drone.yml b/.drone.yml index 26057ac..41802fc 100644 --- a/.drone.yml +++ b/.drone.yml @@ -21,8 +21,7 @@ steps: - name: docker image: plugins/docker when: - branch: - master + branch: ["master"] settings: username: from_secret: dockerhub_user diff --git a/.gitignore b/.gitignore index 422975f..41c0d46 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *DS_Store* data/* .idea +.vscode diff --git a/.golangci.yml b/.golangci.yml index 6912df0..b0c6e37 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -15,6 +15,12 @@ linters: linters-settings: lll: line-length: 420 - gocyclo: - min-complexity: 40 - \ No newline at end of file + cyclop: + max-complexity: 25 + +issues: + exclude-rules: + # There will be some ToDos. + - linters: + - godox + text: "TODO" diff --git a/cmd/giredorectl/main.go b/cmd/giredorectl/main.go index d343205..3b020ec 100644 --- a/cmd/giredorectl/main.go +++ b/cmd/giredorectl/main.go @@ -1,15 +1,11 @@ package main import ( - // stdlib "os" - // local + "github.com/teris-io/cli" clientv1 "go.dev.pztrn.name/giredore/domains/client/v1" "go.dev.pztrn.name/giredore/internal/logger" - - // other - "github.com/teris-io/cli" ) func main() { @@ -34,6 +30,7 @@ func main() { logger.Initialize() clientv1.Initialize() clientv1.SetAllowedIPs(args, options) + return 0 }), ), diff --git a/cmd/giredored/main.go b/cmd/giredored/main.go index 9df5d1b..800cde7 100644 --- a/cmd/giredored/main.go +++ b/cmd/giredored/main.go @@ -1,12 +1,10 @@ package main import ( - // stdlib "os" "os/signal" "syscall" - // local serverv1 "go.dev.pztrn.name/giredore/domains/server/v1" "go.dev.pztrn.name/giredore/internal/configuration" "go.dev.pztrn.name/giredore/internal/httpserver" diff --git a/domains/client/v1/config.go b/domains/client/v1/config.go index 0691216..67e3ea7 100644 --- a/domains/client/v1/config.go +++ b/domains/client/v1/config.go @@ -1,10 +1,8 @@ package clientv1 import ( - // stdlib "strings" - // local "go.dev.pztrn.name/giredore/internal/requester" "go.dev.pztrn.name/giredore/internal/structs" ) diff --git a/domains/client/v1/exported.go b/domains/client/v1/exported.go index 5ef9b2f..10ba894 100644 --- a/domains/client/v1/exported.go +++ b/domains/client/v1/exported.go @@ -1,17 +1,12 @@ package clientv1 import ( - // local + "github.com/rs/zerolog" "go.dev.pztrn.name/giredore/internal/logger" "go.dev.pztrn.name/giredore/internal/requester" - - // other - "github.com/rs/zerolog" ) -var ( - log zerolog.Logger -) +var log zerolog.Logger func Initialize() { log = logger.Logger.With().Str("type", "domain").Str("package", "client").Int("version", 1).Logger() diff --git a/domains/client/v1/packages.go b/domains/client/v1/packages.go index fc70c72..9826a0d 100644 --- a/domains/client/v1/packages.go +++ b/domains/client/v1/packages.go @@ -1,10 +1,8 @@ package clientv1 import ( - // stdlib "strings" - // local "go.dev.pztrn.name/giredore/internal/requester" "go.dev.pztrn.name/giredore/internal/structs" ) @@ -29,6 +27,7 @@ func DeletePackage(args []string, options map[string]string) { func GetPackages(args []string, options map[string]string) { pkgs := strings.Split(args[0], ",") + // nolint:exhaustivestruct req := &structs.PackageGetRequest{} if pkgs[0] == "all" { req.All = true diff --git a/domains/server/v1/configapi.go b/domains/server/v1/configapi.go index df7e164..137bc3f 100644 --- a/domains/server/v1/configapi.go +++ b/domains/server/v1/configapi.go @@ -1,32 +1,32 @@ package serverv1 import ( - // stdlib "net/http" - // local + "github.com/labstack/echo" "go.dev.pztrn.name/giredore/internal/configuration" "go.dev.pztrn.name/giredore/internal/structs" - - // other - "github.com/labstack/echo" ) // This function responsible for getting runtime configuration. func configurationGET(ec echo.Context) error { + // nolint:wrapcheck return ec.JSON(http.StatusOK, configuration.Cfg) } -func configurationAllowedIPsSET(ec echo.Context) error { +func configurationAllowedIPsSET(ectx echo.Context) error { + // nolint:exhaustivestruct req := &structs.AllowedIPsSetRequest{} - if err := ec.Bind(req); err != nil { + if err := ectx.Bind(req); err != nil { log.Error().Err(err).Msg("Failed to parse allowed IPs set request") - return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingAllowedIPsSetRequest}}) + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingAllowedIPsSetRequest}}) } log.Debug().Msgf("Got set allowed IPs request: %+v", req) configuration.Cfg.SetAllowedIPs(req.AllowedIPs) - return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess}) + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess}) } diff --git a/domains/server/v1/exported.go b/domains/server/v1/exported.go index 991cb02..41abafa 100644 --- a/domains/server/v1/exported.go +++ b/domains/server/v1/exported.go @@ -1,17 +1,12 @@ package serverv1 import ( - // local + "github.com/rs/zerolog" "go.dev.pztrn.name/giredore/internal/httpserver" "go.dev.pztrn.name/giredore/internal/logger" - - // other - "github.com/rs/zerolog" ) -var ( - log zerolog.Logger -) +var log zerolog.Logger func Initialize() { log = logger.Logger.With().Str("type", "domain").Str("package", "server").Int("version", 1).Logger() diff --git a/domains/server/v1/goimports.go b/domains/server/v1/goimports.go index ba7dd23..aeba796 100644 --- a/domains/server/v1/goimports.go +++ b/domains/server/v1/goimports.go @@ -1,43 +1,43 @@ package serverv1 import ( - // stdlib "net/http" "strings" - // local + "github.com/labstack/echo" "go.dev.pztrn.name/giredore/internal/configuration" "go.dev.pztrn.name/giredore/internal/structs" - - // other - "github.com/labstack/echo" ) -func throwGoImports(ec echo.Context) error { +func throwGoImports(ectx echo.Context) error { // Getting real path. This might be the package itself, or namespace // to list available packages. // For now only package itself is supported, all other features in ToDo. - packageNameRaw := ec.Request().URL.Path + packageNameRaw := ectx.Request().URL.Path pkgs, errs := configuration.Cfg.GetPackagesInfo([]string{packageNameRaw}) if errs != nil { log.Error().Str("package", packageNameRaw).Msgf("Failed to get package information: %+v", errs) - return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs}) + + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs}) } if len(pkgs) == 0 { - return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}}) + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}}) } pkg, found := pkgs[packageNameRaw] if !found { - return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}}) + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}}) } // We should compose package name using our domain under which giredore // is working. - domain := ec.Request().Host + domain := ectx.Request().Host packageName := domain + packageNameRaw tmpl := singlePackageTemplate @@ -45,5 +45,6 @@ func throwGoImports(ec echo.Context) error { tmpl = strings.Replace(tmpl, "{VCS}", pkg.VCS, 1) tmpl = strings.Replace(tmpl, "{REPOPATH}", pkg.RealPath, 1) - return ec.HTML(http.StatusOK, tmpl) + // nolint:wrapcheck + return ectx.HTML(http.StatusOK, tmpl) } diff --git a/domains/server/v1/packagesapi.go b/domains/server/v1/packagesapi.go index c9f9e31..d66dd46 100644 --- a/domains/server/v1/packagesapi.go +++ b/domains/server/v1/packagesapi.go @@ -1,24 +1,23 @@ package serverv1 import ( - // stdlib "net/http" "strings" - // local + "github.com/labstack/echo" "go.dev.pztrn.name/giredore/internal/configuration" "go.dev.pztrn.name/giredore/internal/structs" - - // other - "github.com/labstack/echo" ) // This function responsible for getting packages configuration. -func packagesGET(ec echo.Context) error { +func packagesGET(ectx echo.Context) error { + // nolint:exhaustivestruct req := &structs.PackageGetRequest{} - if err := ec.Bind(req); err != nil { + if err := ectx.Bind(req); err != nil { log.Error().Err(err).Msg("Failed to parse package get request") - return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingPackagesGetRequest}}) + + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingPackagesGetRequest}}) } log.Info().Msgf("Received package(s) info get request: %+v", req) @@ -34,18 +33,23 @@ func packagesGET(ec echo.Context) error { } if len(errors) > 0 { - return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errors, Data: pkgs}) + // nolint:wrapcheck + return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errors, Data: pkgs}) } - return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess, Data: pkgs}) + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess, Data: pkgs}) } // This function responsible for deleting package. -func packagesDELETE(ec echo.Context) error { +func packagesDELETE(ectx echo.Context) error { + // nolint:exhaustivestruct req := &structs.PackageDeleteRequest{} - if err := ec.Bind(req); err != nil { + if err := ectx.Bind(req); err != nil { log.Error().Err(err).Msg("Failed to parse package delete request") - return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingDeleteRequest}}) + + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingDeleteRequest}}) } log.Info().Msgf("Received package delete request: %+v", req) @@ -53,28 +57,35 @@ func packagesDELETE(ec echo.Context) error { errs := configuration.Cfg.DeletePackage(req) if len(errs) > 0 { - return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs}) + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs}) } - return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess}) + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess}) } // This function responsible for setting or updating packages. -func packagesSET(ec echo.Context) error { +func packagesSET(ectx echo.Context) error { + // nolint:exhaustivestruct req := &structs.Package{} - if err := ec.Bind(req); err != nil { + if err := ectx.Bind(req); err != nil { log.Error().Err(err).Msg("Failed to parse package data") - return ec.JSON(http.StatusBadRequest, nil) + + // nolint:wrapcheck + return ectx.JSON(http.StatusBadRequest, nil) } log.Info().Msgf("Received package set/update request: %+v", req) // Validate passed package data. if !strings.HasPrefix(req.OriginalPath, "/") { - return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrPackageOrigPathShouldStartWithSlash}}) + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrPackageOrigPathShouldStartWithSlash}}) } configuration.Cfg.AddOrUpdatePackage(req) - return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess}) + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess}) } diff --git a/internal/configuration/envconfig.go b/internal/configuration/envconfig.go index be928f8..134fae9 100644 --- a/internal/configuration/envconfig.go +++ b/internal/configuration/envconfig.go @@ -1,7 +1,6 @@ package configuration import ( - // other "github.com/vrischmann/envconfig" ) diff --git a/internal/configuration/exported.go b/internal/configuration/exported.go index 3bd9e53..1372755 100644 --- a/internal/configuration/exported.go +++ b/internal/configuration/exported.go @@ -1,11 +1,8 @@ package configuration import ( - // local - "go.dev.pztrn.name/giredore/internal/logger" - - // other "github.com/rs/zerolog" + "go.dev.pztrn.name/giredore/internal/logger" ) var ( @@ -19,9 +16,11 @@ func Initialize() { log = logger.Logger.With().Str("type", "internal").Str("package", "configuration").Logger() log.Info().Msg("Initializing...") + // nolint:exhaustivestruct envCfg = &envConfig{} envCfg.Initialize() + // nolint:exhaustivestruct Cfg = &fileConfig{} Cfg.Initialize() diff --git a/internal/configuration/fileconfig.go b/internal/configuration/fileconfig.go index 8739d4d..00e764a 100644 --- a/internal/configuration/fileconfig.go +++ b/internal/configuration/fileconfig.go @@ -1,7 +1,6 @@ package configuration import ( - // stdlib "encoding/json" "io/ioutil" "os" @@ -9,7 +8,6 @@ import ( "strings" "sync" - // local "go.dev.pztrn.name/giredore/internal/structs" ) @@ -19,22 +17,22 @@ import ( // may be accesses concurrently. In other words DO NOT USE EXPORTED FIELDS // DIRECTLY! type fileConfig struct { + packagesMutex sync.RWMutex + // Packages describes packages mapping. + Packages map[string]*structs.Package // HTTP describes HTTP server configuration. HTTP struct { - // AllowedIPs is a list of IPs that allowed to access API. - // There might be other authentication implemented in future. - AllowedIPs []string allowedipsmutex sync.RWMutex // Listen is an address on which HTTP server will listen. Listen string + // AllowedIPs is a list of IPs that allowed to access API. + // There might be other authentication implemented in future. + AllowedIPs []string // WaitForSeconds is a timeout during which we will wait for // HTTP server be up. If timeout will pass and HTTP server won't // start processing requests - giredore will exit. WaitForSeconds int } - // Packages describes packages mapping. - Packages map[string]*structs.Package - packagesMutex sync.RWMutex } func (fc *fileConfig) AddOrUpdatePackage(pkg *structs.Package) { @@ -52,6 +50,7 @@ func (fc *fileConfig) DeletePackage(req *structs.PackageDeleteRequest) []structs if !found { errors = append(errors, structs.ErrPackageWasntDefined) + return errors } @@ -116,6 +115,7 @@ func (fc *fileConfig) Initialize() { // exists. if _, err2 := os.Stat(configPath); os.IsNotExist(err2) { cfgLoadLog.Error().Msg("Unable to load configuration from filesystem.") + return } @@ -141,6 +141,7 @@ func (fc *fileConfig) Initialize() { for _, ip := range fc.HTTP.AllowedIPs { if strings.Contains(ip, "127.0.0.1") { localhostIsAllowed = true + break } } @@ -163,6 +164,7 @@ func (fc *fileConfig) normalizePath(configPath string) (string, error) { if strings.Contains(configPath, "~") { homeDir, err := os.UserHomeDir() if err != nil { + // nolint:wrapcheck return "", err } @@ -171,6 +173,7 @@ func (fc *fileConfig) normalizePath(configPath string) (string, error) { absPath, err1 := filepath.Abs(configPath) if err1 != nil { + // nolint:wrapcheck return "", err1 } @@ -186,6 +189,7 @@ func (fc *fileConfig) Save() { data, err := json.Marshal(fc) if err != nil { cfgSaveLog.Fatal().Err(err).Msg("Failed to encode data into JSON. Configuration file won't be saved!") + return } diff --git a/internal/httpserver/checkallowedips.go b/internal/httpserver/checkallowedips.go index 927a594..bd77d37 100644 --- a/internal/httpserver/checkallowedips.go +++ b/internal/httpserver/checkallowedips.go @@ -1,26 +1,21 @@ package httpserver import ( - // stdlib "net" "net/http" "strings" - // local + "github.com/labstack/echo" "go.dev.pztrn.name/giredore/internal/configuration" "go.dev.pztrn.name/giredore/internal/structs" - - // other - "github.com/labstack/echo" ) func checkAllowedIPs() echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { - return func(ec echo.Context) error { + return func(ectx echo.Context) error { // Do nothing if request came not in "/_api" namespace. - if !strings.HasPrefix(ec.Request().RequestURI, "/_api") { - _ = next(ec) - return nil + if !strings.HasPrefix(ectx.Request().RequestURI, "/_api") { + return next(ectx) } // Get IPs and subnets from configuration and parse them @@ -39,7 +34,9 @@ func checkAllowedIPs() echo.MiddlewareFunc { _, net, err := net.ParseCIDR(ipToParse) if err != nil { log.Error().Err(err).Str("subnet", ipToParse).Msg("Failed to parse CIDR. /_api/ endpoint won't be accessible, this should be fixed manually in configuration file!") - return ec.JSON(http.StatusInternalServerError, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrInvalidAllowedIPDefined}}) + + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusInternalServerError, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrInvalidAllowedIPDefined}}) } subnets = append(subnets, net) @@ -47,23 +44,24 @@ func checkAllowedIPs() echo.MiddlewareFunc { // Check if requester's IP address are within allowed IP // subnets. - ipToCheck := net.ParseIP(ec.RealIP()) + ipToCheck := net.ParseIP(ectx.RealIP()) var allowed bool for _, subnet := range subnets { if subnet.Contains(ipToCheck) { allowed = true + break } } if allowed { - _ = next(ec) - return nil + return next(ectx) } - return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrIPAddressNotAllowed}}) + // nolint:exhaustivestruct,wrapcheck + return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrIPAddressNotAllowed}}) } } } diff --git a/internal/httpserver/exported.go b/internal/httpserver/exported.go index 858b4b7..e9e84b0 100644 --- a/internal/httpserver/exported.go +++ b/internal/httpserver/exported.go @@ -1,21 +1,17 @@ package httpserver import ( - // stdlib "context" "io/ioutil" "net/http" "strings" "time" - // local - "go.dev.pztrn.name/giredore/internal/configuration" - "go.dev.pztrn.name/giredore/internal/logger" - - // other "github.com/labstack/echo" "github.com/labstack/echo/middleware" "github.com/rs/zerolog" + "go.dev.pztrn.name/giredore/internal/configuration" + "go.dev.pztrn.name/giredore/internal/logger" ) var ( @@ -66,6 +62,7 @@ func Start() { }() // Check that HTTP server was started. + // nolint:exhaustivestruct httpc := &http.Client{Timeout: time.Second * 1} checks := 0 @@ -78,9 +75,17 @@ func Start() { time.Sleep(time.Second * 1) - resp, err := httpc.Get("http://" + configuration.Cfg.HTTP.Listen + "/_internal/waitForOnline") + localCtx, cancelFunc := context.WithTimeout(context.Background(), time.Second*1) + + req, err := http.NewRequestWithContext(localCtx, "GET", "http://"+configuration.Cfg.HTTP.Listen+"/_internal/waitForOnline", nil) + if err != nil { + log.Panic().Err(err).Msg("Failed to create HTTP request!") + } + + resp, err := httpc.Do(req) if err != nil { log.Debug().Err(err).Msg("HTTP error occurred, HTTP server isn't ready, waiting...") + continue } @@ -89,6 +94,7 @@ func Start() { if err != nil { log.Debug().Err(err).Msg("Failed to read response body, HTTP server isn't ready, waiting...") + continue } @@ -97,23 +103,29 @@ func Start() { if resp.StatusCode == http.StatusOK { if len(response) == 0 { log.Debug().Msg("Response is empty, HTTP server isn't ready, waiting...") + continue } log.Debug().Int("status code", resp.StatusCode).Msgf("Response: %+v", string(response)) if len(response) == 17 { + // This is useless context cancel function call. Thanks to lostcancel linter. + cancelFunc() + break } } } + log.Info().Msg("HTTP server is ready to process requests") } -func waitForHTTPServerToBeUpHandler(ec echo.Context) error { +func waitForHTTPServerToBeUpHandler(ectx echo.Context) error { response := map[string]string{ "error": "None", } - return ec.JSON(200, response) + // nolint:wrapcheck + return ectx.JSON(200, response) } diff --git a/internal/httpserver/requestlogger.go b/internal/httpserver/requestlogger.go index a227aaa..fad24cf 100644 --- a/internal/httpserver/requestlogger.go +++ b/internal/httpserver/requestlogger.go @@ -1,8 +1,6 @@ package httpserver import ( - // other - "time" "github.com/labstack/echo" @@ -10,18 +8,18 @@ import ( func requestLogger() echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { - return func(ec echo.Context) error { + return func(ectx echo.Context) error { startTime := time.Now() - err := next(ec) + err := next(ectx) log.Info(). - Str("From", ec.RealIP()). - Str("To", ec.Request().Host). - Str("Method", ec.Request().Method). - Str("Path", ec.Request().URL.Path). - Int64("Length", ec.Request().ContentLength). - Str("UA", ec.Request().UserAgent()). + Str("From", ectx.RealIP()). + Str("To", ectx.Request().Host). + Str("Method", ectx.Request().Method). + Str("Path", ectx.Request().URL.Path). + Int64("Length", ectx.Request().ContentLength). + Str("UA", ectx.Request().UserAgent()). TimeDiff("TimeMS", time.Now(), startTime). Msg("HTTP request") diff --git a/internal/httpserver/strictjson.go b/internal/httpserver/strictjson.go index 7574b93..4e18d19 100644 --- a/internal/httpserver/strictjson.go +++ b/internal/httpserver/strictjson.go @@ -1,12 +1,10 @@ package httpserver import ( - // stdlib "encoding/json" "fmt" "net/http" - // other "github.com/labstack/echo" ) @@ -15,7 +13,7 @@ import ( type StrictJSONBinder struct{} // Bind parses JSON input. -func (sjb *StrictJSONBinder) Bind(i interface{}, c echo.Context) error { +func (sjb *StrictJSONBinder) Bind(data interface{}, c echo.Context) error { req := c.Request() if req.ContentLength == 0 { return echo.NewHTTPError(http.StatusBadRequest, "Request body can't be empty") @@ -25,7 +23,9 @@ func (sjb *StrictJSONBinder) Bind(i interface{}, c echo.Context) error { decoder := json.NewDecoder(req.Body) decoder.DisallowUnknownFields() - if err := decoder.Decode(i); err != nil { + // ToDo: rework this code. + // nolint:errorlint + if err := decoder.Decode(data); err != nil { if ute, ok := err.(*json.UnmarshalTypeError); ok { return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, field=%v, offset=%v", ute.Type, ute.Value, ute.Field, ute.Offset)) } else if se, ok := err.(*json.SyntaxError); ok { diff --git a/internal/logger/exported.go b/internal/logger/exported.go index 6889d05..8b26412 100644 --- a/internal/logger/exported.go +++ b/internal/logger/exported.go @@ -1,13 +1,11 @@ package logger import ( - // stdlib "fmt" "os" "strings" "time" - // other "github.com/rs/zerolog" ) @@ -17,6 +15,7 @@ var ( ) // Initialize initializes zerolog with proper formatting and log level. +// nolint:forbidigo func Initialize() { // Check environment for logger level. // Defaulting to INFO. @@ -45,27 +44,28 @@ func Initialize() { zerolog.SetGlobalLevel(zerolog.InfoLevel) } + // nolint:exhaustivestruct output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339} - output.FormatLevel = func(i interface{}) string { + output.FormatLevel = func(lvlRaw interface{}) string { var v string - if ii, ok := i.(string); ok { - ii = strings.ToUpper(ii) - switch ii { + if lvl, ok := lvlRaw.(string); ok { + lvl = strings.ToUpper(lvl) + switch lvl { case "DEBUG": - v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", ii) + v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvl) case "ERROR": - v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", ii) + v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvl) case "FATAL": - v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", ii) + v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvl) case "INFO": - v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", ii) + v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvl) case "PANIC": - v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", ii) + v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvl) case "WARN": - v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", ii) + v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvl) default: - v = ii + v = lvl } } diff --git a/internal/requester/exported.go b/internal/requester/exported.go index 9999932..4bba245 100644 --- a/internal/requester/exported.go +++ b/internal/requester/exported.go @@ -1,22 +1,17 @@ package requester import ( - // stdlib "bytes" + "context" "encoding/json" "io/ioutil" "net/http" - // local - "go.dev.pztrn.name/giredore/internal/logger" - - // other "github.com/rs/zerolog" + "go.dev.pztrn.name/giredore/internal/logger" ) -var ( - log zerolog.Logger -) +var log zerolog.Logger func Initialize() { log = logger.Logger.With().Str("type", "internal").Str("package", "requester").Logger() @@ -27,6 +22,7 @@ func Delete(url string, data interface{}) ([]byte, error) { return execRequest("DELETE", url, data) } +// nolint:wrapcheck func execRequest(method string, url string, data interface{}) ([]byte, error) { log.Debug().Str("method", method).Str("URL", url).Msg("Trying to execute HTTP request...") @@ -38,7 +34,7 @@ func execRequest(method string, url string, data interface{}) ([]byte, error) { } // Compose HTTP request. - httpReq, err := http.NewRequest(method, url, bytes.NewReader(dataToSend)) + httpReq, err := http.NewRequestWithContext(context.Background(), method, url, bytes.NewReader(dataToSend)) if err != nil { return nil, err } diff --git a/internal/requester/httpclient.go b/internal/requester/httpclient.go index e0283db..fb42552 100644 --- a/internal/requester/httpclient.go +++ b/internal/requester/httpclient.go @@ -1,14 +1,14 @@ package requester import ( - // stdlib "net" "net/http" "time" ) +// nolint:exhaustivestruct func getHTTPClient() *http.Client { - c := &http.Client{ + client := &http.Client{ Transport: &http.Transport{ ExpectContinueTimeout: time.Second * 5, DialContext: (&net.Dialer{ @@ -19,5 +19,5 @@ func getHTTPClient() *http.Client { }, } - return c + return client }