Compare commits

..

3 Commits

Author SHA1 Message Date
c5ab1debdb stop: use graceful shutdown for 5s 2024-09-26 23:05:05 +02:00
c0fea38790 stop: use graceful shutdown 2024-09-26 22:58:55 +02:00
b98513ad36 stop: catch signals 2024-09-26 22:27:26 +02:00
2 changed files with 24 additions and 2 deletions

View File

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

25
main.go
View File

@ -1,8 +1,14 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"git.vinchent.xyz/vinchent/go-web/framework"
)
@ -15,7 +21,22 @@ 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")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatal("server shutdown: ", err)
}
}