-
Notifications
You must be signed in to change notification settings - Fork 1
/
serveroptions.go
96 lines (82 loc) · 1.71 KB
/
serveroptions.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
package socketify
import (
"github.com/teris-io/shortid"
"net/http"
)
const (
defaultAddress = ":8080"
defaultEndpoint = "/ws"
)
type options struct {
serveMux *http.ServeMux
address string
endpoint string
checkOrigin func(r *http.Request) bool
logger Logger
enableStorage bool
idFunc func(r *http.Request) string
}
func defaultOptions() *options {
return &options{
serveMux: http.NewServeMux(),
address: defaultAddress,
endpoint: defaultEndpoint,
logger: logger{},
}
}
func ServerOptions() *options {
return &options{}
}
func (o *options) SetIdSetterFunction(fn func(r *http.Request) string) *options {
o.idFunc = fn
return o
}
func (o *options) SetEndpoint(endpoint string) *options {
o.endpoint = endpoint
return o
}
func (o *options) SetServeMux(mux *http.ServeMux) *options {
o.serveMux = mux
return o
}
func (o *options) SetAddress(address string) *options {
o.address = address
return o
}
func (o *options) SetCheckOrigin(checkOriginFn func(r *http.Request) bool) *options {
o.checkOrigin = checkOriginFn
return o
}
func (o *options) SetLogger(l Logger) *options {
o.logger = l
return o
}
func (o *options) IgnoreCheckOrigin() *options {
o.checkOrigin = func(r *http.Request) bool {
return true
}
return o
}
func (o *options) EnableStorage() *options {
o.enableStorage = true
return o
}
func (o *options) fillDefaults() {
if o.address == "" {
o.address = defaultAddress
}
if o.endpoint == "" {
o.endpoint = defaultEndpoint
}
if o.serveMux == nil {
o.serveMux = http.NewServeMux()
}
if o.logger == nil {
o.logger = logger{}
}
if o.idFunc == nil {
o.idFunc = func(_ *http.Request) string {
return shortid.MustGenerate()
}
}
}