-
Notifications
You must be signed in to change notification settings - Fork 0
/
gin.go
60 lines (53 loc) · 1.29 KB
/
gin.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
package initialize
import (
"context"
"log"
"net/http"
"os"
"time"
"github.com/gin-gonic/gin"
"github.com/lwzphper/go-mall/bff/global"
"github.com/lwzphper/go-mall/pkg/common/config/app"
"github.com/lwzphper/go-mall/pkg/server"
)
func InitGin() {
gin.SetMode(getGinModeByEnv())
r := Routers()
srv := &http.Server{
ReadHeaderTimeout: 60 * time.Second,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 60 * time.Second,
MaxHeaderBytes: 1 << 20, // 1M
Addr: global.C.App.Addr,
Handler: r,
}
go func() {
// 服务连接
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
global.L.Fatalf("Server listen error: %s\n", err)
}
}()
hook := server.NewHook()
hook.Close(func(sg os.Signal) {
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
global.L.Fatalf("Server Shutdown error:%v", err)
}
log.Println("Server exiting")
})
}
func getGinModeByEnv() string {
var ginMode string
switch global.C.App.Env {
case app.EnvDevelopment:
ginMode = gin.DebugMode
case app.EnvTest:
ginMode = gin.TestMode
default:
ginMode = gin.ReleaseMode
}
return ginMode
}