package main import ( "context" "github.com/Superdanda/hade/framework/gin" middleware2 "github.com/Superdanda/hade/framework/middleware" "github.com/Superdanda/hade/provider/demo" "log" "net/http" "os" "os/signal" "syscall" "time" ) func main() { core := gin.New() core.Use(middleware2.Recovery()) core.Use(middleware2.Cost()) core.Bind(&demo.DemoServiceProvider{}) registerRouter(core) server := &http.Server{ Handler: core, Addr: ":8888", } go func() { server.ListenAndServe() }() // 当前的 Goroutine 等待信号量 quit := make(chan os.Signal) // 监控信号:SIGINT, SIGTERM, SIGQUIT signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) // 这里会阻塞当前goroutine等待信号 <-quit // 调用Server.Shutdown graceful结束 timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := server.Shutdown(timeoutCtx); err != nil { log.Fatal("Server Shutdown:", err) } }