Fix config init
This commit is contained in:
@@ -18,7 +18,6 @@ limitations under the License.
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/jkaninda/goma-gateway/internal/middleware"
|
"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/pkg/logger"
|
||||||
"github.com/jkaninda/goma-gateway/util"
|
"github.com/jkaninda/goma-gateway/util"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
@@ -28,7 +27,6 @@ import (
|
|||||||
"golang.org/x/oauth2/gitlab"
|
"golang.org/x/oauth2/gitlab"
|
||||||
"golang.org/x/oauth2/google"
|
"golang.org/x/oauth2/google"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -182,23 +180,11 @@ func initConfig(configFile string) error {
|
|||||||
Middlewares: []string{"basic-auth", "api-forbidden-paths"},
|
Middlewares: []string{"basic-auth", "api-forbidden-paths"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Path: "/",
|
Path: "/",
|
||||||
Name: "Hostname and load balancing example",
|
Name: "Hostname and load balancing example",
|
||||||
Hosts: []string{"example.com", "example.localhost"},
|
Hosts: []string{"example.com", "example.localhost"},
|
||||||
//ErrorIntercept: []int{404, 405, 500},
|
InterceptErrors: []int{404, 405, 500},
|
||||||
ErrorInterceptor: errorinterceptor.ErrorInterceptor{
|
RateLimit: 60,
|
||||||
Errors: []errorinterceptor.Error{
|
|
||||||
{
|
|
||||||
Code: http.StatusUnauthorized,
|
|
||||||
Message: http.StatusText(http.StatusUnauthorized),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Code: http.StatusInternalServerError,
|
|
||||||
Message: http.StatusText(http.StatusInternalServerError),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
RateLimit: 60,
|
|
||||||
Backends: []string{
|
Backends: []string{
|
||||||
"https://example.com",
|
"https://example.com",
|
||||||
"https://example2.com",
|
"https://example2.com",
|
||||||
@@ -208,7 +194,7 @@ func initConfig(configFile string) error {
|
|||||||
HealthCheck: RouteHealthCheck{},
|
HealthCheck: RouteHealthCheck{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Path: "/loadbalancing",
|
Path: "/",
|
||||||
Name: "loadBalancing example",
|
Name: "loadBalancing example",
|
||||||
Hosts: []string{"example.com", "example.localhost"},
|
Hosts: []string{"example.com", "example.localhost"},
|
||||||
Backends: []string{
|
Backends: []string{
|
||||||
@@ -292,6 +278,7 @@ func initConfig(configFile string) error {
|
|||||||
ClientID: "xxxx",
|
ClientID: "xxxx",
|
||||||
ClientSecret: "xxxx",
|
ClientSecret: "xxxx",
|
||||||
RedirectURL: "http://localhost:8080/callback",
|
RedirectURL: "http://localhost:8080/callback",
|
||||||
|
Provider: "custom",
|
||||||
Scopes: []string{"email", "openid"},
|
Scopes: []string{"email", "openid"},
|
||||||
JWTSecret: "your-strong-jwt-secret | It's optional",
|
JWTSecret: "your-strong-jwt-secret | It's optional",
|
||||||
Endpoint: OauthEndpoint{
|
Endpoint: OauthEndpoint{
|
||||||
|
|||||||
@@ -19,8 +19,6 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"slices"
|
"slices"
|
||||||
)
|
)
|
||||||
@@ -37,27 +35,6 @@ func getRealIP(r *http.Request) string {
|
|||||||
func allowedOrigin(origins []string, origin string) bool {
|
func allowedOrigin(origins []string, origin string) bool {
|
||||||
return slices.Contains(origins, origin)
|
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
|
// RespondWithError is a helper function to handle error responses with flexible content type
|
||||||
func RespondWithError(w http.ResponseWriter, statusCode int, logMessage string) {
|
func RespondWithError(w http.ResponseWriter, statusCode int, logMessage string) {
|
||||||
|
|||||||
@@ -267,7 +267,8 @@ func (gatewayServer GatewayServer) Initialize() *mux.Router {
|
|||||||
}
|
}
|
||||||
// Apply route Error interceptor middleware
|
// Apply route Error interceptor middleware
|
||||||
interceptErrors := middleware.InterceptErrors{
|
interceptErrors := middleware.InterceptErrors{
|
||||||
Origins: gateway.Cors.Origins,
|
Origins: route.Cors.Origins,
|
||||||
|
Errors: route.InterceptErrors,
|
||||||
}
|
}
|
||||||
router.Use(interceptErrors.ErrorInterceptor)
|
router.Use(interceptErrors.ErrorInterceptor)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ package pkg
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -162,12 +161,10 @@ type Route struct {
|
|||||||
//
|
//
|
||||||
// It will not match the backend route
|
// It will not match the backend route
|
||||||
DisableHostFording bool `yaml:"disableHostFording"`
|
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 enable, disable block common exploits
|
||||||
BlockCommonExploits bool `yaml:"blockCommonExploits"`
|
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 Defines route middleware from Middleware names
|
||||||
Middlewares []string `yaml:"middlewares"`
|
Middlewares []string `yaml:"middlewares"`
|
||||||
}
|
}
|
||||||
@@ -290,7 +287,7 @@ type Health struct {
|
|||||||
HealthyStatuses []int
|
HealthyStatuses []int
|
||||||
}
|
}
|
||||||
type Redis struct {
|
type Redis struct {
|
||||||
// Addr redis hostname and post number :
|
// Addr redis hostname and port number :
|
||||||
Addr string `yaml:"addr"`
|
Addr string `yaml:"addr"`
|
||||||
Password string `yaml:"password"`
|
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