chore: parametrize port and prometheus #181
This commit is contained in:
19
main.go
19
main.go
@@ -48,15 +48,20 @@ func run(ctx context.Context, env func(string) string) {
|
||||
utils.MustRunMigrations(db, "")
|
||||
|
||||
// init servers
|
||||
prometheusServer := &http.Server{
|
||||
Addr: ":8081",
|
||||
Handler: promhttp.Handler(),
|
||||
|
||||
var prometheusServer *http.Server
|
||||
if serverSettings.PrometheusEnabled {
|
||||
prometheusServer := &http.Server{
|
||||
Addr: ":8081",
|
||||
Handler: promhttp.Handler(),
|
||||
}
|
||||
go startServer(prometheusServer)
|
||||
}
|
||||
|
||||
httpServer := &http.Server{
|
||||
Addr: ":8080",
|
||||
Addr: ":" + serverSettings.Port,
|
||||
Handler: handler.GetHandler(db, serverSettings),
|
||||
}
|
||||
go startServer(prometheusServer)
|
||||
go startServer(httpServer)
|
||||
|
||||
// graceful shutdown
|
||||
@@ -76,6 +81,10 @@ func startServer(s *http.Server) {
|
||||
|
||||
func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
|
||||
<-ctx.Done()
|
||||
shutdownCtx := context.Background()
|
||||
shutdownCtx, cancel := context.WithTimeout(shutdownCtx, 10*time.Second)
|
||||
|
||||
@@ -6,6 +6,9 @@ import (
|
||||
)
|
||||
|
||||
type ServerSettings struct {
|
||||
Port string
|
||||
PrometheusEnabled bool
|
||||
|
||||
BaseUrl string
|
||||
Environment string
|
||||
DbPath string
|
||||
@@ -23,11 +26,6 @@ type SmtpSettings struct {
|
||||
|
||||
func NewServerSettingsFromEnv(env func(string) string) *ServerSettings {
|
||||
|
||||
dbPath := env("DB_PATH")
|
||||
if dbPath == "" {
|
||||
dbPath = "./data.db"
|
||||
}
|
||||
|
||||
var smtp *SmtpSettings
|
||||
if env("SMTP_ENABLED") == "true" {
|
||||
smtp = &SmtpSettings{
|
||||
@@ -60,20 +58,30 @@ func NewServerSettingsFromEnv(env func(string) string) *ServerSettings {
|
||||
}
|
||||
|
||||
settings := &ServerSettings{
|
||||
BaseUrl: env("BASE_URL"),
|
||||
Environment: env("ENVIRONMENT"),
|
||||
Smtp: smtp,
|
||||
Port: env("PORT"),
|
||||
PrometheusEnabled: env("PROMETHEUS_ENABLED") == "true",
|
||||
BaseUrl: env("BASE_URL"),
|
||||
DbPath: env("DB_PATH"),
|
||||
Environment: env("ENVIRONMENT"),
|
||||
Smtp: smtp,
|
||||
}
|
||||
|
||||
if settings.DbPath == "" {
|
||||
settings.DbPath = "./data.db"
|
||||
}
|
||||
|
||||
if settings.BaseUrl == "" {
|
||||
log.Fatal("BASE_URL must be set")
|
||||
}
|
||||
if settings.Port == "" {
|
||||
log.Fatal("PORT must be set")
|
||||
}
|
||||
if settings.Environment == "" {
|
||||
log.Fatal("ENVIRONMENT must be set")
|
||||
}
|
||||
|
||||
if settings.Environment == "prod" && settings.Smtp == nil {
|
||||
log.Fatal("SMTP must be enabled in production")
|
||||
if settings.Environment == "prod" && (settings.Smtp == nil || !settings.PrometheusEnabled) {
|
||||
log.Fatal("SMTP and Prometheus must be enabled in production")
|
||||
}
|
||||
|
||||
slog.Info("BASE_URL is " + settings.BaseUrl)
|
||||
Reference in New Issue
Block a user