-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathcomponent.go
69 lines (56 loc) · 1.2 KB
/
component.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
package cherryGin
import (
cfacade "github.com/cherry-game/cherry/facade"
clog "github.com/cherry-game/cherry/logger"
"github.com/gin-gonic/gin"
)
const (
NamePrefix = "gin_component_"
)
type (
// Component wrapper gin
Component struct {
cfacade.Component
*HttpServer
name string
}
)
func NewHttp(name, address string) *Component {
return New(name, address)
}
func NewHttps(name, address, certFile, keyFile string) *Component {
return New(
name,
address,
WithCert(certFile, keyFile),
)
}
func New(name string, address string, opts ...OptionFunc) *Component {
return &Component{
name: name,
HttpServer: NewHttpServer(address, opts...),
}
}
// Name unique components name
func (g *Component) Name() string {
return NamePrefix + g.name
}
func (g *Component) Init() {
}
func (g *Component) OnAfterInit() {
g.SetIApplication(g.App())
go g.Run()
}
func (g *Component) OnBeforeStop() {
}
func (g *Component) OnStop() {
g.Stop()
clog.Infof("[component = %s] has been shut down", g.Name())
}
func (g *Component) Register(controllers ...IController) *Component {
g.HttpServer.Register(controllers...)
return g
}
func (g *Component) Engine() *gin.Engine {
return g.HttpServer.Engine
}