-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
61 lines (51 loc) · 1.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"dayu.com/gindemo/framework/config"
"dayu.com/gindemo/router"
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
e := gin.New()
// middleware
e.Use(gin.Logger())
e.Use(gin.Recovery())
gin.SetMode(config.RunMode)
// load router
router.SetupRouter(e)
server := &http.Server{
Addr: fmt.Sprintf(":%d", config.HTTPPort),
Handler: e,
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,
}
log.Println("Port: " + config.Cfg.GetString("app.port") + " Pid: " + fmt.Sprintf("%d", os.Getpid()))
// listen server
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
panic(fmt.Errorf("Http server listen: %s\n", err))
}
}()
// receive sign and exit
gracefulExitApp(server)
}
// 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
func gracefulExitApp(server *http.Server) {
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
sig := <-quit
log.Println("Get Signal:", sig)
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
}