-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
104 lines (84 loc) · 2.22 KB
/
option.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package kitx
import (
"context"
"fmt"
"net/url"
"os"
"time"
"github.com/sado0823/go-kitx/kit/log"
"github.com/sado0823/go-kitx/kit/registry"
"github.com/sado0823/go-kitx/transport"
"github.com/google/uuid"
)
type (
option struct {
id string
name string
version string
metadata map[string]string
endpoints []*url.URL
ctx context.Context
signals []os.Signal
logger log.Logger
registrar registry.Registrar
registrarTimeout time.Duration
stopTimeout time.Duration
servers []transport.Server
}
)
func (opt *option) Fix() {
if opt.id == "" {
if newUUID, err := uuid.NewUUID(); err == nil {
opt.id = newUUID.String()
} else {
opt.id = fmt.Sprintf("kitx.app.id.%d", time.Now().UnixNano())
}
}
if opt.name == "" {
opt.name = "kitx.app.name.unknown"
}
if opt.logger != nil {
log.SetGlobal(opt.logger)
}
}
// WithID id should be unique
func WithID(id string) Option {
return func(o *option) { o.id = id }
}
func WithName(name string) Option {
return func(o *option) { o.name = name }
}
func WithVersion(version string) Option {
return func(o *option) { o.version = version }
}
func WithMetadata(md map[string]string) Option {
return func(o *option) { o.metadata = md }
}
func WithEndpoint(endpoints ...*url.URL) Option {
return func(o *option) { o.endpoints = endpoints }
}
func WithContext(ctx context.Context) Option {
return func(o *option) { o.ctx = ctx }
}
func WithLogger(logger log.Logger) Option {
return func(o *option) { o.logger = logger }
}
func WithServer(srv ...transport.Server) Option {
return func(o *option) { o.servers = srv }
}
// WithSignal with exit signals.
func WithSignal(sigs ...os.Signal) Option {
return func(o *option) { o.signals = sigs }
}
// WithRegistrar with service registry.
func WithRegistrar(r registry.Registrar) Option {
return func(o *option) { o.registrar = r }
}
// WithRegistrarTimeout with registrar timeout.
func WithRegistrarTimeout(t time.Duration) Option {
return func(o *option) { o.registrarTimeout = t }
}
// WithStopTimeout with app stop timeout.
func WithStopTimeout(t time.Duration) Option {
return func(o *option) { o.stopTimeout = t }
}