/
microdule.go
64 lines (50 loc) · 948 Bytes
/
microdule.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
package microdule
type Service interface {
Name() string
Init(...Option)
Options() *Options
Close()
Start() error
}
func NewService(opt ...Option) Service {
return newService(opt...)
}
func (s *service) Name() string {
return s.opts.Name
}
func (s *service) Init(opts ...Option) {
for _, o := range opts {
o(&s.opts)
}
}
func (s *service) Options() *Options {
return &s.opts
}
func (s *service) Close() {
s.opts.Teamwork.Close()
if s.opts.Rpc != nil {
s.opts.Rpc.Close()
}
if s.opts.Gorm != nil {
s.opts.Gorm.Close()
}
if s.opts.Redis != nil {
s.opts.Redis.Close()
}
if s.opts.Etcd != nil {
s.opts.Etcd.Close()
}
}
func (s *service) Start() error {
if s.opts.Http != nil {
s.opts.Teamwork.Reginster("http", func() error {
return s.opts.Http.Run()
})
}
if s.opts.Rpc != nil {
s.opts.Teamwork.Reginster("rpc ", func() error {
return s.opts.Rpc.Run()
})
}
return s.opts.Teamwork.Start()
}