/
options.go
133 lines (113 loc) · 3.71 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
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
package connector
import (
"github.com/gorilla/websocket"
"net/http"
"time"
)
type (
// Option is a function to apply various configurations to customize a connector Component.
Option func(o *Options)
// Options hold the configurable parts of a connector Component.
Options struct {
// WriteTimeout is the maximum time of write message operation.
// Slow client will be disconnected.
// Default is 1 second if not set via WithWriteTimeout.
WriteTimeout time.Duration
// MaxMessageSize is the maximum allowed message size in bytes received from the client.
// Default is 4096 bytes (4KB) if not set via WithMaxMessageSize.
MaxMessageSize int64
// Addr optionally specifies the TCP address for the server to listen on,
// in the form "host:port". If empty, ":http" (port 80) is used.
// See net.Dial for details of the address format.
Addr string
// WebsocketPath is the URL path to accept WebSocket connections.
// This option only applies to WebsocketConnector.
// Default is "/" if not set via WithWebsocketPath.
WebsocketPath string
// TLSCertFile is the path to TLS cert file.
// This option only applies to WebsocketConnector.
TLSCertFile string
// TLSKeyFile is the path to TLS key file.
// This option only applies to WebsocketConnector.
TLSKeyFile string
ServeMux *http.ServeMux
Server *http.Server
Upgrader *websocket.Upgrader
}
)
func defaultOptions() *Options {
return &Options{
WebsocketPath: "/",
WriteTimeout: 1 * time.Second,
MaxMessageSize: 4096,
ServeMux: http.DefaultServeMux,
Server: &http.Server{},
Upgrader: &websocket.Upgrader{},
}
}
// WithAddr is an Option to set the TCP address for the server to listen on.
func WithAddr(a string) Option {
return func(o *Options) {
o.Addr = a
if o.Server != nil {
o.Server.Addr = o.Addr
}
}
}
// WithWebsocketPath is an Option to set the URL path for accepting WebSocket connections.
func WithWebsocketPath(p string) Option {
return func(o *Options) {
o.WebsocketPath = p
}
}
// WithWriteTimeout is an Option to set the maximum time of one write message operation to complete.
func WithWriteTimeout(d time.Duration) Option {
return func(o *Options) {
o.WriteTimeout = d
}
}
// WithMaxMessageSize is an Option to set maximum message size in bytes allowed from client.
func WithMaxMessageSize(s int64) Option {
return func(o *Options) {
o.MaxMessageSize = s
}
}
// WithTLSCertAndKey is an Option to set the path to TLS certificate file with its matching private key.
// WebsocketConnector will start the http.Server with ListenAndServeTLS that expects HTTPS connections,
// when either certFile or keyFile is not an empty string.
func WithTLSCertAndKey(certFile, keyFile string) Option {
return func(o *Options) {
o.TLSCertFile = certFile
o.TLSKeyFile = keyFile
}
}
// WithHTTPServeMux is an Option to set a custom http.ServeMux,
// will also update Server.Handler to mux if Options.Server is not nil.
func WithHTTPServeMux(mux *http.ServeMux) Option {
return func(o *Options) {
o.ServeMux = mux
if o.Server != nil {
o.Server.Handler = mux
}
}
}
// WithHTTPServer is an Option to set a custom http.Server,
// will also force overwrite Server.Addr to Options.Addr if Options.Addr is not nil,
// will also force overwrite Server.Handler to Options.ServeMux if Options.ServeMux is not nil.
func WithHTTPServer(s *http.Server) Option {
return func(o *Options) {
o.Server = s
if o.Addr != "" {
o.Server.Addr = o.Addr
}
if o.ServeMux != nil {
o.Server.Handler = o.ServeMux
}
}
}
// WithWebsocketUpgrader is an Option to set a custom websocket.Upgrader.
func WithWebsocketUpgrader(upgrader *websocket.Upgrader) Option {
return func(o *Options) {
o.Upgrader = upgrader
}
}