-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
59 lines (49 loc) · 995 Bytes
/
server.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
package server
import (
"context"
"fmt"
)
type Option func(c *ServiceInfo)
// ServiceInfo represents service info
type ServiceInfo struct {
Name string `json:"name"`
Scheme string `json:"scheme"`
Address string `json:"address"`
}
// Label ...
func (si ServiceInfo) Label() string {
return fmt.Sprintf("%s://%s", si.Scheme, si.Address)
}
// Server ...
type Server interface {
Serve() error
Stop() error
GracefulStop(ctx context.Context) error
Info() *ServiceInfo
}
func ApplyOptions(options ...Option) ServiceInfo {
info := defaultServiceInfo()
for _, option := range options {
option(&info)
}
return info
}
func WithScheme(scheme string) Option {
return func(c *ServiceInfo) {
c.Scheme = scheme
}
}
func WithAddress(address string) Option {
return func(c *ServiceInfo) {
c.Address = address
}
}
func WithName(name string) Option {
return func(c *ServiceInfo) {
c.Name = name
}
}
func defaultServiceInfo() ServiceInfo {
si := ServiceInfo{}
return si
}