From 4cf2863286b09db6ae517200176e7f54c00e29a2 Mon Sep 17 00:00:00 2001 From: nightnai Date: Tue, 8 Sep 2026 17:24:31 +0800 Subject: [PATCH] catch Ctrl+C --- main.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/main.go b/main.go index 825a725..0f69233 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( + "fmt" "log" "os" "regexp" @@ -15,6 +16,8 @@ import ( "bdgrp_go/db" "bdgrp_go/routes" + "os/signal" + "syscall" ) const PORT = ":8482" @@ -61,5 +64,22 @@ func main() { log.Printf("Running on %s", PORT) color.HEX("#00AABB").Printf("Server: %s\n", server) + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + + go func() { + <-quit + fmt.Print("\nProceed to exit? (y/n): ") + var input string + fmt.Scanln(&input) + if input == "y" { + db.Save() + app.Shutdown() + } else { + signal.Reset(syscall.SIGINT, syscall.SIGTERM) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + } + }() + app.Listen(PORT) }