-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
67 lines (53 loc) · 1.09 KB
/
service.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
package micro
import (
"os"
"os/signal"
"syscall"
"github.com/ofavor/micro-lite/client"
"github.com/ofavor/micro-lite/internal/log"
"github.com/ofavor/micro-lite/server"
)
// Service interface
type Service interface {
// Client get client instance
Client() client.Client
// Server get server instance
Server() server.Server
// Run the service
Run() error
}
func newService(opts ...Option) Service {
options := defaultOptions()
for _, o := range opts {
o(&options)
}
return &service{
opts: options,
}
}
type service struct {
opts Options
}
func (s *service) Client() client.Client {
return s.opts.Client
}
func (s *service) Server() server.Server {
return s.opts.Server
}
func (s *service) Run() error {
log.Info("Service is running ...")
// start the server
if err := s.opts.Server.Start(); err != nil {
return err
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL)
select {
case <-ch:
}
if err := s.opts.Server.Stop(); err != nil {
return err
}
log.Info("Service is terminated")
return nil
}