make shutting down graceful

This commit is contained in:
2025-04-16 10:24:54 +01:00
parent 2440b3a668
commit f7e9fe7794

34
main.go
View File

@@ -1,8 +1,12 @@
package main
import (
"log"
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"synlotto-website/bootstrap"
"synlotto-website/config"
@@ -50,6 +54,30 @@ func main() {
wrapped = middleware.SecureHeaders(wrapped)
wrapped = middleware.Recover(wrapped)
log.Println("🌐 Running on http://localhost:8080")
http.ListenAndServe(":8080", wrapped)
addr := fmt.Sprintf("%s:%d", appState.Config.HttpServer.Address, appState.Config.HttpServer.Port)
srv := &http.Server{
Addr: addr,
Handler: wrapped,
}
go func() {
logging.Info("Server running at %s\n", addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logging.Error("Server error: %v", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
logging.Info("Shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
logging.Error("Forced shutdown: %v", err)
}
logging.Info("Server shutdown complete")
}