chore: parametrize port and prometheus #181

This commit is contained in:
2024-10-02 18:27:16 +02:00
parent bddcfc6778
commit 33380e2124
2 changed files with 32 additions and 15 deletions

13
main.go
View File

@@ -48,15 +48,20 @@ func run(ctx context.Context, env func(string) string) {
utils.MustRunMigrations(db, "") utils.MustRunMigrations(db, "")
// init servers // init servers
var prometheusServer *http.Server
if serverSettings.PrometheusEnabled {
prometheusServer := &http.Server{ prometheusServer := &http.Server{
Addr: ":8081", Addr: ":8081",
Handler: promhttp.Handler(), Handler: promhttp.Handler(),
} }
go startServer(prometheusServer)
}
httpServer := &http.Server{ httpServer := &http.Server{
Addr: ":8080", Addr: ":" + serverSettings.Port,
Handler: handler.GetHandler(db, serverSettings), Handler: handler.GetHandler(db, serverSettings),
} }
go startServer(prometheusServer)
go startServer(httpServer) go startServer(httpServer)
// graceful shutdown // graceful shutdown
@@ -76,6 +81,10 @@ func startServer(s *http.Server) {
func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) { func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
if s == nil {
return
}
<-ctx.Done() <-ctx.Done()
shutdownCtx := context.Background() shutdownCtx := context.Background()
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second) shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)

View File

@@ -6,6 +6,9 @@ import (
) )
type ServerSettings struct { type ServerSettings struct {
Port string
PrometheusEnabled bool
BaseUrl string BaseUrl string
Environment string Environment string
DbPath string DbPath string
@@ -23,11 +26,6 @@ type SmtpSettings struct {
func NewServerSettingsFromEnv(env func(string) string) *ServerSettings { func NewServerSettingsFromEnv(env func(string) string) *ServerSettings {
dbPath := env("DB_PATH")
if dbPath == "" {
dbPath = "./data.db"
}
var smtp *SmtpSettings var smtp *SmtpSettings
if env("SMTP_ENABLED") == "true" { if env("SMTP_ENABLED") == "true" {
smtp = &SmtpSettings{ smtp = &SmtpSettings{
@@ -60,20 +58,30 @@ func NewServerSettingsFromEnv(env func(string) string) *ServerSettings {
} }
settings := &ServerSettings{ settings := &ServerSettings{
Port: env("PORT"),
PrometheusEnabled: env("PROMETHEUS_ENABLED") == "true",
BaseUrl: env("BASE_URL"), BaseUrl: env("BASE_URL"),
DbPath: env("DB_PATH"),
Environment: env("ENVIRONMENT"), Environment: env("ENVIRONMENT"),
Smtp: smtp, Smtp: smtp,
} }
if settings.DbPath == "" {
settings.DbPath = "./data.db"
}
if settings.BaseUrl == "" { if settings.BaseUrl == "" {
log.Fatal("BASE_URL must be set") log.Fatal("BASE_URL must be set")
} }
if settings.Port == "" {
log.Fatal("PORT must be set")
}
if settings.Environment == "" { if settings.Environment == "" {
log.Fatal("ENVIRONMENT must be set") log.Fatal("ENVIRONMENT must be set")
} }
if settings.Environment == "prod" && settings.Smtp == nil { if settings.Environment == "prod" && (settings.Smtp == nil || !settings.PrometheusEnabled) {
log.Fatal("SMTP must be enabled in production") log.Fatal("SMTP and Prometheus must be enabled in production")
} }
slog.Info("BASE_URL is " + settings.BaseUrl) slog.Info("BASE_URL is " + settings.BaseUrl)