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"
)
var cfg *Gateway
// Config reads config file and returns Gateway
func (GatewayServer) Config(configFile string) (*GatewayServer, error) {
if util.FileExists(configFile) {

View File

@@ -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: ""})

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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")))