From b98513ad3612d2696b35c84b2d0ed9b9c5fac0d8 Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Thu, 26 Sep 2024 22:27:26 +0200 Subject: [PATCH] stop: catch signals --- main.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 06f6368..b0f1847 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,11 @@ package main import ( - "log" + "fmt" "net/http" + "os" + "os/signal" + "syscall" "git.vinchent.xyz/vinchent/go-web/framework" ) @@ -15,7 +18,16 @@ func main() { Handler: core, } - if err := server.ListenAndServe(); err != nil { - log.Panic(err) - } + go func() { + server.ListenAndServe() + }() + + // create quit channel + quit := make(chan os.Signal, 1) + + // set notifier + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) + + <-quit + fmt.Println("YOLO") }