Compare commits

..

2 Commits

Author SHA1 Message Date
Muyao CHEN
c0fea38790 stop: use graceful shutdown 2024-09-26 22:58:55 +02:00
Muyao CHEN
b98513ad36 stop: catch signals 2024-09-26 22:27:26 +02:00
2 changed files with 21 additions and 2 deletions

View File

@ -58,6 +58,7 @@ func FooControllerHandler(ctx *framework.Context) error {
} }
func UserLoginController(ctx *framework.Context) error { func UserLoginController(ctx *framework.Context) error {
time.Sleep(10 * time.Second)
ctx.WriteJSON(http.StatusOK, "ok") ctx.WriteJSON(http.StatusOK, "ok")
return nil return nil
} }

22
main.go
View File

@ -1,8 +1,13 @@
package main package main
import ( import (
"context"
"fmt"
"log" "log"
"net/http" "net/http"
"os"
"os/signal"
"syscall"
"git.vinchent.xyz/vinchent/go-web/framework" "git.vinchent.xyz/vinchent/go-web/framework"
) )
@ -15,7 +20,20 @@ func main() {
Handler: core, Handler: core,
} }
if err := server.ListenAndServe(); err != nil { go func() {
log.Panic(err) 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")
if err := server.Shutdown(context.Background()); err != nil {
log.Fatal("server shutdown: ", err)
} }
} }