-
Notifications
You must be signed in to change notification settings - Fork 488
/
server.go
231 lines (193 loc) · 5.45 KB
/
server.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package pkg
import (
"build-booster/common"
"build-booster/common/blog"
"build-booster/common/encrypt"
"build-booster/common/http/httpserver"
"build-booster/gateway/config"
"build-booster/gateway/pkg/api"
_ "build-booster/gateway/pkg/api/v1"
"build-booster/gateway/pkg/register-discover"
"build-booster/server/pkg/engine"
"build-booster/server/pkg/engine/apisjob"
"build-booster/server/pkg/engine/distcc"
"build-booster/server/pkg/engine/disttask"
"build-booster/server/pkg/engine/fastbuild"
)
type GatewayServer struct {
conf *config.GatewayConfig
httpServer *httpserver.HTTPServer
rd register_discover.RegisterDiscover
}
func NewGatewayServer(conf *config.GatewayConfig) (*GatewayServer, error) {
s := &GatewayServer{conf: conf}
// Http server
s.httpServer = httpserver.NewHTTPServer(s.conf.Port, s.conf.Address, "")
if s.conf.ServerCert.IsSSL {
s.httpServer.SetSSL(
s.conf.ServerCert.CAFile, s.conf.ServerCert.CertFile, s.conf.ServerCert.KeyFile, s.conf.ServerCert.CertPwd)
}
return s, nil
}
func (dcs *GatewayServer) initHTTPServer() error {
api.Rd = dcs.rd
// Api v1
return dcs.httpServer.RegisterWebServer(api.PathV1, nil, api.GetAPIV1Action())
}
func (dcs *GatewayServer) initDistCCResource() error {
if !dcs.conf.DistCCMySQL.Enable {
return nil
}
a := api.GetDistCCServerAPIResource()
a.Conf = dcs.conf
pwd, err := encrypt.DesDecryptFromBase([]byte(dcs.conf.DistCCMySQL.MySQLPwd))
if err != nil {
return err
}
a.MySQL, err = distcc.NewMySQL(engine.MySQLConf{
MySQLStorage: dcs.conf.DistCCMySQL.MySQLStorage,
MySQLDatabase: dcs.conf.DistCCMySQL.MySQLDatabase,
MySQLUser: dcs.conf.DistCCMySQL.MySQLUser,
MySQLPwd: string(pwd),
MySQLDebug: dcs.conf.DistCCMySQL.Debug,
})
if err != nil {
return err
}
if err := api.InitActionsFunc(); err != nil {
return err
}
// Init routes in actions
a.InitActions()
blog.Infof("success to enable gateway for distcc mysql")
return nil
}
func (dcs *GatewayServer) initFBResource() error {
if !dcs.conf.FastBuildMySQL.Enable {
return nil
}
a := api.GetFBServerAPIResource()
a.Conf = dcs.conf
pwd, err := encrypt.DesDecryptFromBase([]byte(dcs.conf.FastBuildMySQL.MySQLPwd))
if err != nil {
return err
}
a.MySQL, err = fastbuild.NewMySQL(engine.MySQLConf{
MySQLStorage: dcs.conf.FastBuildMySQL.MySQLStorage,
MySQLDatabase: dcs.conf.FastBuildMySQL.MySQLDatabase,
MySQLUser: dcs.conf.FastBuildMySQL.MySQLUser,
MySQLPwd: string(pwd),
MySQLDebug: dcs.conf.FastBuildMySQL.Debug,
})
if err != nil {
return err
}
if err := api.InitActionsFunc(); err != nil {
return err
}
// Init routes in actions
a.InitActions()
blog.Infof("success to enable gateway for fastbuild mysql")
return nil
}
func (dcs *GatewayServer) initAPISJobResource() error {
if !dcs.conf.ApisJobMySQL.Enable {
return nil
}
a := api.GetXNAPISServerAPIResource()
a.Conf = dcs.conf
pwd, err := encrypt.DesDecryptFromBase([]byte(dcs.conf.ApisJobMySQL.MySQLPwd))
if err != nil {
return err
}
a.MySQL, err = apisjob.NewMySQL(engine.MySQLConf{
MySQLStorage: dcs.conf.ApisJobMySQL.MySQLStorage,
MySQLDatabase: dcs.conf.ApisJobMySQL.MySQLDatabase,
MySQLUser: dcs.conf.ApisJobMySQL.MySQLUser,
MySQLPwd: string(pwd),
MySQLDebug: dcs.conf.ApisJobMySQL.Debug,
})
if err != nil {
return err
}
if err := api.InitActionsFunc(); err != nil {
return err
}
// Init routes in actions
a.InitActions()
blog.Infof("success to enable gateway for apisjob mysql")
return nil
}
func (dcs *GatewayServer) initDistTaskResource() error {
if !dcs.conf.DistTaskMySQL.Enable {
return nil
}
a := api.GetDistTaskServerAPIResource()
a.Conf = dcs.conf
pwd, err := encrypt.DesDecryptFromBase([]byte(dcs.conf.DistTaskMySQL.MySQLPwd))
if err != nil {
return err
}
a.MySQL, err = disttask.NewMySQL(engine.MySQLConf{
MySQLStorage: dcs.conf.DistTaskMySQL.MySQLStorage,
MySQLDatabase: dcs.conf.DistTaskMySQL.MySQLDatabase,
MySQLUser: dcs.conf.DistTaskMySQL.MySQLUser,
MySQLPwd: string(pwd),
MySQLDebug: dcs.conf.DistTaskMySQL.Debug,
})
if err != nil {
return err
}
if err := api.InitActionsFunc(); err != nil {
return err
}
// Init routes in actions
a.InitActions()
blog.Infof("success to enable gateway for disttask mysql")
return nil
}
func (dcs *GatewayServer) Start() error {
var err error
if dcs.rd, err = register_discover.NewRegisterDiscover(dcs.conf); err != nil {
blog.Errorf("get new register discover failed: %v", err)
return err
}
if err = dcs.rd.Run(); err != nil {
blog.Errorf("get register discover event chan failed: %v", err)
return err
}
// init distCC server related resources
if err = dcs.initDistCCResource(); err != nil {
return err
}
// init fb server related resources
if err = dcs.initFBResource(); err != nil {
return err
}
// init apis job server related resources
if err = dcs.initAPISJobResource(); err != nil {
return err
}
// init disttask server related resources
if err = dcs.initDistTaskResource(); err != nil {
return err
}
// register all APIs
if err = dcs.initHTTPServer(); err != nil {
return err
}
return dcs.httpServer.ListenAndServe()
}
// Run brings up the server
func Run(conf *config.GatewayConfig) error {
if err := common.SavePid(conf.ProcessConfig); err != nil {
blog.Errorf("save pid failed: %v", err)
return err
}
server, err := NewGatewayServer(conf)
if err != nil {
blog.Errorf("init proxy server failed: %v", err)
return err
}
return server.Start()
}