forked from erda-project/erda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
91 lines (82 loc) · 2.41 KB
/
provider.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
// 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 hepa
import (
"context"
"net/http"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda-infra/base/logs"
"github.com/erda-project/erda-infra/base/servicehub"
_ "github.com/erda-project/erda-infra/providers/health"
"github.com/erda-project/erda/modules/hepa/common"
"github.com/erda-project/erda/modules/hepa/config"
"github.com/erda-project/erda/modules/hepa/repository/orm"
"github.com/erda-project/erda/modules/hepa/server"
"github.com/erda-project/erda/modules/hepa/ver"
)
type myCfg struct {
Log config.LogConfig
Server config.ServerConfig
}
type provider struct {
Cfg *myCfg // auto inject this field
Log logs.Logger // auto inject this field
}
func (p *provider) Init(ctx servicehub.Context) error {
config.ServerConf = &p.Cfg.Server
config.LogConf = &p.Cfg.Log
common.InitLogger()
orm.Init()
logrus.Infof("server conf: %+v", config.ServerConf)
logrus.Infof("log conf: %+v", config.LogConf)
return nil
}
func (p *provider) Run(ctx context.Context) error {
server.CreateSingleton(common.AccessLog)
gwCtl, err := server.NewGatewayController()
if err != nil {
return err
}
gwCtl.Register()
openapiCtl, err := server.NewOpenapiController()
if err != nil {
return err
}
openapiCtl.Register()
logrus.Info(ver.String())
srv := &http.Server{
Addr: p.Cfg.Server.ListenAddr,
}
errChan := make(chan error, 0)
go func() {
err = server.Start(srv)
errChan <- err
}()
select {
case <-ctx.Done():
if err := srv.Shutdown(context.Background()); err != nil {
logrus.Fatal("Server Shutdown:", err)
}
return nil
case err := <-errChan:
return err
}
}
func init() {
servicehub.Register("hepa", &servicehub.Spec{
Services: []string{"hepa"},
Description: "hepa",
ConfigFunc: func() interface{} { return &myCfg{} },
Creator: func() servicehub.Provider { return &provider{} },
})
}