framework1/main.go

45 lines
894 B
Go
Raw Normal View History

2024-10-16 16:53:09 +08:00
package main
import (
2024-10-18 14:41:55 +08:00
"context"
"github.com/echo/hade/framework/gin"
middleware2 "github.com/echo/hade/framework/middleware"
"log"
2024-10-16 16:53:09 +08:00
"net/http"
2024-10-18 14:41:55 +08:00
"os"
"os/signal"
"syscall"
"time"
2024-10-16 16:53:09 +08:00
)
func main() {
2024-10-18 14:41:55 +08:00
core := gin.New()
core.Use(middleware2.Recovery())
core.Use(middleware2.Cost())
2024-10-16 16:53:09 +08:00
registerRouter(core)
server := &http.Server{
Handler: core,
Addr: ":8888",
}
2024-10-18 14:41:55 +08:00
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)
}
2024-10-16 16:53:09 +08:00
}