-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
125 lines (106 loc) · 2.43 KB
/
app.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package ys_gin
import (
"context"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
"google.golang.org/grpc"
"github.com/banbo/ys-gin/cache"
"github.com/banbo/ys-gin/conf"
"github.com/banbo/ys-gin/id"
"github.com/banbo/ys-gin/log"
"github.com/banbo/ys-gin/model"
)
type App struct {
GinEngine *gin.Engine
apiSvr *http.Server
RpcSvr *grpc.Server
rpcAddr string
}
func NewApp(configFile string) *App {
//初始化组件
conf.NewConfiger(configFile)
log.NewLogger()
if conf.Configer.BeeConfiger.String("system::worker_id") != "" {
id.NewIdWorker(conf.Configer.ApiConf.WorkerID)
}
if conf.Configer.BeeConfiger.String("redis::host") != "" {
cache.NewRedisClient()
}
//设置gin运行模式
gin.SetMode(conf.Configer.ApiConf.RunMode)
//初始化数据库连接池
_, err := model.NewEngine()
if err != nil {
panic(fmt.Sprintf("初始化连接池出错,%v", err))
}
/*switch conf.Configer.DbConf.DriverName {
case "mysql":
model.NewMysqlOrm()
case "postgres":
model.NewPostgresOrm()
}*/
app := &App{}
//初始化api服务
if len(conf.Configer.ApiConf.HttpPort) > 0 {
app.GinEngine = gin.Default()
app.apiSvr = &http.Server{
Addr: ":" + conf.Configer.ApiConf.HttpPort,
Handler: app.GinEngine,
}
}
//初始化rpc服务
if len(conf.Configer.ApiConf.RpcPort) > 0 {
app.rpcAddr = ":" + conf.Configer.ApiConf.RpcPort
app.RpcSvr = grpc.NewServer()
}
return app
}
func (app *App) Run() {
//启动api服务
if app.apiSvr != nil {
go func() {
err := app.apiSvr.ListenAndServe()
if err != nil {
panic(fmt.Sprintf("启动http服务失败,%v", err))
}
}()
fmt.Println("Api Svr Listen and serve on", app.apiSvr.Addr)
}
//启动rpc服务
if app.RpcSvr != nil {
listen, err := net.Listen("tcp", app.rpcAddr)
if err != nil {
panic("监听rpc端口失败,err: " + err.Error())
}
go func() {
err := app.RpcSvr.Serve(listen)
if err != nil {
panic(fmt.Sprintf("启动rpc服务失败,%v", err))
}
}()
fmt.Println("Rpc Svr Listen and serve on", app.rpcAddr)
}
//监听退出
quitChan := make(chan os.Signal)
signal.Notify(quitChan,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)
<-quitChan
//优雅退出
if app.apiSvr != nil {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
app.apiSvr.Shutdown(ctx)
}
if app.RpcSvr != nil {
app.RpcSvr.GracefulStop()
}
}