-
Notifications
You must be signed in to change notification settings - Fork 375
/
initialize.go
107 lines (89 loc) · 3.04 KB
/
initialize.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package adaptor gittar adaptor的初始化
package adaptor
import (
"time"
"github.com/gorilla/schema"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/gittar-adaptor/conf"
"github.com/erda-project/erda/modules/gittar-adaptor/endpoints"
"github.com/erda-project/erda/modules/gittar-adaptor/event"
"github.com/erda-project/erda/modules/gittar-adaptor/service/cdp"
"github.com/erda-project/erda/modules/gittar-adaptor/service/filetree"
"github.com/erda-project/erda/modules/gittar-adaptor/service/permission"
"github.com/erda-project/erda/modules/gittar-adaptor/service/pipeline"
"github.com/erda-project/erda/pkg/httpclient"
"github.com/erda-project/erda/pkg/httpserver"
//"terminus.io/dice/telemetry/promxp"
)
// Initialize 初始化应用启动服务.
func Initialize() error {
// 装载配置信息
conf.Load()
if conf.Debug() {
logrus.SetLevel(logrus.DebugLevel)
logrus.Infof("set log level: %s", logrus.DebugLevel)
}
ep := initEndpoints()
// 注册 hook
err := ep.RegisterEvents()
if err != nil {
return err
}
server := httpserver.New(conf.ListenAddr())
//server.Router().Path("/metrics").Methods(http.MethodGet).Handler(promxp.Handler("adaptor"))
server.Router().UseEncodedPath()
server.RegisterEndpoint(ep.Routes())
logrus.Infof("start the service and listen on address: \"%s\"", conf.ListenAddr())
return server.ListenAndServe()
}
// 初始化 Endpoints
func initEndpoints() *endpoints.Endpoints {
// init bundle
bdl := bundle.New(
bundle.WithGittar(),
bundle.WithPipeline(),
bundle.WithEventBox(),
bundle.WithCMDB(),
bundle.WithMonitor(),
bundle.WithHTTPClient(
httpclient.New(
httpclient.WithTimeout(time.Second, time.Duration(conf.BundleTimeoutSecond())*time.Second),
),
),
)
// init pipeline
p := pipeline.New(pipeline.WithBundle(bdl))
c := cdp.New(cdp.WithBundle(bdl))
// init event
e := event.New(event.WithBundle(bdl))
// init permission
perm := permission.New(permission.WithBundle(bdl))
// queryStringDecoder
queryStringDecoder := schema.NewDecoder()
queryStringDecoder.IgnoreUnknownKeys(true)
fileTree := filetree.New(filetree.WithBundle(bdl))
// compose endpoints
ep := endpoints.New(
endpoints.WithBundle(bdl),
endpoints.WithPipeline(p),
endpoints.WithEvent(e),
endpoints.WithCDP(c),
endpoints.WithPermission(perm),
endpoints.WithQueryStringDecoder(queryStringDecoder),
endpoints.WithGittarFileTree(fileTree),
)
return ep
}