-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparam.go
More file actions
77 lines (62 loc) · 2.72 KB
/
param.go
File metadata and controls
77 lines (62 loc) · 2.72 KB
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
package httpsrv
import (
"net"
"net/http"
"time"
)
type (
// ServerParam is the parameter type for [Run] function.
ServerParam interface {
apply(cfg *serverConf)
}
serverParam struct{ set func(*serverConf) }
)
func (p serverParam) apply(cfg *serverConf) { p.set(cfg) }
/*
Listener allows to set the net listener the http server binds to.
Usually it is easier to just set the Addr field of the srv parameter when calling [Run].
This parameter is mostly useful for testing where server is bind to a random port which
the test need to know.
If both server.Addr is set and Listener is used then the listener set by Listener is used.
*/
func Listener(l net.Listener) ServerParam {
return serverParam{func(cfg *serverConf) { cfg.l = l }}
}
/*
Endpoints sets the HTTP request multiplexer for the server (Server.Handler field).
Instead of using this parameter the Handler could be assigned in the Server parameter of the [Run]
function but sometimes it is more convenient to set them separately - ie the [http.Server] is returned
by configuration provider while http.Handler is implemented by a service struct.
Endpoints overrides Handler set by the srv parameter of [Run]!
*/
func Endpoints(h http.Handler) ServerParam {
return serverParam{func(cfg *serverConf) { cfg.srv.Handler = h }}
}
/*
ShutdownTimeout sets timeout for graceful shutdown (ie context timeout for the [http.Server.Shutdown] call).
When not provided or duration is smaller than or equal to zero no graceful shutdown is attempted,
all connections are closed immediately (ie [http.Server.Close] is used to stop the server).
Keep in mind that in "managed environment" (ie Kubernetes) the instance (pod) could still be killed
by the orchestrator before this timeout is reached.
*/
func ShutdownTimeout(to time.Duration) ServerParam {
return serverParam{func(cfg *serverConf) { cfg.shutdownTO = to }}
}
/*
ShutdownOnPanic instructs the http server to shut down when unhandled panic (except [http.ErrAbortHandler])
escapes some handler. The http server's Close method will be used to shut down the server immediately, ie
the [ShutdownTimeout] parameter is ignored.
By default http.Server just logs the panic and carries on but some argue that in case of
unhandled panic service should always die and new instance started - this option provides
easy way to implement that behavior.
*/
func ShutdownOnPanic() ServerParam {
return serverParam{func(cfg *serverConf) { cfg.dieOnPanic = true }}
}
/*
TLS allows to start the server using [http.Server.ServeTLS].
Alternatively the server's [http.Server.TLSConfig] field can be assigned when passing it to [Run].
*/
func TLS(certFile, keyFile string) ServerParam {
return serverParam{func(cfg *serverConf) { cfg.certFile, cfg.keyFile = certFile, keyFile }}
}