From 5e66a634f2e76f48399019779d0f9e8627451500 Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Thu, 14 Nov 2024 18:32:25 +0100 Subject: [PATCH] Fix config init --- internal/config.go | 27 +++++++-------------------- internal/middleware/helpers.go | 23 ----------------------- internal/route.go | 3 ++- internal/types.go | 9 +++------ pkg/errorinterceptor/types.go | 31 ------------------------------- pkg/errorinterceptor/var.go | 22 ---------------------- 6 files changed, 12 insertions(+), 103 deletions(-) delete mode 100644 pkg/errorinterceptor/types.go delete mode 100644 pkg/errorinterceptor/var.go diff --git a/internal/config.go b/internal/config.go index 6e9fa2c..84a34e9 100644 --- a/internal/config.go +++ b/internal/config.go @@ -18,7 +18,6 @@ limitations under the License. import ( "fmt" "github.com/jkaninda/goma-gateway/internal/middleware" - "github.com/jkaninda/goma-gateway/pkg/errorinterceptor" "github.com/jkaninda/goma-gateway/pkg/logger" "github.com/jkaninda/goma-gateway/util" "golang.org/x/oauth2" @@ -28,7 +27,6 @@ import ( "golang.org/x/oauth2/gitlab" "golang.org/x/oauth2/google" "gopkg.in/yaml.v3" - "net/http" "os" ) @@ -182,23 +180,11 @@ func initConfig(configFile string) error { Middlewares: []string{"basic-auth", "api-forbidden-paths"}, }, { - Path: "/", - Name: "Hostname and load balancing example", - Hosts: []string{"example.com", "example.localhost"}, - //ErrorIntercept: []int{404, 405, 500}, - ErrorInterceptor: errorinterceptor.ErrorInterceptor{ - Errors: []errorinterceptor.Error{ - { - Code: http.StatusUnauthorized, - Message: http.StatusText(http.StatusUnauthorized), - }, - { - Code: http.StatusInternalServerError, - Message: http.StatusText(http.StatusInternalServerError), - }, - }, - }, - RateLimit: 60, + Path: "/", + Name: "Hostname and load balancing example", + Hosts: []string{"example.com", "example.localhost"}, + InterceptErrors: []int{404, 405, 500}, + RateLimit: 60, Backends: []string{ "https://example.com", "https://example2.com", @@ -208,7 +194,7 @@ func initConfig(configFile string) error { HealthCheck: RouteHealthCheck{}, }, { - Path: "/loadbalancing", + Path: "/", Name: "loadBalancing example", Hosts: []string{"example.com", "example.localhost"}, Backends: []string{ @@ -292,6 +278,7 @@ func initConfig(configFile string) error { ClientID: "xxxx", ClientSecret: "xxxx", RedirectURL: "http://localhost:8080/callback", + Provider: "custom", Scopes: []string{"email", "openid"}, JWTSecret: "your-strong-jwt-secret | It's optional", Endpoint: OauthEndpoint{ diff --git a/internal/middleware/helpers.go b/internal/middleware/helpers.go index 1594165..5f78b7a 100644 --- a/internal/middleware/helpers.go +++ b/internal/middleware/helpers.go @@ -19,8 +19,6 @@ package middleware import ( "encoding/json" - "fmt" - "github.com/jkaninda/goma-gateway/pkg/errorinterceptor" "net/http" "slices" ) @@ -37,27 +35,6 @@ func getRealIP(r *http.Request) string { func allowedOrigin(origins []string, origin string) bool { return slices.Contains(origins, origin) } -func canInterceptError(code int, errors []errorinterceptor.Error) bool { - for _, er := range errors { - if er.Code == code { - return true - } - continue - - } - return false -} -func errMessage(code int, errors []errorinterceptor.Error) (string, error) { - for _, er := range errors { - if er.Code == code { - if len(er.Message) != 0 { - return er.Message, nil - } - continue - } - } - return "", fmt.Errorf("%d errors occurred", code) -} // RespondWithError is a helper function to handle error responses with flexible content type func RespondWithError(w http.ResponseWriter, statusCode int, logMessage string) { diff --git a/internal/route.go b/internal/route.go index a6fcbad..c7f72f8 100644 --- a/internal/route.go +++ b/internal/route.go @@ -267,7 +267,8 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router { } // Apply route Error interceptor middleware interceptErrors := middleware.InterceptErrors{ - Origins: gateway.Cors.Origins, + Origins: route.Cors.Origins, + Errors: route.InterceptErrors, } router.Use(interceptErrors.ErrorInterceptor) } else { diff --git a/internal/types.go b/internal/types.go index c3c85e2..2805d38 100644 --- a/internal/types.go +++ b/internal/types.go @@ -20,7 +20,6 @@ package pkg import ( "context" "github.com/gorilla/mux" - errorinterceptor "github.com/jkaninda/goma-gateway/pkg/errorinterceptor" "time" ) @@ -162,12 +161,10 @@ type Route struct { // // It will not match the backend route DisableHostFording bool `yaml:"disableHostFording"` - + // InterceptErrors holds the status codes to intercept the error from backend + InterceptErrors []int `yaml:"interceptErrors"` // BlockCommonExploits enable, disable block common exploits BlockCommonExploits bool `yaml:"blockCommonExploits"` - // ErrorInterceptor intercepts backend errors based on the status codes and custom message - // - ErrorInterceptor errorinterceptor.ErrorInterceptor `yaml:"errorInterceptor"` // Middlewares Defines route middleware from Middleware names Middlewares []string `yaml:"middlewares"` } @@ -290,7 +287,7 @@ type Health struct { HealthyStatuses []int } type Redis struct { - // Addr redis hostname and post number : + // Addr redis hostname and port number : Addr string `yaml:"addr"` Password string `yaml:"password"` } diff --git a/pkg/errorinterceptor/types.go b/pkg/errorinterceptor/types.go deleted file mode 100644 index 23b0ffc..0000000 --- a/pkg/errorinterceptor/types.go +++ /dev/null @@ -1,31 +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 errorinterceptor - -type ErrorInterceptor struct { - // ContentType error response content type, application/json, plain/text - //ContentType string `yaml:"contentType"` - //Errors contains error status code and custom message - Errors []Error `yaml:"errors"` -} -type Error struct { - // Code HTTP status code - Code int `yaml:"code"` - // Message Error custom response message - Message string `yaml:"message"` -} diff --git a/pkg/errorinterceptor/var.go b/pkg/errorinterceptor/var.go deleted file mode 100644 index d924846..0000000 --- a/pkg/errorinterceptor/var.go +++ /dev/null @@ -1,22 +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 errorinterceptor - -const TextPlain = "text/plain" -const ApplicationXml = "application/xml" -const ApplicationJson = "application/json"