github.com/anycable/anycable-go@v1.5.1/node/controller.go (about)

     1  package node
     2  
     3  import (
     4  	"errors"
     5  	"log/slog"
     6  
     7  	"github.com/anycable/anycable-go/common"
     8  )
     9  
    10  // Controller is an interface describing business-logic handler (e.g. RPC)
    11  type Controller interface {
    12  	Start() error
    13  	Shutdown() error
    14  	Authenticate(sid string, env *common.SessionEnv) (*common.ConnectResult, error)
    15  	Subscribe(sid string, env *common.SessionEnv, ids string, channel string) (*common.CommandResult, error)
    16  	Unsubscribe(sid string, env *common.SessionEnv, ids string, channel string) (*common.CommandResult, error)
    17  	Perform(sid string, env *common.SessionEnv, ids string, channel string, data string) (*common.CommandResult, error)
    18  	Disconnect(sid string, env *common.SessionEnv, ids string, subscriptions []string) error
    19  }
    20  
    21  type NullController struct {
    22  	log *slog.Logger
    23  }
    24  
    25  func NewNullController(l *slog.Logger) *NullController {
    26  	return &NullController{l.With("context", "rpc", "impl", "null")}
    27  }
    28  
    29  func (c *NullController) Start() (err error) {
    30  	c.log.Info("no RPC configured")
    31  
    32  	return
    33  }
    34  
    35  func (c *NullController) Shutdown() (err error) { return }
    36  
    37  func (c *NullController) Authenticate(sid string, env *common.SessionEnv) (*common.ConnectResult, error) {
    38  	c.log.Debug("reject connection")
    39  
    40  	return &common.ConnectResult{
    41  		Status:             common.FAILURE,
    42  		Transmissions:      []string{common.DisconnectionMessage(common.UNAUTHORIZED_REASON, false)},
    43  		DisconnectInterest: -1,
    44  	}, nil
    45  }
    46  
    47  func (c *NullController) Subscribe(sid string, env *common.SessionEnv, ids string, channel string) (*common.CommandResult, error) {
    48  	c.log.Debug("reject subscription", "channel", channel)
    49  
    50  	return &common.CommandResult{
    51  		Status:             common.FAILURE,
    52  		Transmissions:      []string{common.RejectionMessage(channel)},
    53  		DisconnectInterest: -1,
    54  	}, nil
    55  }
    56  
    57  func (c *NullController) Perform(sid string, env *common.SessionEnv, ids string, channel string, data string) (*common.CommandResult, error) {
    58  	return nil, errors.New("not implemented")
    59  }
    60  
    61  func (c *NullController) Unsubscribe(sid string, env *common.SessionEnv, ids string, channel string) (*common.CommandResult, error) {
    62  	return nil, errors.New("not implemented")
    63  }
    64  
    65  func (c *NullController) Disconnect(sid string, env *common.SessionEnv, ids string, subscriptions []string) error {
    66  	return nil
    67  }