-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ccapi.go
172 lines (144 loc) · 4.29 KB
/
ccapi.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
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package ccapi
import (
"configcenter/src/common/blog"
"configcenter/src/common/core/cc/api"
"configcenter/src/common/core/cc/config"
"configcenter/src/common/errors"
"configcenter/src/common/http/httpserver"
"configcenter/src/common/rdapi"
"configcenter/src/common/types"
"sync"
"time"
// migrateCommon "configcenter/src/scene_server/admin_server/common"
confCenter "configcenter/src/scene_server/admin_server/migrate_service/config"
"configcenter/src/scene_server/admin_server/migrate_service/rdiscover"
//"time"
)
//CCAPIServer define data struct of bcs ccapi server
type CCAPIServer struct {
conf *config.CCAPIConfig
httpServ *httpserver.HttpServer
rd *rdiscover.RegDiscover
cfCenter *confCenter.ConfCenter
}
func NewCCAPIServer(conf *config.CCAPIConfig) (*CCAPIServer, error) {
s := &CCAPIServer{}
//config
s.conf = conf
addr, _ := s.conf.GetAddress()
port, _ := s.conf.GetPort()
//http server
s.httpServ = httpserver.NewHttpServer(port, addr, "")
a := api.NewAPIResource()
a.SetConfig(s.conf)
a.InitAction()
configctx, _ := a.ParseConfig()
regDiscAddrs := configctx["register-server.addrs"]
confCenterAddrs := configctx["config-server.addrs"]
//RDiscover
s.rd = rdiscover.NewRegDiscover(regDiscAddrs, addr, port, false)
a.AddrSrv = s.rd
//ConfCenter
s.cfCenter = confCenter.NewConfCenter(confCenterAddrs)
return s, nil
}
//Stop the ccapi server
func (ccAPI *CCAPIServer) Stop() error {
return nil
}
//Start the ccapi server
func (ccAPI *CCAPIServer) Start() error {
chErr := make(chan error, 3)
a := api.NewAPIResource()
config, _ := a.ParseConfig()
confDir := config["confs.dir"]
errres := config["errors.res"]
// configure center
err := ccAPI.cfCenter.Start(confDir, errres)
if err != nil {
blog.Errorf("configure center module start failed!. err:%s", err.Error())
return err
}
//http server
ccAPI.initHttpServ()
// load the errors resource
if errorres, ok := config["errors.res"]; ok {
if errif, err := errors.New(errorres); nil != err {
blog.Error("failed to create errors object, error info is %s ", err.Error())
chErr <- err
} else {
a.Error = errif
}
} else {
for {
errcode := ccAPI.cfCenter.GetLanguageCxt()
if errcode == nil {
blog.Warnf("fail to get language package, will get again")
time.Sleep(time.Second * 2)
continue
} else {
errif := errors.NewFromCtx(errcode)
a.Error = errif
break
}
}
}
a.TopoAPI = rdapi.GetRdAddrSrvHandle(types.CC_MODULE_TOPO, a.AddrSrv)
a.ProcAPI = rdapi.GetRdAddrSrvHandle(types.CC_MODULE_PROC, a.AddrSrv)
err = a.GetDataCli(config, "mongodb")
if err != nil {
blog.Error("connect mongodb error exit! err:%s", err.Error())
return err
}
go func() {
err := ccAPI.rd.Start()
blog.Errorf("rdiscover start failed! err:%s", err.Error())
chErr <- err
}()
waitfunc := func(f func() string, wg *sync.WaitGroup) {
for {
if f() != "" {
wg.Done()
return
}
time.Sleep(time.Millisecond * 500)
}
}
all := []string{
types.CC_MODULE_PROC,
types.CC_MODULE_TOPO,
}
wg := &sync.WaitGroup{}
for _, module := range all {
wg.Add(1)
go waitfunc(rdapi.GetRdAddrSrvHandle(module, a.AddrSrv), wg)
}
go func() {
wg.Wait()
err := ccAPI.httpServ.ListenAndServe()
blog.Error("http listen and serve failed! err:%s", err.Error())
chErr <- err
}()
select {
case err := <-chErr:
blog.Error("exit! err:%s", err.Error())
return err
}
return nil
}
func (ccAPI *CCAPIServer) initHttpServ() error {
a := api.NewAPIResource()
ccAPI.httpServ.RegisterWebServer("/migrate/{version}", rdapi.GlobalFilter(types.CC_MODULE_PROC, types.CC_MODULE_TOPO), a.Actions)
return nil
}