chore: add configuration validation
This commit is contained in:
@@ -19,12 +19,14 @@ package pkg
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/jkaninda/goma-gateway/pkg/logger"
|
||||||
"github.com/jkaninda/goma-gateway/util"
|
"github.com/jkaninda/goma-gateway/util"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// CheckConfig checks configs
|
||||||
func CheckConfig(fileName string) error {
|
func CheckConfig(fileName string) error {
|
||||||
if !util.FileExists(fileName) {
|
if !util.FileExists(fileName) {
|
||||||
return fmt.Errorf("config file not found: %s", 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) {
|
func checkRoutes(routes []Route, middlewares []Middleware) {
|
||||||
midNames := middlewareNames(middlewares)
|
midNames := middlewareNames(middlewares)
|
||||||
for index, route := range routes {
|
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 {
|
func middlewareNames(middlewares []Middleware) []string {
|
||||||
names := []string{}
|
names := []string{}
|
||||||
for _, mid := range middlewares {
|
for _, mid := range middlewares {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func getMiddleware(rules []string, middlewares []Middleware) (Middleware, error)
|
|||||||
continue
|
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 {
|
func doesExist(tyName string) bool {
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ func loadExtraRoutes(routePath string) ([]Route, error) {
|
|||||||
return extraRoutes, nil
|
return extraRoutes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// findDuplicateRouteNames finds duplicated route names
|
||||||
func findDuplicateRouteNames(routes []Route) []string {
|
func findDuplicateRouteNames(routes []Route) []string {
|
||||||
// Create a map to track occurrences of names
|
// Create a map to track occurrences of names
|
||||||
nameMap := make(map[string]int)
|
nameMap := make(map[string]int)
|
||||||
|
|||||||
@@ -56,20 +56,10 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
|||||||
logger.Info("Loaded %d additional routes", len(extraRoutes))
|
logger.Info("Loaded %d additional routes", len(extraRoutes))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// Check configs
|
||||||
// find duplicated middleware name
|
err = checkConfig(dynamicRoutes, dynamicMiddlewares)
|
||||||
duplicates := findDuplicateMiddlewareNames(dynamicMiddlewares)
|
if err != nil {
|
||||||
if len(duplicates) != 0 {
|
logger.Fatal("Error: %v", err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
m := dynamicMiddlewares
|
m := dynamicMiddlewares
|
||||||
redisBased := false
|
redisBased := false
|
||||||
|
|||||||
Reference in New Issue
Block a user