-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
85 lines (72 loc) · 1.78 KB
/
options.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
package pluto
import (
"github.com/aukbit/pluto/v6/client"
"github.com/aukbit/pluto/v6/common"
"github.com/aukbit/pluto/v6/discovery"
"github.com/aukbit/pluto/v6/server"
"github.com/rs/zerolog"
)
// Option is used to set options for the service.
type Option interface {
apply(*Service)
}
// optionFunc wraps a func so it satisfies the Option interface.
type optionFunc func(*Service)
func (f optionFunc) apply(s *Service) {
f(s)
}
// ID service id
func ID(id string) Option {
return optionFunc(func(s *Service) {
s.cfg.ID = id
})
}
// Name service name
func Name(n string) Option {
return optionFunc(func(s *Service) {
s.cfg.Name = common.SafeName(n, defaultName)
})
}
// Description service description
func Description(d string) Option {
return optionFunc(func(s *Service) {
s.cfg.Description = d
})
}
// Servers slice of service servers
func Servers(srv *server.Server) Option {
return optionFunc(func(s *Service) {
s.cfg.Servers[srv.Name()] = srv
})
}
// Clients slice of service clients
func Clients(clt *client.Client) Option {
return optionFunc(func(s *Service) {
s.cfg.Clients[clt.Name()] = clt
s.cfg.clientsCh <- clt
})
}
// Discovery service discoery
func Discovery(d discovery.Discovery) Option {
return optionFunc(func(s *Service) {
s.cfg.Discovery = d
})
}
// HookAfterStart execute functions after service starts
func HookAfterStart(fn ...HookFunc) Option {
return optionFunc(func(s *Service) {
s.cfg.Hooks["after_start"] = append(s.cfg.Hooks["after_start"], fn...)
})
}
// Logger sets a new configuration for service logger
func Logger(l zerolog.Logger) Option {
return optionFunc(func(s *Service) {
s.logger = l
})
}
// HealthAddr health server address
func HealthAddr(a string) Option {
return optionFunc(func(s *Service) {
s.cfg.HealthAddr = a
})
}