Linter and Drone config fixes, code linting.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
// other
|
||||
"github.com/vrischmann/envconfig"
|
||||
)
|
||||
|
||||
|
@@ -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()
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
@@ -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}})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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")
|
||||
|
||||
|
@@ -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 {
|
||||
|
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user