From f30ddac7a127a60d57d5bc5d254d8483faa8739c Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Tue, 29 Oct 2024 14:21:55 +0100 Subject: [PATCH 1/2] chore: update default configuration --- pkg/config.go | 16 ++++++++++++---- pkg/proxy.go | 2 ++ pkg/var.go | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/pkg/config.go b/pkg/config.go index 28db467..853167a 100644 --- a/pkg/config.go +++ b/pkg/config.go @@ -243,6 +243,7 @@ func initConfig(configFile string) { DisableRouteHealthCheckError: false, DisableDisplayRouteOnStart: false, RateLimiter: 0, + InterceptErrors: []int{405, 500}, Cors: Cors{ Origins: []string{"http://localhost:8080", "https://example.com"}, Headers: map[string]string{ @@ -253,8 +254,7 @@ func initConfig(configFile string) { }, Routes: []Route{ { - Name: "HealthCheck", - Host: "localhost", + Name: "Public", Path: "/public", Destination: "http://localhost:80", Rewrite: "/healthz", @@ -282,19 +282,27 @@ func initConfig(configFile string) { }, }, }, + { + Name: "Hostname example", + Host: "example.com", + Path: "/", + Destination: "https://example.com", + Rewrite: "/", + HealthCheck: "", + }, }, }, Middlewares: []Middleware{ { Name: "basic-auth", - Type: "basic", + Type: basicAuth, Rule: BasicRule{ Username: "goma", Password: "goma", }, }, { Name: "jwt", - Type: "jwt", + Type: jwtAuth, Rule: JWTRuler{ URL: "https://www.googleapis.com/auth/userinfo.email", RequiredHeaders: []string{ diff --git a/pkg/proxy.go b/pkg/proxy.go index 88d9e90..9d60298 100644 --- a/pkg/proxy.go +++ b/pkg/proxy.go @@ -38,6 +38,8 @@ func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { realIP := getRealIP(r) logger.Info("%s %s %s %s", r.Method, realIP, r.URL, r.UserAgent()) + //Set Server name + w.Header().Set("Server", serverName) // Set CORS headers from the cors config //Update Cors Headers for k, v := range proxyRoute.cors.Headers { diff --git a/pkg/var.go b/pkg/var.go index 5a75b09..a750893 100644 --- a/pkg/var.go +++ b/pkg/var.go @@ -5,3 +5,4 @@ const accessControlAllowOrigin = "Access-Control-Allow-Origin" const basicAuth = "basicAuth" const jwtAuth = "jwtAuth" const OAuth = "OAuth" +const serverName = "Goma" From 97c7e0940b5cde0b4bb5b017f3ab0bc9d55199a2 Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Tue, 29 Oct 2024 14:36:57 +0100 Subject: [PATCH 2/2] feat: add enable and disable keep alive --- pkg/config.go | 1 + pkg/middleware/error-interceptor.go | 2 ++ pkg/proxy.go | 2 -- pkg/server.go | 2 ++ 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/config.go b/pkg/config.go index 853167a..a70b593 100644 --- a/pkg/config.go +++ b/pkg/config.go @@ -153,6 +153,7 @@ type Gateway struct { //Disable dispelling routes on start DisableDisplayRouteOnStart bool `yaml:"disableDisplayRouteOnStart"` InterceptErrors []int `yaml:"interceptErrors"` + EnableKeepAlive bool `yaml:"enableKeepAlive"` // Cors contains the proxy global cors Cors Cors `yaml:"cors"` // Routes defines the proxy routes diff --git a/pkg/middleware/error-interceptor.go b/pkg/middleware/error-interceptor.go index 8383487..607df03 100644 --- a/pkg/middleware/error-interceptor.go +++ b/pkg/middleware/error-interceptor.go @@ -57,6 +57,8 @@ func (intercept InterceptErrors) ErrorInterceptor(next http.Handler) http.Handle return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rec := newResponseRecorder(w) next.ServeHTTP(rec, r) + //Set Server name + w.Header().Set("Server", "Goma") if canIntercept(rec.statusCode, intercept.Errors) { logger.Debug("Backend error intercepted") logger.Debug("An error occurred in the backend, %d", rec.statusCode) diff --git a/pkg/proxy.go b/pkg/proxy.go index 9d60298..88d9e90 100644 --- a/pkg/proxy.go +++ b/pkg/proxy.go @@ -38,8 +38,6 @@ func (proxyRoute ProxyRoute) ProxyHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { realIP := getRealIP(r) logger.Info("%s %s %s %s", r.Method, realIP, r.URL, r.UserAgent()) - //Set Server name - w.Header().Set("Server", serverName) // Set CORS headers from the cors config //Update Cors Headers for k, v := range proxyRoute.cors.Headers { diff --git a/pkg/server.go b/pkg/server.go index 9dfbcdc..446eb61 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -39,6 +39,8 @@ func (gatewayServer GatewayServer) Start(ctx context.Context) error { if !gatewayServer.gateway.DisableDisplayRouteOnStart { printRoute(gatewayServer.gateway.Routes) } + // Set KeepAlive + srv.SetKeepAlivesEnabled(gatewayServer.gateway.EnableKeepAlive) go func() { logger.Info("Started Goma Gateway server on %v", gatewayServer.gateway.ListenAddr)