-
Notifications
You must be signed in to change notification settings - Fork 41
/
serveroptions.go
78 lines (71 loc) · 2.43 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
package signalr
import (
"errors"
"fmt"
"reflect"
)
// UseHub sets the hub instance used by the server
func UseHub(hub HubInterface) func(Party) error {
return func(p Party) error {
if s, ok := p.(*server); ok {
s.newHub = func() HubInterface { return hub }
return nil
}
return errors.New("option UseHub is server only")
}
}
// HubFactory sets the function which returns the hub instance for every hub method invocation
// The function might create a new hub instance on every invocation.
// If hub instances should be created and initialized by a DI framework,
// the frameworks' factory method can be called here.
func HubFactory(factory func() HubInterface) func(Party) error {
return func(p Party) error {
if s, ok := p.(*server); ok {
s.newHub = factory
return nil
}
return errors.New("option HubFactory is server only")
}
}
// SimpleHubFactory sets a HubFactory which creates a new hub with the underlying type
// of hubProto on each hub method invocation.
func SimpleHubFactory(hubProto HubInterface) func(Party) error {
return HubFactory(
func() HubInterface {
return reflect.New(reflect.ValueOf(hubProto).Elem().Type()).Interface().(HubInterface)
})
}
// HTTPTransports sets the list of available transports for http connections. Allowed transports are
// "WebSockets", "ServerSentEvents". Default is both transports are available.
func HTTPTransports(transports ...TransportType) func(Party) error {
return func(p Party) error {
if s, ok := p.(*server); ok {
for _, transport := range transports {
switch transport {
case TransportWebSockets, TransportServerSentEvents:
s.transports = append(s.transports, transport)
default:
return fmt.Errorf("unsupported transport: %v", transport)
}
}
return nil
}
return errors.New("option Transports is server only")
}
}
// InsecureSkipVerify disables Accepts origin verification behaviour which is used to avoid same origin strategy.
// See https://pkg.go.dev/nhooyr.io/websocket#AcceptOptions
func InsecureSkipVerify(skip bool) func(Party) error {
return func(p Party) error {
p.setInsecureSkipVerify(skip)
return nil
}
}
// AllowOriginPatterns lists the host patterns for authorized origins which is used for avoid same origin strategy.
// See https://pkg.go.dev/nhooyr.io/websocket#AcceptOptions
func AllowOriginPatterns(origins []string) func(Party) error {
return func(p Party) error {
p.setOriginPatterns(origins)
return nil
}
}