Fix config init
This commit is contained in:
@@ -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"
|
||||
)
|
||||
|
||||
@@ -185,19 +183,7 @@ func initConfig(configFile string) error {
|
||||
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),
|
||||
},
|
||||
},
|
||||
},
|
||||
InterceptErrors: []int{404, 405, 500},
|
||||
RateLimit: 60,
|
||||
Backends: []string{
|
||||
"https://example.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{
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -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"
|
||||
Reference in New Issue
Block a user