From c8b16a204c9a6558bbbff8b293d308036c738f2c Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Sun, 24 Nov 2024 06:04:55 +0100 Subject: [PATCH] chore: add configuration validation --- internal/check_config.go | 40 ++++++++++++++++++++++++++++++++++++++++ internal/middleware.go | 2 +- internal/route_config.go | 1 + internal/routes.go | 18 ++++-------------- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/internal/check_config.go b/internal/check_config.go index 176ec99..50cf81d 100644 --- a/internal/check_config.go +++ b/internal/check_config.go @@ -19,12 +19,14 @@ package pkg import ( "fmt" + "github.com/jkaninda/goma-gateway/pkg/logger" "github.com/jkaninda/goma-gateway/util" "gopkg.in/yaml.v3" "os" "slices" ) +// CheckConfig checks configs func CheckConfig(fileName string) error { if !util.FileExists(fileName) { return fmt.Errorf("config file not found: %s", fileName) @@ -81,6 +83,7 @@ func CheckConfig(fileName string) error { } +// checkRoutes checks routes func checkRoutes(routes []Route, middlewares []Middleware) { midNames := middlewareNames(middlewares) for index, route := range routes { @@ -106,6 +109,43 @@ func checkRoutes(routes []Route, middlewares []Middleware) { } } +// checkConfig checks configurations and returns error +func checkConfig(routes []Route, middlewares []Middleware) error { + logger.Info("Checking configurations...") + midNames := middlewareNames(middlewares) + for index, route := range routes { + if len(route.Name) == 0 { + logger.Warn("Warning: route name is empty, index: [%d]", index) + } + if route.Destination == "" && len(route.Backends) == 0 { + return fmt.Errorf("Error: no destination or backends specified for route: %s | index: [%d] \n", route.Name, index) + } + // checking middleware applied to routes + for _, middleware := range route.Middlewares { + if !slices.Contains(midNames, middleware) { + logger.Warn("Couldn't find a middleware with the name: %s | route: %s", middleware, route.Name) + } + } + } + + // find duplicated middleware name + duplicates := findDuplicateMiddlewareNames(dynamicMiddlewares) + if len(duplicates) != 0 { + for _, duplicate := range duplicates { + return fmt.Errorf("duplicated middleware name: %s, the name of the middleware should be unique", duplicate) + } + } + // find duplicated route name + duplicates = findDuplicateRouteNames(dynamicRoutes) + if len(duplicates) != 0 { + for _, duplicate := range duplicates { + logger.Warn("Duplicated route name was found: %s ", duplicate) + } + } + return nil +} + +// middlewareNames reruns middleware names func middlewareNames(middlewares []Middleware) []string { names := []string{} for _, mid := range middlewares { diff --git a/internal/middleware.go b/internal/middleware.go index 9339cff..6f519bc 100644 --- a/internal/middleware.go +++ b/internal/middleware.go @@ -17,7 +17,7 @@ func getMiddleware(rules []string, middlewares []Middleware) (Middleware, error) continue } - return Middleware{}, errors.New("middlewares not found with name: [" + strings.Join(rules, ";") + "]") + return Middleware{}, errors.New("middleware not found with name: [" + strings.Join(rules, ";") + "]") } func doesExist(tyName string) bool { diff --git a/internal/route_config.go b/internal/route_config.go index 1ca3cc6..ba8307b 100644 --- a/internal/route_config.go +++ b/internal/route_config.go @@ -49,6 +49,7 @@ func loadExtraRoutes(routePath string) ([]Route, error) { return extraRoutes, nil } +// findDuplicateRouteNames finds duplicated route names func findDuplicateRouteNames(routes []Route) []string { // Create a map to track occurrences of names nameMap := make(map[string]int) diff --git a/internal/routes.go b/internal/routes.go index fa84671..592acde 100644 --- a/internal/routes.go +++ b/internal/routes.go @@ -56,20 +56,10 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router { logger.Info("Loaded %d additional routes", len(extraRoutes)) } - - // find duplicated middleware name - duplicates := findDuplicateMiddlewareNames(dynamicMiddlewares) - if len(duplicates) != 0 { - for _, duplicate := range duplicates { - logger.Fatal("Duplicated middleware name: %s, the name of the middleware should be unique.", duplicate) - } - } - // find duplicated route name - duplicates = findDuplicateRouteNames(dynamicRoutes) - if len(duplicates) != 0 { - for _, duplicate := range duplicates { - logger.Error("Duplicated route name was found: %s ", duplicate) - } + // Check configs + err = checkConfig(dynamicRoutes, dynamicMiddlewares) + if err != nil { + logger.Fatal("Error: %v", err) } m := dynamicMiddlewares redisBased := false