/
init_app_mq.go
60 lines (57 loc) · 1.85 KB
/
init_app_mq.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 app
import (
"fmt"
gin_config "github.com/fellowme/gin_common_library/config"
grpc_consul "github.com/fellowme/gin_common_library/consul"
gin_logger "github.com/fellowme/gin_common_library/logger"
"github.com/fvbock/endless"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"syscall"
"time"
)
/*
creatApp 初始化app
*/
func creatMqApp(configPath, serverName string) *gin.Engine {
app := gin.New()
app.Use(gin_logger.RecoveryWithZap(gin_logger.RecoveryLogger,
gin_config.ServerConfigSettings.Server.IsDebug), cors.Default())
return app
}
func CreateAppMqServer(configPath, serverName string, f func(), consul grpc_consul.ServiceConsul) {
// 执行配置
initCommonExtend(configPath, serverName)
// 是否执行 debug 模式
if !gin_config.ServerConfigSettings.Server.IsDebug {
gin.SetMode(gin.ReleaseMode)
}
// 初始化app
app := creatMqApp(configPath, serverName)
// 激活测活
initRouter(app, nil)
// 执行 方法
f()
if consul.Id != "" {
consul.Id = fmt.Sprintf("%s-version-%d", consul.Name, time.Now().Unix())
}
// endless 启动
endless.DefaultReadTimeOut = time.Duration(gin_config.ServerConfigSettings.Server.ReadTimeout) * time.Second
endless.DefaultWriteTimeOut = time.Duration(gin_config.ServerConfigSettings.Server.WriteTimeout) * time.Second
endless.DefaultMaxHeaderBytes = 1 << 20
endPoint := fmt.Sprintf("%s:%d", gin_config.ServerConfigSettings.Server.ServerHost,
gin_config.ServerConfigSettings.Server.ServerMqPort)
defer func() {
grpc_consul.UnRegisterConsul(consul.Id)
deferClose()
}()
server := endless.NewServer(endPoint, app)
server.BeforeBegin = func(add string) {
zap.L().Info(fmt.Sprintf("Actual pid is %d", syscall.Getpid()))
grpc_consul.RegisterWebConsul(consul)
}
if err := server.ListenAndServe(); err != nil {
panic(fmt.Sprint("init server fail err=", err))
}
}