catch Ctrl+C

This commit is contained in:
2026-09-08 17:24:31 +08:00
parent 67eee6c633
commit 620da470ee

20
main.go
View File

@@ -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)
}