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

     1  package router
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/anycable/anycable-go/common"
     8  	"github.com/anycable/anycable-go/node"
     9  )
    10  
    11  type RouterController struct {
    12  	controller node.Controller
    13  	routes     map[string]node.Controller
    14  }
    15  
    16  var _ node.Controller = (*RouterController)(nil)
    17  
    18  func NewRouterController(c node.Controller) *RouterController {
    19  	return &RouterController{c, make(map[string]node.Controller)}
    20  }
    21  
    22  func (c *RouterController) SetDefault(controller node.Controller) {
    23  	c.controller = controller
    24  }
    25  
    26  func (c *RouterController) Route(channel string, handler node.Controller) error {
    27  	if _, ok := c.routes[channel]; ok {
    28  		return fmt.Errorf("Route has been already defined: %s", channel)
    29  	}
    30  
    31  	c.routes[channel] = handler
    32  
    33  	return nil
    34  }
    35  
    36  func (c *RouterController) Empty() bool {
    37  	return len(c.routes) == 0
    38  }
    39  
    40  func (c *RouterController) Routes() []string {
    41  	keys := []string{}
    42  	for k := range c.routes {
    43  		keys = append(keys, k)
    44  	}
    45  
    46  	return keys
    47  }
    48  
    49  func (c *RouterController) Start() error {
    50  	return c.controller.Start()
    51  }
    52  
    53  func (c *RouterController) Shutdown() error {
    54  	return c.controller.Shutdown()
    55  }
    56  
    57  func (c *RouterController) Authenticate(sid string, env *common.SessionEnv) (*common.ConnectResult, error) {
    58  	return c.controller.Authenticate(sid, env)
    59  }
    60  
    61  func (c *RouterController) Subscribe(sid string, env *common.SessionEnv, id string, channel string) (*common.CommandResult, error) {
    62  	channelName := extractChannel(channel)
    63  
    64  	if channelName != "" {
    65  		if handler, ok := c.routes[channelName]; ok {
    66  			res, err := handler.Subscribe(sid, env, id, channel)
    67  
    68  			if res != nil || err != nil {
    69  				return res, err
    70  			}
    71  		}
    72  	}
    73  
    74  	return c.controller.Subscribe(sid, env, id, channel)
    75  }
    76  
    77  func (c *RouterController) Unsubscribe(sid string, env *common.SessionEnv, id string, channel string) (*common.CommandResult, error) {
    78  	channelName := extractChannel(channel)
    79  
    80  	if channelName != "" {
    81  		if handler, ok := c.routes[channelName]; ok {
    82  			res, err := handler.Unsubscribe(sid, env, id, channel)
    83  
    84  			if res != nil || err != nil {
    85  				return res, err
    86  			}
    87  		}
    88  	}
    89  
    90  	return c.controller.Unsubscribe(sid, env, id, channel)
    91  }
    92  
    93  func (c *RouterController) Perform(sid string, env *common.SessionEnv, id string, channel string, data string) (*common.CommandResult, error) {
    94  	channelName := extractChannel(channel)
    95  
    96  	if channelName != "" {
    97  		if handler, ok := c.routes[channelName]; ok {
    98  			res, err := handler.Perform(sid, env, id, channel, data)
    99  
   100  			if res != nil || err != nil {
   101  				return res, err
   102  			}
   103  		}
   104  	}
   105  
   106  	return c.controller.Perform(sid, env, id, channel, data)
   107  }
   108  func (c *RouterController) Disconnect(sid string, env *common.SessionEnv, id string, subscriptions []string) error {
   109  	return c.controller.Disconnect(sid, env, id, subscriptions)
   110  }
   111  
   112  func extractChannel(identifier string) string {
   113  	params := struct {
   114  		Channel string `json:"channel"`
   115  	}{}
   116  
   117  	err := json.Unmarshal([]byte(identifier), &params)
   118  
   119  	if err != nil {
   120  		return ""
   121  	}
   122  
   123  	return params.Channel
   124  }