Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change server setup #743

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 54 additions & 43 deletions cmd/gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,60 @@ 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{
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 {
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 := ""
Expand Down Expand Up @@ -130,49 +184,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)
Expand Down
Loading