diff --git a/cmd/blog/main.go b/cmd/blog/main.go index 46cfebf..756496c 100644 --- a/cmd/blog/main.go +++ b/cmd/blog/main.go @@ -6,8 +6,10 @@ import ( "html/template" "net/http" "os" + "os/signal" "strconv" "strings" + "syscall" "time" "git.send.nrw/sendnrw/b1tsblog/internal/article" @@ -93,6 +95,18 @@ var ( func main() { + // Signal-Kanal einrichten + stop := make(chan os.Signal, 1) + signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) + + // Goroutine, die auf Signale wartet + go func() { + <-stop + fmt.Println("Stop Sign...") + prepareExit() + os.Exit(0) + }() + // --- Verzeichnisse konfigurierbar machen ------------------------- contentDir := getenv("BLOG_CONTENT_DIR", "/content") staticDir := getenv("BLOG_STATIC_DIR", "/app/internal/web/static") @@ -196,11 +210,21 @@ func main() { cacheControl(http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir))))) - http.ListenAndServe(":8080", mux) + StopServer(http.ListenAndServe(":8080", mux)) - if err := SaveTickCatalog(ticksDir + "/ticks.json"); err != nil { +} + +func prepareExit() { + fmt.Println("~", "Running exit tasks...") + if err := SaveTickCatalog(getenv("BLOG_TICKS_DIR", "/ticks") + "/ticks.json"); err != nil { fmt.Println("Fehler beim Speichern:", err) } - fmt.Println("Geladener Katalog:", TickCatalog) + fmt.Println("~", "Exit completed.") +} + +func StopServer(e error) { + fmt.Println("~", "Stopping server...") + prepareExit() + fmt.Println("~", "Server stopped!") }