refactor: clean up code to pass go lint test

This commit is contained in:
Jonas Kaninda
2024-11-15 08:57:27 +01:00
parent 28b7643f0c
commit db590a2cfc
10 changed files with 20 additions and 47 deletions

View File

@@ -30,8 +30,6 @@ import (
"os" "os"
) )
var cfg *Gateway
// Config reads config file and returns Gateway // Config reads config file and returns Gateway
func (GatewayServer) Config(configFile string) (*GatewayServer, error) { func (GatewayServer) Config(configFile string) (*GatewayServer, error) {
if util.FileExists(configFile) { if util.FileExists(configFile) {

View File

@@ -61,7 +61,6 @@ func ProxyErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
if err != nil { if err != nil {
return return
} }
return
} }
// HealthCheckHandler handles health check of routes // HealthCheckHandler handles health check of routes
@@ -77,8 +76,9 @@ func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r *
if err != nil { if err != nil {
if heathRoute.DisableRouteHealthCheckError { if heathRoute.DisableRouteHealthCheckError {
routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "unhealthy", Error: "Route healthcheck errors disabled"}) 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 { } else {
logger.Debug("Route %s is healthy", health.Name) logger.Debug("Route %s is healthy", health.Name)
routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "healthy", Error: ""}) routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "healthy", Error: ""})

View File

@@ -53,6 +53,7 @@ func (health Health) Check() error {
defer func(Body io.ReadCloser) { defer func(Body io.ReadCloser) {
err := Body.Close() err := Body.Close()
if err != nil { if err != nil {
logger.Debug("Error performing HealthCheck request: %v ", err)
} }
}(healthResp.Body) }(healthResp.Body)
if len(health.HealthyStatuses) > 0 { if len(health.HealthyStatuses) > 0 {

View File

@@ -19,11 +19,7 @@ func getMiddleware(rules []string, middlewares []Middleware) (Middleware, error)
func doesExist(tyName string) bool { func doesExist(tyName string) bool {
middlewareList := []string{BasicAuth, JWTAuth, AccessMiddleware} middlewareList := []string{BasicAuth, JWTAuth, AccessMiddleware}
if slices.Contains(middlewareList, tyName) { return slices.Contains(middlewareList, tyName)
return true
}
return false
} }
func GetMiddleware(rule string, middlewares []Middleware) (Middleware, error) { func GetMiddleware(rule string, middlewares []Middleware) (Middleware, error) {
for _, m := range middlewares { for _, m := range middlewares {

View File

@@ -79,7 +79,7 @@ func (jwtAuth JwtAuth) AuthMiddleware(next http.Handler) http.Handler {
defer func(Body io.ReadCloser) { defer func(Body io.ReadCloser) {
err := Body.Close() err := Body.Close()
if err != nil { if err != nil {
logger.Error("Error closing body: %v", err)
} }
}(authResp.Body) }(authResp.Body)
// Inject specific header tp the current request's header // Inject specific header tp the current request's header

View File

@@ -32,7 +32,7 @@ type RateLimiter struct {
clientMap map[string]*Client clientMap map[string]*Client
mu sync.Mutex mu sync.Mutex
origins []string origins []string
hosts []string //hosts []string
redisBased bool redisBased bool
} }

View File

@@ -24,11 +24,11 @@ func TestCheckConfig(t *testing.T) {
TestInit(t) TestInit(t)
err := initConfig(configFile) err := initConfig(configFile)
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatal("Error init config:", err)
} }
err = CheckConfig(configFile) err = CheckConfig(configFile)
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf("Error checking config: %s", err.Error())
} }
log.Println("Goma Gateway configuration file checked successfully") log.Println("Goma Gateway configuration file checked successfully")
} }
@@ -37,7 +37,7 @@ func TestStart(t *testing.T) {
TestInit(t) TestInit(t)
err := initConfig(configFile) err := initConfig(configFile)
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf("Error initializing config: %s", err.Error())
} }
g := GatewayServer{} g := GatewayServer{}
gatewayServer, err := g.Config(configFile) gatewayServer, err := g.Config(configFile)

View File

@@ -19,13 +19,9 @@ package pkg
import ( import (
"context" "context"
"github.com/gorilla/mux"
"time" "time"
) )
type Config struct {
file string
}
type BasicRuleMiddleware struct { type BasicRuleMiddleware struct {
Username string `yaml:"username"` Username string `yaml:"username"`
Password string `yaml:"password"` Password string `yaml:"password"`
@@ -126,20 +122,12 @@ type ProxyRoute struct {
rewrite string rewrite string
destination string destination string
backends []string backends []string
healthCheck RouteHealthCheck //healthCheck RouteHealthCheck
methods []string methods []string
cors Cors cors Cors
disableHostFording bool disableHostFording bool
insecureSkipVerify bool insecureSkipVerify bool
} }
type RoutePath struct {
route Route
path string
rules []string
middlewares []Middleware
router *mux.Router
}
type HealthCheckRoute struct { type HealthCheckRoute struct {
DisableRouteHealthCheckError bool DisableRouteHealthCheckError bool
Routes []Route Routes []Route

View File

@@ -3,16 +3,11 @@ package pkg
const ConfigDir = "/etc/goma/" // Default configuration file const ConfigDir = "/etc/goma/" // Default configuration file
const ConfigFile = "/etc/goma/goma.yml" // Default configuration file const ConfigFile = "/etc/goma/goma.yml" // Default configuration file
const accessControlAllowOrigin = "Access-Control-Allow-Origin" // Cors const accessControlAllowOrigin = "Access-Control-Allow-Origin" // Cors
const serverName = "Goma"
const gatewayName = "Goma Gateway" const gatewayName = "Goma Gateway"
const AccessMiddleware = "access" // access middleware const AccessMiddleware = "access" // access middleware
const BasicAuth = "basic" // basic authentication middleware const BasicAuth = "basic" // basic authentication middleware
const JWTAuth = "jwt" // JWT authentication middleware const JWTAuth = "jwt" // JWT authentication middleware
const OAuth = "oauth" // OAuth authentication middleware const OAuth = "oauth" // OAuth authentication middleware
const applicationJson = "application/json"
const textPlain = "text/plain"
const applicationXml = "application/xml"
// Round-robin counter // Round-robin counter
var counter uint32 var counter uint32

View File

@@ -26,11 +26,6 @@ import (
"github.com/jkaninda/goma-gateway/util" "github.com/jkaninda/goma-gateway/util"
) )
type Logger struct {
msg string
args interface{}
}
// Info returns info log // Info returns info log
func Info(msg string, args ...interface{}) { func Info(msg string, args ...interface{}) {
log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout"))) log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout")))