catch Ctrl+C

This commit is contained in:
2026-09-08 17:24:31 +08:00
parent a4f1212d7b
commit 4cf2863286

20
main.go
View File

@@ -3,6 +3,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"os" "os"
"regexp" "regexp"
@@ -15,6 +16,8 @@ import (
"bdgrp_go/db" "bdgrp_go/db"
"bdgrp_go/routes" "bdgrp_go/routes"
"os/signal"
"syscall"
) )
const PORT = ":8482" const PORT = ":8482"
@@ -61,5 +64,22 @@ func main() {
log.Printf("Running on %s", PORT) log.Printf("Running on %s", PORT)
color.HEX("#00AABB").Printf("Server: %s\n", server) 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) app.Listen(PORT)
} }