-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
207 lines (193 loc) · 6.85 KB
/
config.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package httpserver
import (
"context"
"crypto/tls"
"database/sql"
"net"
"net/http"
"net/url"
"strings"
"syscall"
"time"
)
type configuration struct {
Context context.Context
ContextShutdown context.CancelFunc
Handler http.Handler
MaxRequestHeaderSize int
ReadRequestTimeout time.Duration
ReadRequestHeaderTimeout time.Duration
WriteResponseTimeout time.Duration
IdleConnectionTimeout time.Duration
ShutdownTimeout time.Duration
ForceShutdownTimeout time.Duration
ListenNetwork string
ListenAddress string
ListenConfig listenConfig
ListenAdapter func(net.Listener) net.Listener
ListenReady func(bool)
TLSConfig *tls.Config
HandlePanic bool
DumpRequestOnPanic bool
IgnoredErrors []error
Monitor monitor
Logger logger
ErrorLogger logger
HTTPServer httpServer
}
func New(options ...option) ListenCloser {
var config configuration
Options.apply(options...)(&config)
return newServer(config)
}
var Options singleton
type singleton struct{}
type option func(*configuration)
func (singleton) Context(value context.Context) option {
return func(this *configuration) { this.Context = value }
}
func (singleton) ListenAddress(value string) option {
return func(this *configuration) { this.ListenNetwork, this.ListenAddress = parseListenAddress(value) }
}
func (singleton) TLSConfig(value *tls.Config) option {
return func(this *configuration) { this.TLSConfig = value }
}
func (singleton) Handler(value http.Handler) option {
return func(this *configuration) { this.Handler = value }
}
func (singleton) HandlePanic(value bool) option {
return func(this *configuration) { this.HandlePanic = value }
}
func (singleton) DumpRequestOnPanic(value bool) option {
return func(this *configuration) { this.DumpRequestOnPanic = value }
}
func (singleton) IgnoredErrors(value ...error) option {
return func(this *configuration) { this.IgnoredErrors = value }
}
func (singleton) HTTPServer(value httpServer) option {
return func(this *configuration) { this.HTTPServer = value }
}
func (singleton) MaxRequestHeaderSize(value int) option {
return func(this *configuration) { this.MaxRequestHeaderSize = value }
}
func (singleton) ReadRequestTimeout(value time.Duration) option {
return func(this *configuration) { this.ReadRequestTimeout = value }
}
func (singleton) ReadRequestHeaderTimeout(value time.Duration) option {
return func(this *configuration) { this.ReadRequestHeaderTimeout = value }
}
func (singleton) WriteResponseTimeout(value time.Duration) option {
return func(this *configuration) { this.WriteResponseTimeout = value }
}
func (singleton) IdleConnectionTimeout(value time.Duration) option {
return func(this *configuration) { this.IdleConnectionTimeout = value }
}
func (singleton) ShutdownTimeout(value time.Duration) option {
return func(this *configuration) { this.ShutdownTimeout = value }
}
func (singleton) ForceShutdownTimeout(value time.Duration) option {
return func(this *configuration) { this.ForceShutdownTimeout = value }
}
func (singleton) ListenConfig(value listenConfig) option {
return func(this *configuration) { this.ListenConfig = value }
}
func (singleton) ListenAdapter(value func(net.Listener) net.Listener) option {
return func(this *configuration) { this.ListenAdapter = value }
}
func (singleton) ListenReady(value func(bool)) option {
return func(this *configuration) { this.ListenReady = value }
}
func (singleton) Monitor(value monitor) option {
return func(this *configuration) { this.Monitor = value }
}
func (singleton) Logger(value logger) option {
return func(this *configuration) { this.Logger = value }
}
func (singleton) ErrorLogger(value logger) option {
return func(this *configuration) { this.ErrorLogger = value }
}
func (singleton) apply(options ...option) option {
return func(this *configuration) {
for _, item := range Options.defaults(options...) {
item(this)
}
if this.HandlePanic {
this.Handler = newRecoveryHandler(this.Handler, this.IgnoredErrors, this.DumpRequestOnPanic, this.Monitor, this.Logger)
}
this.Context, this.ContextShutdown = context.WithCancel(this.Context)
if this.HTTPServer == nil {
this.HTTPServer = &http.Server{
Addr: this.ListenAddress,
Handler: this.Handler,
MaxHeaderBytes: this.MaxRequestHeaderSize,
ReadTimeout: this.ReadRequestTimeout,
ReadHeaderTimeout: this.ReadRequestHeaderTimeout,
WriteTimeout: this.WriteResponseTimeout,
IdleTimeout: this.IdleConnectionTimeout,
BaseContext: func(net.Listener) context.Context { return this.Context },
ErrorLog: newServerLogger(this.ErrorLogger),
}
}
}
}
func (singleton) defaults(options ...option) []option {
var defaultListenConfig = &net.ListenConfig{Control: func(_, _ string, conn syscall.RawConn) error {
return conn.Control(func(descriptor uintptr) {
_ = syscall.SetsockoptInt(int(descriptor), syscall.SOL_SOCKET, socketReusePort, 1)
})
}}
defaultNop := &nop{}
return append([]option{
Options.ListenAddress(":http"),
Options.TLSConfig(nil),
Options.MaxRequestHeaderSize(1024 * 2),
Options.ReadRequestTimeout(time.Second * 5),
Options.ReadRequestHeaderTimeout(time.Second),
Options.WriteResponseTimeout(time.Second * 90),
Options.IdleConnectionTimeout(time.Second * 30),
Options.ShutdownTimeout(time.Second * 5),
Options.ForceShutdownTimeout(time.Second),
Options.HandlePanic(true),
Options.DumpRequestOnPanic(false),
Options.IgnoredErrors(context.Canceled, context.DeadlineExceeded, sql.ErrTxDone),
Options.Context(context.Background()),
Options.Handler(defaultNop),
Options.Monitor(defaultNop),
Options.Logger(defaultNop),
Options.ErrorLogger(defaultNop),
Options.ListenConfig(defaultListenConfig),
Options.ListenAdapter(nil),
Options.ListenReady(nil),
}, options...)
}
func parseListenAddress(value string) (network, address string) {
if parsed := parseURL(value); parsed == nil {
return "tcp", value
} else if strings.ToLower(parsed.Scheme) == "unix" {
return "unix", value[len("unix://"):] // don't prepend slash which assumes full path because path might be relative
} else {
return coalesce(parsed.Scheme, "tcp"), coalesce(parsed.Host, parsed.Path)
}
}
func parseURL(value string) *url.URL {
value = strings.TrimSpace(value)
if len(value) == 0 {
return nil
} else if parsed, err := url.Parse(value); err != nil {
return nil
} else {
return parsed
}
}
func coalesce(values ...string) string {
for _, item := range values {
if len(item) > 0 {
return item
}
}
return ""
}
type nop struct{}
func (*nop) Printf(_ string, _ ...any) {}
func (*nop) ServeHTTP(_ http.ResponseWriter, _ *http.Request) {}
func (*nop) PanicRecovered(*http.Request, any) {}