-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
116 lines (95 loc) · 2.47 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
105
106
107
108
109
110
111
112
113
114
115
116
package gofr
import (
"context"
"net/url"
"os"
"time"
"github.com/neo532/gofr/logger"
"github.com/neo532/gofr/transport"
)
type Option func(o *options)
type options struct {
id string
name string
version string
metadata map[string]string
endpoints []string
ctx context.Context
sigs []os.Signal
logger logger.Logger
stopTimeout time.Duration
servers []transport.Server
beforeStart []func(context.Context) error
beforeStop []func(context.Context) error
afterStart []func(context.Context) error
afterStop []func(context.Context) error
}
// ID with service id.
func ID(id string) Option {
return func(o *options) { o.id = id }
}
// Name with service name.
func Name(name string) Option {
return func(o *options) { o.name = name }
}
// Version with service version.
func Version(version string) Option {
return func(o *options) { o.version = version }
}
// Metadata with service metadata.
func Metadata(md map[string]string) Option {
return func(o *options) { o.metadata = md }
}
// Endpoint with service endpoint.
func Endpoint(endpoints ...*url.URL) Option {
return func(o *options) {
o.endpoints = make([]string, 0, len(endpoints))
for _, e := range endpoints {
o.endpoints = append(o.endpoints, e.String())
}
}
}
// Context with service context.
func Context(ctx context.Context) Option {
return func(o *options) { o.ctx = ctx }
}
// Logger with service logger.
func Logger(logger logger.Logger) Option {
return func(o *options) { o.logger = logger }
}
// Server with transport servers.
func Server(srv ...transport.Server) Option {
return func(o *options) { o.servers = srv }
}
// Signal with exit signals.
func Signal(sigs ...os.Signal) Option {
return func(o *options) { o.sigs = sigs }
}
// StopTimeout with app stop timeout.
func StopTimeout(t time.Duration) Option {
return func(o *options) { o.stopTimeout = t }
}
// BeforeStart run funcs before app starts
func BeforeStart(fns ...func(context.Context) error) Option {
return func(o *options) {
o.beforeStart = fns
}
}
// BeforeStop run funcs before app stops
func BeforeStop(fns ...func(context.Context) error) Option {
return func(o *options) {
o.beforeStop = fns
}
}
// AfterStart run funcs after app starts
func AfterStart(fns ...func(context.Context) error) Option {
return func(o *options) {
o.afterStart = fns
}
}
// AfterStop run funcs after app stops
func AfterStop(fns ...func(context.Context) error) Option {
return func(o *options) {
o.afterStop = fns
}
}