forked from lukasjarosch/genki
/
options.go
97 lines (81 loc) · 2.17 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
86
87
88
89
90
91
92
93
94
95
96
97
package http
import (
"time"
"github.com/spf13/pflag"
)
const DefaultPort = "8080"
const DefaultGracePeriod = 3 * time.Second
const DefaultName = "default"
const DefaultLoggingMiddlewareEnabled = true
const DefaultPrometheusMiddlewareEnabled = true
const DefaultHealthEndpoint = "/health"
type Options struct {
Port string
Name string
HealthEndpoint string
ShutdownGracePeriod time.Duration
LoggingMiddlewareEnabled bool
PrometheusMiddlewareEnabled bool
LoggingSkipEndpoints []string
}
func Port(addr string) Option {
return func(opts *Options) {
opts.Port = addr
}
}
func ShutdownGracePeriod(duration time.Duration) Option {
return func(opts *Options) {
opts.ShutdownGracePeriod = duration
}
}
func Name(name string) Option {
return func(opts *Options) {
opts.Name = name
}
}
func DisableLoggingMiddleware() Option {
return func(opts *Options) {
opts.LoggingMiddlewareEnabled = false
}
}
func LoggingSkipEndpoints(skip ...string) Option {
return func(opts *Options) {
opts.LoggingSkipEndpoints = append(opts.LoggingSkipEndpoints, skip...)
}
}
func HealthEndpoint(endpoint string) Option {
return func(opts *Options) {
opts.HealthEndpoint = endpoint
}
}
func newOptions(opts ...Option) Options {
opt := Options{
Port: DefaultPort,
ShutdownGracePeriod: DefaultGracePeriod,
Name: DefaultName,
LoggingMiddlewareEnabled: DefaultLoggingMiddlewareEnabled,
PrometheusMiddlewareEnabled: DefaultPrometheusMiddlewareEnabled,
HealthEndpoint: DefaultHealthEndpoint,
LoggingSkipEndpoints: []string{},
}
for _, o := range opts {
o(&opt)
}
return opt
}
// Flags is a convenience function to quickly add the HTTP server options as CLI flags
// Implements the cli.FlagProvider type
func Flags() *pflag.FlagSet {
fs := pflag.NewFlagSet("http-server", pflag.ContinueOnError)
fs.String(
PortConfigKey,
DefaultPort,
"the port on which the HTTP server is listening on",
)
fs.Duration(
GracePeriodConfigKey,
DefaultGracePeriod,
"grace period after which the server shutdown is terminated",
)
return fs
}