gin优雅的重启
安装
go get-u github.com/fvbock/endless
main代码
package main
import (
"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
"log"
"syscall"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"10.1.162.18": map[string]string{"model": "主机", "IP": "10.1.162.18"},
})
})
s := endless.NewServer(":8001", r)
s.BeforeBegin = func(add string) {
log.Printf("pid is %d", syscall.Getpid())
}
err := s.ListenAndServe()
if err != nil {
log.Printf("server err: %v", err)
}
}
ctrl + c
^C2021/07/16 22:10:50 57294 Received SIGINT.
2021/07/16 22:10:50 57294 Waiting for connections to finish...
2021/07/16 22:10:50 57294 Serve() returning...
2021/07/16 22:10:50 server err: accept tcp [::]:8001: use of closed network connection
|