From db590a2cfcee43e44f0e234a4f7fa16b8d416da1 Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Fri, 15 Nov 2024 08:57:27 +0100 Subject: [PATCH] refactor: clean up code to pass go lint test --- internal/config.go | 2 -- internal/handler.go | 4 ++-- internal/healthCheck.go | 1 + internal/middleware.go | 6 +----- internal/middleware/middleware.go | 2 +- internal/middleware/types.go | 14 +++++++------- internal/server_test.go | 6 +++--- internal/types.go | 22 +++++----------------- internal/var.go | 5 ----- pkg/logger/logger.go | 5 ----- 10 files changed, 20 insertions(+), 47 deletions(-) diff --git a/internal/config.go b/internal/config.go index 84a34e9..72ad4ff 100644 --- a/internal/config.go +++ b/internal/config.go @@ -30,8 +30,6 @@ import ( "os" ) -var cfg *Gateway - // Config reads config file and returns Gateway func (GatewayServer) Config(configFile string) (*GatewayServer, error) { if util.FileExists(configFile) { diff --git a/internal/handler.go b/internal/handler.go index 107b4bf..5d53df1 100644 --- a/internal/handler.go +++ b/internal/handler.go @@ -61,7 +61,6 @@ func ProxyErrorHandler(w http.ResponseWriter, r *http.Request, err error) { if err != nil { return } - return } // HealthCheckHandler handles health check of routes @@ -77,8 +76,9 @@ func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r * if err != nil { if heathRoute.DisableRouteHealthCheckError { routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "unhealthy", Error: "Route healthcheck errors disabled"}) + } else { + routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "unhealthy", Error: "Error: " + err.Error()}) } - routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "unhealthy", Error: "Error: " + err.Error()}) } else { logger.Debug("Route %s is healthy", health.Name) routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "healthy", Error: ""}) diff --git a/internal/healthCheck.go b/internal/healthCheck.go index 90b9652..406ad50 100644 --- a/internal/healthCheck.go +++ b/internal/healthCheck.go @@ -53,6 +53,7 @@ func (health Health) Check() error { defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { + logger.Debug("Error performing HealthCheck request: %v ", err) } }(healthResp.Body) if len(health.HealthyStatuses) > 0 { diff --git a/internal/middleware.go b/internal/middleware.go index 3ee792a..aa7b146 100644 --- a/internal/middleware.go +++ b/internal/middleware.go @@ -19,11 +19,7 @@ func getMiddleware(rules []string, middlewares []Middleware) (Middleware, error) func doesExist(tyName string) bool { middlewareList := []string{BasicAuth, JWTAuth, AccessMiddleware} - if slices.Contains(middlewareList, tyName) { - return true - - } - return false + return slices.Contains(middlewareList, tyName) } func GetMiddleware(rule string, middlewares []Middleware) (Middleware, error) { for _, m := range middlewares { diff --git a/internal/middleware/middleware.go b/internal/middleware/middleware.go index c2e6461..985bb95 100644 --- a/internal/middleware/middleware.go +++ b/internal/middleware/middleware.go @@ -79,7 +79,7 @@ func (jwtAuth JwtAuth) AuthMiddleware(next http.Handler) http.Handler { defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { - + logger.Error("Error closing body: %v", err) } }(authResp.Body) // Inject specific header tp the current request's header diff --git a/internal/middleware/types.go b/internal/middleware/types.go index ad3d613..27d1cda 100644 --- a/internal/middleware/types.go +++ b/internal/middleware/types.go @@ -26,13 +26,13 @@ import ( // RateLimiter defines requests limit properties. type RateLimiter struct { - requests int - id string - window time.Duration - clientMap map[string]*Client - mu sync.Mutex - origins []string - hosts []string + requests int + id string + window time.Duration + clientMap map[string]*Client + mu sync.Mutex + origins []string + //hosts []string redisBased bool } diff --git a/internal/server_test.go b/internal/server_test.go index d91e8fd..da298e9 100644 --- a/internal/server_test.go +++ b/internal/server_test.go @@ -24,11 +24,11 @@ func TestCheckConfig(t *testing.T) { TestInit(t) err := initConfig(configFile) if err != nil { - t.Fatalf(err.Error()) + t.Fatal("Error init config:", err) } err = CheckConfig(configFile) if err != nil { - t.Fatalf(err.Error()) + t.Fatalf("Error checking config: %s", err.Error()) } log.Println("Goma Gateway configuration file checked successfully") } @@ -37,7 +37,7 @@ func TestStart(t *testing.T) { TestInit(t) err := initConfig(configFile) if err != nil { - t.Fatalf(err.Error()) + t.Fatalf("Error initializing config: %s", err.Error()) } g := GatewayServer{} gatewayServer, err := g.Config(configFile) diff --git a/internal/types.go b/internal/types.go index 56caff7..e358156 100644 --- a/internal/types.go +++ b/internal/types.go @@ -19,13 +19,9 @@ package pkg import ( "context" - "github.com/gorilla/mux" "time" ) -type Config struct { - file string -} type BasicRuleMiddleware struct { Username string `yaml:"username"` Password string `yaml:"password"` @@ -122,24 +118,16 @@ type GatewayServer struct { middlewares []Middleware } type ProxyRoute struct { - path string - rewrite string - destination string - backends []string - healthCheck RouteHealthCheck + path string + rewrite string + destination string + backends []string + //healthCheck RouteHealthCheck methods []string cors Cors disableHostFording bool insecureSkipVerify bool } -type RoutePath struct { - route Route - path string - rules []string - middlewares []Middleware - router *mux.Router -} - type HealthCheckRoute struct { DisableRouteHealthCheckError bool Routes []Route diff --git a/internal/var.go b/internal/var.go index b57bb0d..b4ded35 100644 --- a/internal/var.go +++ b/internal/var.go @@ -3,16 +3,11 @@ package pkg const ConfigDir = "/etc/goma/" // Default configuration file const ConfigFile = "/etc/goma/goma.yml" // Default configuration file const accessControlAllowOrigin = "Access-Control-Allow-Origin" // Cors -const serverName = "Goma" const gatewayName = "Goma Gateway" const AccessMiddleware = "access" // access middleware const BasicAuth = "basic" // basic authentication middleware const JWTAuth = "jwt" // JWT authentication middleware const OAuth = "oauth" // OAuth authentication middleware -const applicationJson = "application/json" -const textPlain = "text/plain" -const applicationXml = "application/xml" - // Round-robin counter var counter uint32 diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index b9e18b8..3ca6d3b 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -26,11 +26,6 @@ import ( "github.com/jkaninda/goma-gateway/util" ) -type Logger struct { - msg string - args interface{} -} - // Info returns info log func Info(msg string, args ...interface{}) { log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout")))