From a904022dbef80d8bfeffbce471ebec679ba882cc Mon Sep 17 00:00:00 2001 From: rishavvajpayee Date: Wed, 2 Oct 2024 01:52:34 +0530 Subject: [PATCH] resolved golangci-lint erros --- config/config.go | 10 +++------- internal/middleware/cors.go | 2 +- pkg/util/helpers.go | 6 ++++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index 4d82a35..9d43cd8 100644 --- a/config/config.go +++ b/config/config.go @@ -20,9 +20,6 @@ type Config struct { // LoadConfig loads the application configuration from environment variables or defaults func LoadConfig() *Config { - - // go does not automatically load .env file - // thats why added a package as a dependency err := godotenv.Load() if err != nil { fmt.Println("Warning: .env file not found, falling back to system environment variables.") @@ -67,8 +64,7 @@ func getEnvFloat64(key string, fallback float64) float64 { func getEnvArray(key string, fallback []string) []string { if value, exists := os.LookupEnv(key); exists { - fmt.Println("value", value) - if arrayValue, err := splitString(value); err == nil { + if arrayValue := splitString(value); len(arrayValue) > 0 { return arrayValue } } @@ -76,10 +72,10 @@ func getEnvArray(key string, fallback []string) []string { } // splitString splits a string by comma and returns a slice of strings -func splitString(s string) ([]string, error) { +func splitString(s string) []string { var array []string for _, v := range strings.Split(s, ",") { array = append(array, strings.TrimSpace(v)) } - return array, nil + return array } diff --git a/internal/middleware/cors.go b/internal/middleware/cors.go index 5373607..2657468 100644 --- a/internal/middleware/cors.go +++ b/internal/middleware/cors.go @@ -7,7 +7,7 @@ import ( func enableCors(w http.ResponseWriter, origin string) { configValue := config.LoadConfig() - var allAllowedOrigins []string = configValue.AllowedOrigins + allAllowedOrigins := configValue.AllowedOrigins allowed := false for _, allowedOrigin := range allAllowedOrigins { if origin == allowedOrigin || allowedOrigin == "*" { diff --git a/pkg/util/helpers.go b/pkg/util/helpers.go index dc19d0c..b5eb34c 100644 --- a/pkg/util/helpers.go +++ b/pkg/util/helpers.go @@ -169,7 +169,9 @@ func JSONResponse(w http.ResponseWriter, status int, data interface{}) { func MockHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - w.Write([]byte("OK")) + if _, err := w.Write([]byte("OK")); err != nil { + log.Fatalf("Failed to write response: %v", err) + } } func SetupRateLimiter(limit int, window float64) (*httptest.ResponseRecorder, *http.Request, http.Handler) { @@ -179,7 +181,7 @@ func SetupRateLimiter(limit int, window float64) (*httptest.ResponseRecorder, *h log.Fatalf("Failed to initialize dice client: %v", err) } - r := httptest.NewRequest("GET", "/cli/get", nil) + r := httptest.NewRequest("GET", "/cli/get", http.NoBody) w := httptest.NewRecorder() rateLimiter := middleware.RateLimiter(client, http.HandlerFunc(MockHandler), limit, window)