refatcor: improve error route interceptor

This commit is contained in:
Jonas Kaninda
2024-11-14 09:49:18 +01:00
parent 42abf56473
commit 3c4920ec9a
8 changed files with 25 additions and 54 deletions

View File

@@ -18,7 +18,7 @@ limitations under the License.
import ( import (
"fmt" "fmt"
"github.com/jkaninda/goma-gateway/internal/middleware" "github.com/jkaninda/goma-gateway/internal/middleware"
error_interceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor" "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"
@@ -185,10 +185,9 @@ func initConfig(configFile string) error {
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"},
//InterceptErrors: []int{404, 405, 500}, //ErrorIntercept: []int{404, 405, 500},
ErrorInterceptor: error_interceptor.ErrorInterceptor{ ErrorInterceptor: errorinterceptor.ErrorInterceptor{
ContentType: applicationJson, Errors: []errorinterceptor.Error{
Errors: []error_interceptor.Error{
{ {
Code: http.StatusUnauthorized, Code: http.StatusUnauthorized,
Message: http.StatusText(http.StatusUnauthorized), Message: http.StatusText(http.StatusUnauthorized),

View File

@@ -19,7 +19,7 @@ package middleware
import ( import (
"fmt" "fmt"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor" errorinterceptor "github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"github.com/jkaninda/goma-gateway/pkg/logger" "github.com/jkaninda/goma-gateway/pkg/logger"
"net/http" "net/http"
"regexp" "regexp"

View File

@@ -20,8 +20,9 @@ package middleware
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor" "github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"net/http" "net/http"
"slices"
) )
func getRealIP(r *http.Request) string { func getRealIP(r *http.Request) string {
@@ -34,14 +35,7 @@ func getRealIP(r *http.Request) string {
return r.RemoteAddr return r.RemoteAddr
} }
func allowedOrigin(origins []string, origin string) bool { func allowedOrigin(origins []string, origin string) bool {
for _, o := range origins { return slices.Contains(origins, origin)
if o == origin {
return true
}
continue
}
return false
} }
func canInterceptError(code int, errors []errorinterceptor.Error) bool { func canInterceptError(code int, errors []errorinterceptor.Error) bool {
for _, er := range errors { for _, er := range errors {
@@ -71,25 +65,16 @@ func RespondWithError(w http.ResponseWriter, statusCode int, logMessage string,
if err != nil { if err != nil {
message = logMessage message = logMessage
} }
if errorIntercept.ContentType == errorinterceptor.ApplicationJson { w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Type", "application/json") w.WriteHeader(statusCode)
w.WriteHeader(statusCode) err = json.NewEncoder(w).Encode(ProxyResponseError{
err := json.NewEncoder(w).Encode(ProxyResponseError{ Success: false,
Success: false, Code: statusCode,
Code: statusCode, Message: message,
Message: message, })
}) if err != nil {
if err != nil {
return
}
return
} else {
w.Header().Set("Content-Type", "plain/text;charset=utf-8")
w.WriteHeader(statusCode)
_, err2 := w.Write([]byte(message))
if err2 != nil {
return
}
return return
} }
return
} }

View File

@@ -17,7 +17,7 @@ package middleware
* *
*/ */
import ( import (
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor" "github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"github.com/jkaninda/goma-gateway/pkg/logger" "github.com/jkaninda/goma-gateway/pkg/logger"
"io" "io"
"net/http" "net/http"

View File

@@ -19,7 +19,7 @@ package middleware
import ( import (
"bytes" "bytes"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor" errorinterceptor "github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"net/http" "net/http"
"sync" "sync"
"time" "time"

View File

@@ -20,7 +20,7 @@ package pkg
import ( import (
"context" "context"
"github.com/gorilla/mux" "github.com/gorilla/mux"
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/error-interceptor" errorinterceptor "github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
"time" "time"
) )
@@ -287,16 +287,3 @@ type Health struct {
Interval string Interval string
HealthyStatuses []int HealthyStatuses []int
} }
//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 []ErrorInterceptor `yaml:"errors"`
//}
//type ErrorInterceptor struct {
// // Code HTTP status code
// Code int `yaml:"code"`
// // Message custom message
// Message string `yaml:"message"`
//}

View File

@@ -15,17 +15,17 @@
* *
*/ */
package error_interceptor package errorinterceptor
type ErrorInterceptor struct { type ErrorInterceptor struct {
// ContentType error response content type, application/json, plain/text // ContentType error response content type, application/json, plain/text
ContentType string `yaml:"contentType"` //ContentType string `yaml:"contentType"`
//Errors contains error status code and custom message //Errors contains error status code and custom message
Errors []Error `yaml:"errors"` Errors []Error `yaml:"errors"`
} }
type Error struct { type Error struct {
// Code HTTP status code // Code HTTP status code
Code int `yaml:"code"` Code int `yaml:"code"`
// Message custom message // Message Error custom response message
Message string `yaml:"message"` Message string `yaml:"message"`
} }

View File

@@ -15,7 +15,7 @@
* *
*/ */
package error_interceptor package errorinterceptor
const TextPlain = "text/plain" const TextPlain = "text/plain"
const ApplicationXml = "application/xml" const ApplicationXml = "application/xml"