github.com/philippseith/signalr@v0.6.3/serveroptions.go (about)

     1  package signalr
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"reflect"
     7  )
     8  
     9  // UseHub sets the hub instance used by the server
    10  func UseHub(hub HubInterface) func(Party) error {
    11  	return func(p Party) error {
    12  		if s, ok := p.(*server); ok {
    13  			s.newHub = func() HubInterface { return hub }
    14  			return nil
    15  		}
    16  		return errors.New("option UseHub is server only")
    17  	}
    18  }
    19  
    20  // HubFactory sets the function which returns the hub instance for every hub method invocation
    21  // The function might create a new hub instance on every invocation.
    22  // If hub instances should be created and initialized by a DI framework,
    23  // the frameworks' factory method can be called here.
    24  func HubFactory(factory func() HubInterface) func(Party) error {
    25  	return func(p Party) error {
    26  		if s, ok := p.(*server); ok {
    27  			s.newHub = factory
    28  			return nil
    29  		}
    30  		return errors.New("option HubFactory is server only")
    31  	}
    32  }
    33  
    34  // SimpleHubFactory sets a HubFactory which creates a new hub with the underlying type
    35  // of hubProto on each hub method invocation.
    36  func SimpleHubFactory(hubProto HubInterface) func(Party) error {
    37  	return HubFactory(
    38  		func() HubInterface {
    39  			return reflect.New(reflect.ValueOf(hubProto).Elem().Type()).Interface().(HubInterface)
    40  		})
    41  }
    42  
    43  // HTTPTransports sets the list of available transports for http connections. Allowed transports are
    44  // "WebSockets", "ServerSentEvents". Default is both transports are available.
    45  func HTTPTransports(transports ...TransportType) func(Party) error {
    46  	return func(p Party) error {
    47  		if s, ok := p.(*server); ok {
    48  			for _, transport := range transports {
    49  				switch transport {
    50  				case TransportWebSockets, TransportServerSentEvents:
    51  					s.transports = append(s.transports, transport)
    52  				default:
    53  					return fmt.Errorf("unsupported transport: %v", transport)
    54  				}
    55  			}
    56  			return nil
    57  		}
    58  		return errors.New("option Transports is server only")
    59  	}
    60  }
    61  
    62  // InsecureSkipVerify disables Accepts origin verification behaviour which is used to avoid same origin strategy.
    63  // See https://pkg.go.dev/nhooyr.io/websocket#AcceptOptions
    64  func InsecureSkipVerify(skip bool) func(Party) error {
    65  	return func(p Party) error {
    66  		p.setInsecureSkipVerify(skip)
    67  		return nil
    68  	}
    69  }
    70  
    71  // AllowOriginPatterns lists the host patterns for authorized origins which is used for avoid same origin strategy.
    72  // See https://pkg.go.dev/nhooyr.io/websocket#AcceptOptions
    73  func AllowOriginPatterns(origins []string) func(Party) error {
    74  	return func(p Party) error {
    75  		p.setOriginPatterns(origins)
    76  		return nil
    77  	}
    78  }