Linter and Drone config fixes, code linting.

This commit is contained in:
Stanislav Nikitin 2021-11-20 23:06:40 +05:00
parent 9c045e9fd3
commit d5fcc5cef9
Signed by: pztrn
GPG Key ID: 1E944A0F0568B550
22 changed files with 152 additions and 146 deletions

View File

@ -21,8 +21,7 @@ steps:
- name: docker - name: docker
image: plugins/docker image: plugins/docker
when: when:
branch: branch: ["master"]
master
settings: settings:
username: username:
from_secret: dockerhub_user from_secret: dockerhub_user

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*DS_Store* *DS_Store*
data/* data/*
.idea .idea
.vscode

View File

@ -15,6 +15,12 @@ linters:
linters-settings: linters-settings:
lll: lll:
line-length: 420 line-length: 420
gocyclo: cyclop:
min-complexity: 40 max-complexity: 25
issues:
exclude-rules:
# There will be some ToDos.
- linters:
- godox
text: "TODO"

View File

@ -1,15 +1,11 @@
package main package main
import ( import (
// stdlib
"os" "os"
// local "github.com/teris-io/cli"
clientv1 "go.dev.pztrn.name/giredore/domains/client/v1" clientv1 "go.dev.pztrn.name/giredore/domains/client/v1"
"go.dev.pztrn.name/giredore/internal/logger" "go.dev.pztrn.name/giredore/internal/logger"
// other
"github.com/teris-io/cli"
) )
func main() { func main() {
@ -34,6 +30,7 @@ func main() {
logger.Initialize() logger.Initialize()
clientv1.Initialize() clientv1.Initialize()
clientv1.SetAllowedIPs(args, options) clientv1.SetAllowedIPs(args, options)
return 0 return 0
}), }),
), ),

View File

@ -1,12 +1,10 @@
package main package main
import ( import (
// stdlib
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
// local
serverv1 "go.dev.pztrn.name/giredore/domains/server/v1" serverv1 "go.dev.pztrn.name/giredore/domains/server/v1"
"go.dev.pztrn.name/giredore/internal/configuration" "go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/httpserver" "go.dev.pztrn.name/giredore/internal/httpserver"

View File

@ -1,10 +1,8 @@
package clientv1 package clientv1
import ( import (
// stdlib
"strings" "strings"
// local
"go.dev.pztrn.name/giredore/internal/requester" "go.dev.pztrn.name/giredore/internal/requester"
"go.dev.pztrn.name/giredore/internal/structs" "go.dev.pztrn.name/giredore/internal/structs"
) )

View File

@ -1,17 +1,12 @@
package clientv1 package clientv1
import ( import (
// local "github.com/rs/zerolog"
"go.dev.pztrn.name/giredore/internal/logger" "go.dev.pztrn.name/giredore/internal/logger"
"go.dev.pztrn.name/giredore/internal/requester" "go.dev.pztrn.name/giredore/internal/requester"
// other
"github.com/rs/zerolog"
) )
var ( var log zerolog.Logger
log zerolog.Logger
)
func Initialize() { func Initialize() {
log = logger.Logger.With().Str("type", "domain").Str("package", "client").Int("version", 1).Logger() log = logger.Logger.With().Str("type", "domain").Str("package", "client").Int("version", 1).Logger()

View File

@ -1,10 +1,8 @@
package clientv1 package clientv1
import ( import (
// stdlib
"strings" "strings"
// local
"go.dev.pztrn.name/giredore/internal/requester" "go.dev.pztrn.name/giredore/internal/requester"
"go.dev.pztrn.name/giredore/internal/structs" "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) { func GetPackages(args []string, options map[string]string) {
pkgs := strings.Split(args[0], ",") pkgs := strings.Split(args[0], ",")
// nolint:exhaustivestruct
req := &structs.PackageGetRequest{} req := &structs.PackageGetRequest{}
if pkgs[0] == "all" { if pkgs[0] == "all" {
req.All = true req.All = true

View File

@ -1,32 +1,32 @@
package serverv1 package serverv1
import ( import (
// stdlib
"net/http" "net/http"
// local "github.com/labstack/echo"
"go.dev.pztrn.name/giredore/internal/configuration" "go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/structs" "go.dev.pztrn.name/giredore/internal/structs"
// other
"github.com/labstack/echo"
) )
// This function responsible for getting runtime configuration. // This function responsible for getting runtime configuration.
func configurationGET(ec echo.Context) error { func configurationGET(ec echo.Context) error {
// nolint:wrapcheck
return ec.JSON(http.StatusOK, configuration.Cfg) return ec.JSON(http.StatusOK, configuration.Cfg)
} }
func configurationAllowedIPsSET(ec echo.Context) error { func configurationAllowedIPsSET(ectx echo.Context) error {
// nolint:exhaustivestruct
req := &structs.AllowedIPsSetRequest{} 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") 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) log.Debug().Msgf("Got set allowed IPs request: %+v", req)
configuration.Cfg.SetAllowedIPs(req.AllowedIPs) 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})
} }

View File

@ -1,17 +1,12 @@
package serverv1 package serverv1
import ( import (
// local "github.com/rs/zerolog"
"go.dev.pztrn.name/giredore/internal/httpserver" "go.dev.pztrn.name/giredore/internal/httpserver"
"go.dev.pztrn.name/giredore/internal/logger" "go.dev.pztrn.name/giredore/internal/logger"
// other
"github.com/rs/zerolog"
) )
var ( var log zerolog.Logger
log zerolog.Logger
)
func Initialize() { func Initialize() {
log = logger.Logger.With().Str("type", "domain").Str("package", "server").Int("version", 1).Logger() log = logger.Logger.With().Str("type", "domain").Str("package", "server").Int("version", 1).Logger()

View File

@ -1,43 +1,43 @@
package serverv1 package serverv1
import ( import (
// stdlib
"net/http" "net/http"
"strings" "strings"
// local "github.com/labstack/echo"
"go.dev.pztrn.name/giredore/internal/configuration" "go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/structs" "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 // Getting real path. This might be the package itself, or namespace
// to list available packages. // to list available packages.
// For now only package itself is supported, all other features in ToDo. // 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}) pkgs, errs := configuration.Cfg.GetPackagesInfo([]string{packageNameRaw})
if errs != nil { if errs != nil {
log.Error().Str("package", packageNameRaw).Msgf("Failed to get package information: %+v", errs) 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 { 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] pkg, found := pkgs[packageNameRaw]
if !found { 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 // We should compose package name using our domain under which giredore
// is working. // is working.
domain := ec.Request().Host domain := ectx.Request().Host
packageName := domain + packageNameRaw packageName := domain + packageNameRaw
tmpl := singlePackageTemplate tmpl := singlePackageTemplate
@ -45,5 +45,6 @@ func throwGoImports(ec echo.Context) error {
tmpl = strings.Replace(tmpl, "{VCS}", pkg.VCS, 1) tmpl = strings.Replace(tmpl, "{VCS}", pkg.VCS, 1)
tmpl = strings.Replace(tmpl, "{REPOPATH}", pkg.RealPath, 1) tmpl = strings.Replace(tmpl, "{REPOPATH}", pkg.RealPath, 1)
return ec.HTML(http.StatusOK, tmpl) // nolint:wrapcheck
return ectx.HTML(http.StatusOK, tmpl)
} }

View File

@ -1,24 +1,23 @@
package serverv1 package serverv1
import ( import (
// stdlib
"net/http" "net/http"
"strings" "strings"
// local "github.com/labstack/echo"
"go.dev.pztrn.name/giredore/internal/configuration" "go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/structs" "go.dev.pztrn.name/giredore/internal/structs"
// other
"github.com/labstack/echo"
) )
// This function responsible for getting packages configuration. // This function responsible for getting packages configuration.
func packagesGET(ec echo.Context) error { func packagesGET(ectx echo.Context) error {
// nolint:exhaustivestruct
req := &structs.PackageGetRequest{} 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") 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) 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 { 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. // This function responsible for deleting package.
func packagesDELETE(ec echo.Context) error { func packagesDELETE(ectx echo.Context) error {
// nolint:exhaustivestruct
req := &structs.PackageDeleteRequest{} 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") 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) log.Info().Msgf("Received package delete request: %+v", req)
@ -53,28 +57,35 @@ func packagesDELETE(ec echo.Context) error {
errs := configuration.Cfg.DeletePackage(req) errs := configuration.Cfg.DeletePackage(req)
if len(errs) > 0 { 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. // 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{} 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") 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) log.Info().Msgf("Received package set/update request: %+v", req)
// Validate passed package data. // Validate passed package data.
if !strings.HasPrefix(req.OriginalPath, "/") { 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) 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})
} }

View File

@ -1,7 +1,6 @@
package configuration package configuration
import ( import (
// other
"github.com/vrischmann/envconfig" "github.com/vrischmann/envconfig"
) )

View File

@ -1,11 +1,8 @@
package configuration package configuration
import ( import (
// local
"go.dev.pztrn.name/giredore/internal/logger"
// other
"github.com/rs/zerolog" "github.com/rs/zerolog"
"go.dev.pztrn.name/giredore/internal/logger"
) )
var ( var (
@ -19,9 +16,11 @@ func Initialize() {
log = logger.Logger.With().Str("type", "internal").Str("package", "configuration").Logger() log = logger.Logger.With().Str("type", "internal").Str("package", "configuration").Logger()
log.Info().Msg("Initializing...") log.Info().Msg("Initializing...")
// nolint:exhaustivestruct
envCfg = &envConfig{} envCfg = &envConfig{}
envCfg.Initialize() envCfg.Initialize()
// nolint:exhaustivestruct
Cfg = &fileConfig{} Cfg = &fileConfig{}
Cfg.Initialize() Cfg.Initialize()

View File

@ -1,7 +1,6 @@
package configuration package configuration
import ( import (
// stdlib
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
@ -9,7 +8,6 @@ import (
"strings" "strings"
"sync" "sync"
// local
"go.dev.pztrn.name/giredore/internal/structs" "go.dev.pztrn.name/giredore/internal/structs"
) )
@ -19,22 +17,22 @@ import (
// may be accesses concurrently. In other words DO NOT USE EXPORTED FIELDS // may be accesses concurrently. In other words DO NOT USE EXPORTED FIELDS
// DIRECTLY! // DIRECTLY!
type fileConfig struct { type fileConfig struct {
packagesMutex sync.RWMutex
// Packages describes packages mapping.
Packages map[string]*structs.Package
// HTTP describes HTTP server configuration. // HTTP describes HTTP server configuration.
HTTP struct { 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 allowedipsmutex sync.RWMutex
// Listen is an address on which HTTP server will listen. // Listen is an address on which HTTP server will listen.
Listen string 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 // WaitForSeconds is a timeout during which we will wait for
// HTTP server be up. If timeout will pass and HTTP server won't // HTTP server be up. If timeout will pass and HTTP server won't
// start processing requests - giredore will exit. // start processing requests - giredore will exit.
WaitForSeconds int WaitForSeconds int
} }
// Packages describes packages mapping.
Packages map[string]*structs.Package
packagesMutex sync.RWMutex
} }
func (fc *fileConfig) AddOrUpdatePackage(pkg *structs.Package) { func (fc *fileConfig) AddOrUpdatePackage(pkg *structs.Package) {
@ -52,6 +50,7 @@ func (fc *fileConfig) DeletePackage(req *structs.PackageDeleteRequest) []structs
if !found { if !found {
errors = append(errors, structs.ErrPackageWasntDefined) errors = append(errors, structs.ErrPackageWasntDefined)
return errors return errors
} }
@ -116,6 +115,7 @@ func (fc *fileConfig) Initialize() {
// exists. // exists.
if _, err2 := os.Stat(configPath); os.IsNotExist(err2) { if _, err2 := os.Stat(configPath); os.IsNotExist(err2) {
cfgLoadLog.Error().Msg("Unable to load configuration from filesystem.") cfgLoadLog.Error().Msg("Unable to load configuration from filesystem.")
return return
} }
@ -141,6 +141,7 @@ func (fc *fileConfig) Initialize() {
for _, ip := range fc.HTTP.AllowedIPs { for _, ip := range fc.HTTP.AllowedIPs {
if strings.Contains(ip, "127.0.0.1") { if strings.Contains(ip, "127.0.0.1") {
localhostIsAllowed = true localhostIsAllowed = true
break break
} }
} }
@ -163,6 +164,7 @@ func (fc *fileConfig) normalizePath(configPath string) (string, error) {
if strings.Contains(configPath, "~") { if strings.Contains(configPath, "~") {
homeDir, err := os.UserHomeDir() homeDir, err := os.UserHomeDir()
if err != nil { if err != nil {
// nolint:wrapcheck
return "", err return "", err
} }
@ -171,6 +173,7 @@ func (fc *fileConfig) normalizePath(configPath string) (string, error) {
absPath, err1 := filepath.Abs(configPath) absPath, err1 := filepath.Abs(configPath)
if err1 != nil { if err1 != nil {
// nolint:wrapcheck
return "", err1 return "", err1
} }
@ -186,6 +189,7 @@ func (fc *fileConfig) Save() {
data, err := json.Marshal(fc) data, err := json.Marshal(fc)
if err != nil { if err != nil {
cfgSaveLog.Fatal().Err(err).Msg("Failed to encode data into JSON. Configuration file won't be saved!") cfgSaveLog.Fatal().Err(err).Msg("Failed to encode data into JSON. Configuration file won't be saved!")
return return
} }

View File

@ -1,26 +1,21 @@
package httpserver package httpserver
import ( import (
// stdlib
"net" "net"
"net/http" "net/http"
"strings" "strings"
// local "github.com/labstack/echo"
"go.dev.pztrn.name/giredore/internal/configuration" "go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/structs" "go.dev.pztrn.name/giredore/internal/structs"
// other
"github.com/labstack/echo"
) )
func checkAllowedIPs() echo.MiddlewareFunc { func checkAllowedIPs() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc { 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. // Do nothing if request came not in "/_api" namespace.
if !strings.HasPrefix(ec.Request().RequestURI, "/_api") { if !strings.HasPrefix(ectx.Request().RequestURI, "/_api") {
_ = next(ec) return next(ectx)
return nil
} }
// Get IPs and subnets from configuration and parse them // Get IPs and subnets from configuration and parse them
@ -39,7 +34,9 @@ func checkAllowedIPs() echo.MiddlewareFunc {
_, net, err := net.ParseCIDR(ipToParse) _, net, err := net.ParseCIDR(ipToParse)
if err != nil { 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!") 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) subnets = append(subnets, net)
@ -47,23 +44,24 @@ func checkAllowedIPs() echo.MiddlewareFunc {
// Check if requester's IP address are within allowed IP // Check if requester's IP address are within allowed IP
// subnets. // subnets.
ipToCheck := net.ParseIP(ec.RealIP()) ipToCheck := net.ParseIP(ectx.RealIP())
var allowed bool var allowed bool
for _, subnet := range subnets { for _, subnet := range subnets {
if subnet.Contains(ipToCheck) { if subnet.Contains(ipToCheck) {
allowed = true allowed = true
break break
} }
} }
if allowed { if allowed {
_ = next(ec) return next(ectx)
return nil
} }
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}})
} }
} }
} }

View File

@ -1,21 +1,17 @@
package httpserver package httpserver
import ( import (
// stdlib
"context" "context"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strings" "strings"
"time" "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"
"github.com/labstack/echo/middleware" "github.com/labstack/echo/middleware"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/logger"
) )
var ( var (
@ -66,6 +62,7 @@ func Start() {
}() }()
// Check that HTTP server was started. // Check that HTTP server was started.
// nolint:exhaustivestruct
httpc := &http.Client{Timeout: time.Second * 1} httpc := &http.Client{Timeout: time.Second * 1}
checks := 0 checks := 0
@ -78,9 +75,17 @@ func Start() {
time.Sleep(time.Second * 1) 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 { if err != nil {
log.Debug().Err(err).Msg("HTTP error occurred, HTTP server isn't ready, waiting...") log.Debug().Err(err).Msg("HTTP error occurred, HTTP server isn't ready, waiting...")
continue continue
} }
@ -89,6 +94,7 @@ func Start() {
if err != nil { if err != nil {
log.Debug().Err(err).Msg("Failed to read response body, HTTP server isn't ready, waiting...") log.Debug().Err(err).Msg("Failed to read response body, HTTP server isn't ready, waiting...")
continue continue
} }
@ -97,23 +103,29 @@ func Start() {
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
if len(response) == 0 { if len(response) == 0 {
log.Debug().Msg("Response is empty, HTTP server isn't ready, waiting...") log.Debug().Msg("Response is empty, HTTP server isn't ready, waiting...")
continue continue
} }
log.Debug().Int("status code", resp.StatusCode).Msgf("Response: %+v", string(response)) log.Debug().Int("status code", resp.StatusCode).Msgf("Response: %+v", string(response))
if len(response) == 17 { if len(response) == 17 {
// This is useless context cancel function call. Thanks to lostcancel linter.
cancelFunc()
break break
} }
} }
} }
log.Info().Msg("HTTP server is ready to process requests") 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{ response := map[string]string{
"error": "None", "error": "None",
} }
return ec.JSON(200, response) // nolint:wrapcheck
return ectx.JSON(200, response)
} }

View File

@ -1,8 +1,6 @@
package httpserver package httpserver
import ( import (
// other
"time" "time"
"github.com/labstack/echo" "github.com/labstack/echo"
@ -10,18 +8,18 @@ import (
func requestLogger() echo.MiddlewareFunc { func requestLogger() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc { return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ec echo.Context) error { return func(ectx echo.Context) error {
startTime := time.Now() startTime := time.Now()
err := next(ec) err := next(ectx)
log.Info(). log.Info().
Str("From", ec.RealIP()). Str("From", ectx.RealIP()).
Str("To", ec.Request().Host). Str("To", ectx.Request().Host).
Str("Method", ec.Request().Method). Str("Method", ectx.Request().Method).
Str("Path", ec.Request().URL.Path). Str("Path", ectx.Request().URL.Path).
Int64("Length", ec.Request().ContentLength). Int64("Length", ectx.Request().ContentLength).
Str("UA", ec.Request().UserAgent()). Str("UA", ectx.Request().UserAgent()).
TimeDiff("TimeMS", time.Now(), startTime). TimeDiff("TimeMS", time.Now(), startTime).
Msg("HTTP request") Msg("HTTP request")

View File

@ -1,12 +1,10 @@
package httpserver package httpserver
import ( import (
// stdlib
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
// other
"github.com/labstack/echo" "github.com/labstack/echo"
) )
@ -15,7 +13,7 @@ import (
type StrictJSONBinder struct{} type StrictJSONBinder struct{}
// Bind parses JSON input. // 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() req := c.Request()
if req.ContentLength == 0 { if req.ContentLength == 0 {
return echo.NewHTTPError(http.StatusBadRequest, "Request body can't be empty") 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 := json.NewDecoder(req.Body)
decoder.DisallowUnknownFields() 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 { 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)) 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 { } else if se, ok := err.(*json.SyntaxError); ok {

View File

@ -1,13 +1,11 @@
package logger package logger
import ( import (
// stdlib
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time" "time"
// other
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
@ -17,6 +15,7 @@ var (
) )
// Initialize initializes zerolog with proper formatting and log level. // Initialize initializes zerolog with proper formatting and log level.
// nolint:forbidigo
func Initialize() { func Initialize() {
// Check environment for logger level. // Check environment for logger level.
// Defaulting to INFO. // Defaulting to INFO.
@ -45,27 +44,28 @@ func Initialize() {
zerolog.SetGlobalLevel(zerolog.InfoLevel) zerolog.SetGlobalLevel(zerolog.InfoLevel)
} }
// nolint:exhaustivestruct
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339} 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 var v string
if ii, ok := i.(string); ok { if lvl, ok := lvlRaw.(string); ok {
ii = strings.ToUpper(ii) lvl = strings.ToUpper(lvl)
switch ii { switch lvl {
case "DEBUG": case "DEBUG":
v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", ii) v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvl)
case "ERROR": case "ERROR":
v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", ii) v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvl)
case "FATAL": case "FATAL":
v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", ii) v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvl)
case "INFO": case "INFO":
v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", ii) v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvl)
case "PANIC": case "PANIC":
v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", ii) v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvl)
case "WARN": case "WARN":
v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", ii) v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvl)
default: default:
v = ii v = lvl
} }
} }

View File

@ -1,22 +1,17 @@
package requester package requester
import ( import (
// stdlib
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
// local
"go.dev.pztrn.name/giredore/internal/logger"
// other
"github.com/rs/zerolog" "github.com/rs/zerolog"
"go.dev.pztrn.name/giredore/internal/logger"
) )
var ( var log zerolog.Logger
log zerolog.Logger
)
func Initialize() { func Initialize() {
log = logger.Logger.With().Str("type", "internal").Str("package", "requester").Logger() 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) return execRequest("DELETE", url, data)
} }
// nolint:wrapcheck
func execRequest(method string, url string, data interface{}) ([]byte, error) { func execRequest(method string, url string, data interface{}) ([]byte, error) {
log.Debug().Str("method", method).Str("URL", url).Msg("Trying to execute HTTP request...") 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. // 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,14 +1,14 @@
package requester package requester
import ( import (
// stdlib
"net" "net"
"net/http" "net/http"
"time" "time"
) )
// nolint:exhaustivestruct
func getHTTPClient() *http.Client { func getHTTPClient() *http.Client {
c := &http.Client{ client := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
ExpectContinueTimeout: time.Second * 5, ExpectContinueTimeout: time.Second * 5,
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
@ -19,5 +19,5 @@ func getHTTPClient() *http.Client {
}, },
} }
return c return client
} }