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, "")
|
utils.MustRunMigrations(db, "")
|
||||||
|
|
||||||
// init servers
|
// init servers
|
||||||
prometheusServer := &http.Server{
|
|
||||||
Addr: ":8081",
|
var prometheusServer *http.Server
|
||||||
Handler: promhttp.Handler(),
|
if serverSettings.PrometheusEnabled {
|
||||||
|
prometheusServer := &http.Server{
|
||||||
|
Addr: ":8081",
|
||||||
|
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)
|
||||||
|
|||||||
@@ -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{
|
||||||
BaseUrl: env("BASE_URL"),
|
Port: env("PORT"),
|
||||||
Environment: env("ENVIRONMENT"),
|
PrometheusEnabled: env("PROMETHEUS_ENABLED") == "true",
|
||||||
Smtp: smtp,
|
BaseUrl: env("BASE_URL"),
|
||||||
|
DbPath: env("DB_PATH"),
|
||||||
|
Environment: env("ENVIRONMENT"),
|
||||||
|
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)
|
||||||
Reference in New Issue
Block a user