Fix config init

This commit is contained in:
Jonas Kaninda
2024-11-14 18:32:25 +01:00
parent 0905a41941
commit 5e66a634f2
6 changed files with 12 additions and 103 deletions

View File

@@ -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{

View File

@@ -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) {

View File

@@ -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 {

View File

@@ -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"`
}