From 33478a76bf5a7d4aecc51f84ce0a4301197a513d Mon Sep 17 00:00:00 2001 From: Flora Thiebaut Date: Mon, 28 Oct 2024 14:04:33 +0100 Subject: [PATCH 1/3] fix: change server setup --- cmd/gateway/main.go | 86 ++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/cmd/gateway/main.go b/cmd/gateway/main.go index ebbc4707..f833ae49 100644 --- a/cmd/gateway/main.go +++ b/cmd/gateway/main.go @@ -67,6 +67,49 @@ func main() { // TODO: maybe implement a real health check return c.NoContent(http.StatusOK) }) + // Rate limiting + if gwConfig.Server.RateLimits.Enabled { + e.Use(middleware.RateLimiter( + middleware.NewRateLimiterMemoryStoreWithConfig( + middleware.RateLimiterMemoryStoreConfig{ + Rate: rate.Limit(gwConfig.Server.RateLimits.Rate), + Burst: gwConfig.Server.RateLimits.Burst, + ExpiresIn: 3 * time.Minute, + }), + ), + ) + } + // CORS + if len(gwConfig.Server.AllowOrigin) > 0 { + e.Use(middleware.CORSWithConfig(middleware.CORSConfig{AllowOrigins: gwConfig.Server.AllowOrigin})) + } + // Sentry + if gwConfig.Monitoring.Sentry.Enabled { + err := sentry.Init(sentry.ClientOptions{ + Dsn: string(gwConfig.Monitoring.Sentry.Dsn), + TracesSampleRate: gwConfig.Monitoring.Sentry.SampleRate, + Environment: gwConfig.Monitoring.Sentry.Environment, + }) + if err != nil { + slog.Error("sentry initialization failed", "error", err) + } + e.Use(sentryecho.New(sentryecho.Options{})) + } + // Prometheus + if gwConfig.Monitoring.Prometheus.Enabled { + e.Use(echoprometheus.NewMiddleware("gateway")) + go func() { + metrics := echo.New() + metrics.HideBanner = true + metrics.HidePort = true + metrics.GET("/metrics", echoprometheus.NewHandler()) + err := metrics.Start(fmt.Sprintf(":%d", gwConfig.Monitoring.Prometheus.Port)) + if err != nil && !errors.Is(err, http.ErrServerClosed) { + slog.Error("prometheus server failed to start", "error", err) + os.Exit(1) + } + }() + } // Version endpoint buildInfo, ok := debug.ReadBuildInfo() version := "" @@ -130,49 +173,6 @@ func main() { os.Exit(1) } loginServer.RegisterHandlers(e, gwMiddlewares...) - // Rate limiting - if gwConfig.Server.RateLimits.Enabled { - e.Use(middleware.RateLimiter( - middleware.NewRateLimiterMemoryStoreWithConfig( - middleware.RateLimiterMemoryStoreConfig{ - Rate: rate.Limit(gwConfig.Server.RateLimits.Rate), - Burst: gwConfig.Server.RateLimits.Burst, - ExpiresIn: 3 * time.Minute, - }), - ), - ) - } - // CORS - if len(gwConfig.Server.AllowOrigin) > 0 { - e.Use(middleware.CORSWithConfig(middleware.CORSConfig{AllowOrigins: gwConfig.Server.AllowOrigin})) - } - // Sentry - if gwConfig.Monitoring.Sentry.Enabled { - err := sentry.Init(sentry.ClientOptions{ - Dsn: string(gwConfig.Monitoring.Sentry.Dsn), - TracesSampleRate: gwConfig.Monitoring.Sentry.SampleRate, - Environment: gwConfig.Monitoring.Sentry.Environment, - }) - if err != nil { - slog.Error("sentry initialization failed", "error", err) - } - e.Use(sentryecho.New(sentryecho.Options{})) - } - // Prometheus - if gwConfig.Monitoring.Prometheus.Enabled { - e.Use(echoprometheus.NewMiddleware("gateway")) - go func() { - metrics := echo.New() - metrics.HideBanner = true - metrics.HidePort = true - metrics.GET("/metrics", echoprometheus.NewHandler()) - err := metrics.Start(fmt.Sprintf(":%d", gwConfig.Monitoring.Prometheus.Port)) - if err != nil && !errors.Is(err, http.ErrServerClosed) { - slog.Error("prometheus server failed to start", "error", err) - os.Exit(1) - } - }() - } // Start server address := fmt.Sprintf("%s:%d", gwConfig.Server.Host, gwConfig.Server.Port) slog.Info("starting the server on address " + address) From 58226bfaa7ce5dbf44dadb69e406d145996dcb9f Mon Sep 17 00:00:00 2001 From: Flora Thiebaut Date: Mon, 28 Oct 2024 14:26:40 +0100 Subject: [PATCH 2/3] repanic --- cmd/gateway/main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/gateway/main.go b/cmd/gateway/main.go index f833ae49..320e7d8d 100644 --- a/cmd/gateway/main.go +++ b/cmd/gateway/main.go @@ -93,7 +93,9 @@ func main() { if err != nil { slog.Error("sentry initialization failed", "error", err) } - e.Use(sentryecho.New(sentryecho.Options{})) + e.Use(sentryecho.New(sentryecho.Options{ + Repanic: true, + })) } // Prometheus if gwConfig.Monitoring.Prometheus.Enabled { From 4dc77c829f6658ca514d1b3279801b74017592de Mon Sep 17 00:00:00 2001 From: Flora Thiebaut Date: Mon, 28 Oct 2024 14:38:34 +0100 Subject: [PATCH 3/3] test --- cmd/gateway/main.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/gateway/main.go b/cmd/gateway/main.go index 320e7d8d..4a2a7c37 100644 --- a/cmd/gateway/main.go +++ b/cmd/gateway/main.go @@ -96,6 +96,15 @@ func main() { e.Use(sentryecho.New(sentryecho.Options{ Repanic: true, })) + e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + err := next(c) + if err != nil { + sentry.CaptureException(err) + } + return err + } + }) } // Prometheus if gwConfig.Monitoring.Prometheus.Enabled {