forked from raystack/raccoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.go
55 lines (48 loc) · 1.24 KB
/
services.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
package services
import (
"context"
"net/http"
"github.com/goto/raccoon/collection"
"github.com/goto/raccoon/logger"
"github.com/goto/raccoon/services/grpc"
"github.com/goto/raccoon/services/pprof"
"github.com/goto/raccoon/services/rest"
)
type bootstrapper interface {
// Init initialize each HTTP based server. Return error if initialization failed. Put the Serve() function as return mostly suffice for Init process.
Init(ctx context.Context) error
Shutdown(ctx context.Context) error
Name() string
}
type Services struct {
b []bootstrapper
}
func (s *Services) Start(ctx context.Context, cancel context.CancelFunc) {
logger.Info("starting servers")
for _, init := range s.b {
i := init
go func() {
logger.Infof("%s Server --> startServers", i.Name())
err := i.Init(ctx)
if err != nil && err != http.ErrServerClosed {
cancel()
}
}()
}
}
func (s *Services) Shutdown(ctx context.Context) {
for _, b := range s.b {
logger.Infof("%s Server --> shutting down", b.Name())
b.Shutdown(ctx)
}
}
func Create(b chan collection.CollectRequest) Services {
c := collection.NewChannelCollector(b)
return Services{
b: []bootstrapper{
grpc.NewGRPCService(c),
pprof.NewPprofService(),
rest.NewRestService(c),
},
}
}