package main import ( "context" "fmt" "log" "os" "os/signal" "syscall" "gitea.urkob.com/urko/prosody-password/internal/api" "gitea.urkob.com/urko/prosody-password/internal/services/fail2ban" "gitea.urkob.com/urko/prosody-password/internal/services/prosody" "gitea.urkob.com/urko/prosody-password/kit/config" ) func main() { envFile := "" if os.Getenv("PROSODY_ENV") != "prod" { envFile = ".env" } cfg := config.NewConfig(envFile) ctx, cancel := context.WithCancel(signalContext(context.Background())) defer cancel() restServer := api.NewRestServer(prosody.NewProsody(cfg.Domain), fail2ban.NewFail2Ban()) go func() { if err := restServer.Start(cfg.ApiPort, cfg.Views); err != nil { panic(fmt.Errorf("restServer.Start: %w", err)) } }() <-ctx.Done() log.Println("on shutdown") if restServer != nil { if err := restServer.Shutdown(); err != nil { panic(err) } } log.Println("gracefully shutdown") } func signalContext(ctx context.Context) context.Context { ctx, cancel := context.WithCancel(ctx) sigs := make(chan os.Signal, 1) signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) go func() { log.Println("listening for shutdown signal") <-sigs log.Println("shutdown signal received") signal.Stop(sigs) close(sigs) cancel() }() return ctx }