prosody-password/cmd/http/main.go

61 lines
1.2 KiB
Go

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/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))
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 {
restServer.Shutdown()
}
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
}