chore: add route config check
This commit is contained in:
@@ -1,66 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2024 Jonas Kaninda
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package pkg
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/jkaninda/goma-gateway/util"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func CheckConfig(fileName string) error {
|
|
||||||
if !util.FileExists(fileName) {
|
|
||||||
return fmt.Errorf("config file not found: %s", fileName)
|
|
||||||
}
|
|
||||||
buf, err := os.ReadFile(fileName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
c := &GatewayConfig{}
|
|
||||||
err = yaml.Unmarshal(buf, c)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("parsing the configuration file %q: %w", fileName, err)
|
|
||||||
}
|
|
||||||
gateway := &GatewayServer{
|
|
||||||
ctx: nil,
|
|
||||||
version: c.Version,
|
|
||||||
gateway: c.GatewayConfig,
|
|
||||||
middlewares: c.Middlewares,
|
|
||||||
}
|
|
||||||
for index, route := range gateway.gateway.Routes {
|
|
||||||
if len(route.Name) == 0 {
|
|
||||||
fmt.Printf("Warning: route name is empty, index: [%d]", index)
|
|
||||||
}
|
|
||||||
if route.Destination == "" && len(route.Backends) == 0 {
|
|
||||||
fmt.Printf("Error: no destination or backends specified for route: %s | index: [%d] \n", route.Name, index)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check middlewares
|
|
||||||
for index, mid := range c.Middlewares {
|
|
||||||
if util.HasWhitespace(mid.Name) {
|
|
||||||
fmt.Printf("Warning: Middleware contains whitespace: %s | index: [%d], please remove whitespace characters\n", mid.Name, index)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Routes count=%d Middlewares count=%d\n", len(gateway.gateway.Routes), len(gateway.middlewares))
|
|
||||||
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
118
internal/check_config.go
Normal file
118
internal/check_config.go
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 Jonas Kaninda
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package pkg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/jkaninda/goma-gateway/util"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
"os"
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CheckConfig(fileName string) error {
|
||||||
|
if !util.FileExists(fileName) {
|
||||||
|
return fmt.Errorf("config file not found: %s", fileName)
|
||||||
|
}
|
||||||
|
buf, err := os.ReadFile(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c := &GatewayConfig{}
|
||||||
|
err = yaml.Unmarshal(buf, c)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("parsing the configuration file %q: %w", fileName, err)
|
||||||
|
}
|
||||||
|
gateway := &GatewayServer{
|
||||||
|
ctx: nil,
|
||||||
|
version: c.Version,
|
||||||
|
gateway: c.GatewayConfig,
|
||||||
|
middlewares: c.Middlewares,
|
||||||
|
}
|
||||||
|
dynamicRoutes = gateway.gateway.Routes
|
||||||
|
// Check middlewares
|
||||||
|
fmt.Println("Checking middlewares...")
|
||||||
|
for index, mid := range c.Middlewares {
|
||||||
|
if len(mid.Name) == 0 {
|
||||||
|
fmt.Printf("Warning: Middleware name required: index: [%d]\n", index)
|
||||||
|
}
|
||||||
|
if util.HasWhitespace(mid.Name) {
|
||||||
|
fmt.Printf("Warning: Middleware contains whitespace: %s | index: [%d], please remove whitespace characters\n", mid.Name, index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("Checking middlewares...done")
|
||||||
|
//Check additional routes
|
||||||
|
fmt.Println("Checking routes...")
|
||||||
|
// Load Extra Routes
|
||||||
|
if len(gateway.gateway.ExtraRoutes.Directory) != 0 {
|
||||||
|
extraRoutes, err := loadExtraRoutes(gateway.gateway.ExtraRoutes.Directory)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error: %v\n", err.Error())
|
||||||
|
}
|
||||||
|
if len(extraRoutes) == 0 {
|
||||||
|
fmt.Printf("no extra routes found in %s\n", gateway.gateway.ExtraRoutes.Directory)
|
||||||
|
} else {
|
||||||
|
dynamicRoutes = append(dynamicRoutes, extraRoutes...)
|
||||||
|
fmt.Printf("Loaded %d extra routes from %s\n", len(extraRoutes), gateway.gateway.ExtraRoutes.Directory)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Check routes
|
||||||
|
checkRoutes(dynamicRoutes, gateway.middlewares)
|
||||||
|
fmt.Println("Checking routes...done")
|
||||||
|
|
||||||
|
fmt.Printf("Routes count=%d Middlewares count=%d\n", len(dynamicRoutes), len(gateway.middlewares))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkRoutes(routes []Route, middlewares []Middleware) {
|
||||||
|
midNames := middlewareNames(middlewares)
|
||||||
|
for index, route := range routes {
|
||||||
|
if len(route.Name) == 0 {
|
||||||
|
fmt.Printf("Warning: route name is empty, index: [%d]\n", index)
|
||||||
|
}
|
||||||
|
if route.Destination == "" && len(route.Backends) == 0 {
|
||||||
|
fmt.Printf("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) {
|
||||||
|
fmt.Printf("Couldn't find a middleware with the name: %s | route: %s \n", middleware, route.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// find duplicated route name
|
||||||
|
duplicates := findDuplicateRouteNames(routes)
|
||||||
|
if len(duplicates) != 0 {
|
||||||
|
for _, duplicate := range duplicates {
|
||||||
|
fmt.Printf("Duplicated route name was found: %s \n", duplicate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func middlewareNames(middlewares []Middleware) []string {
|
||||||
|
|
||||||
|
var names []string
|
||||||
|
|
||||||
|
for _, mid := range middlewares {
|
||||||
|
names = append(names, mid.Name)
|
||||||
|
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
@@ -38,11 +38,17 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
|||||||
dynamicRoutes = gateway.Routes
|
dynamicRoutes = gateway.Routes
|
||||||
// Load Extra Routes
|
// Load Extra Routes
|
||||||
if len(gateway.ExtraRoutes.Directory) != 0 {
|
if len(gateway.ExtraRoutes.Directory) != 0 {
|
||||||
|
logger.Info("Loading additional routes from %s", gateway.ExtraRoutes.Directory)
|
||||||
extraRoutes, err := loadExtraRoutes(gateway.ExtraRoutes.Directory)
|
extraRoutes, err := loadExtraRoutes(gateway.ExtraRoutes.Directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Error: %v", err.Error())
|
logger.Error("Error: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
if len(extraRoutes) == 0 {
|
||||||
|
logger.Info("no extra routes found in %s", gateway.ExtraRoutes.Directory)
|
||||||
|
} else {
|
||||||
dynamicRoutes = append(dynamicRoutes, extraRoutes...)
|
dynamicRoutes = append(dynamicRoutes, extraRoutes...)
|
||||||
|
logger.Info("Loaded %d extra routes from %s", len(extraRoutes), gateway.ExtraRoutes.Directory)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// find duplicated route name
|
// find duplicated route name
|
||||||
duplicates := findDuplicateRouteNames(dynamicRoutes)
|
duplicates := findDuplicateRouteNames(dynamicRoutes)
|
||||||
|
|||||||
@@ -19,14 +19,12 @@ package pkg
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jkaninda/goma-gateway/pkg/logger"
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// loadExtraRoutes loads additional routes
|
// loadExtraRoutes loads additional routes
|
||||||
func loadExtraRoutes(routePath string) ([]Route, error) {
|
func loadExtraRoutes(routePath string) ([]Route, error) {
|
||||||
logger.Info("Loading additional routes from %s", routePath)
|
|
||||||
yamlFiles, err := loadExtraFiles(routePath)
|
yamlFiles, err := loadExtraFiles(routePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error loading extra files: %v", err)
|
return nil, fmt.Errorf("error loading extra files: %v", err)
|
||||||
@@ -44,12 +42,6 @@ func loadExtraRoutes(routePath string) ([]Route, error) {
|
|||||||
}
|
}
|
||||||
extraRoutes = append(extraRoutes, ex.Routes...)
|
extraRoutes = append(extraRoutes, ex.Routes...)
|
||||||
|
|
||||||
}
|
|
||||||
if len(extraRoutes) == 0 {
|
|
||||||
return nil, fmt.Errorf("no extra routes found in %s", routePath)
|
|
||||||
} else {
|
|
||||||
logger.Info("Loaded %d extra routes from %s", len(extraRoutes), routePath)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return extraRoutes, nil
|
return extraRoutes, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,10 @@ func TestStart(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error creating extra routes file: %s", err.Error())
|
t.Fatalf("Error creating extra routes file: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
err = CheckConfig(configFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error checking config: %s", err.Error())
|
||||||
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
g := GatewayServer{}
|
g := GatewayServer{}
|
||||||
gatewayServer, err := g.Config(configFile, ctx)
|
gatewayServer, err := g.Config(configFile, ctx)
|
||||||
|
|||||||
@@ -28,4 +28,5 @@ func FullVersion() {
|
|||||||
|
|
||||||
const MainExample = "Initialize config: config init --output config.yml\n" +
|
const MainExample = "Initialize config: config init --output config.yml\n" +
|
||||||
"Start server: server \n" +
|
"Start server: server \n" +
|
||||||
"Start server with custom config file: server --config config.yml"
|
"Start server with custom config file: server --config config.yml \n" +
|
||||||
|
"Check config file: config init -f -o config.yml"
|
||||||
|
|||||||
Reference in New Issue
Block a user