github.com/glide-im/glide@v1.6.0/pkg/bootstrap/bootstrap.go (about)

     1  package bootstrap
     2  
     3  import (
     4  	"errors"
     5  	"github.com/glide-im/glide/pkg/gate"
     6  	"github.com/glide-im/glide/pkg/messages"
     7  	"github.com/glide-im/glide/pkg/messaging"
     8  	"github.com/glide-im/glide/pkg/subscription"
     9  )
    10  
    11  type Options struct {
    12  	Messaging    messaging.Interface
    13  	Gate         gate.DefaultGateway
    14  	Subscription subscription.Interface
    15  }
    16  
    17  func Bootstrap(opts *Options) error {
    18  
    19  	err := setupDependence(opts)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	_, ok := opts.Gate.(gate.Server)
    25  	if ok {
    26  		return bootGatewayServer(opts)
    27  	}
    28  	_, ok = opts.Messaging.(messaging.Server)
    29  	if ok {
    30  		return bootMessagingServer(opts)
    31  	}
    32  	_, ok = opts.Subscription.(subscription.Server)
    33  	if ok {
    34  		return bootSubscriptionServer(opts)
    35  	}
    36  
    37  	return errors.New("no server found")
    38  }
    39  
    40  func setupDependence(opts *Options) error {
    41  	m, ok := opts.Messaging.(messaging.Messaging)
    42  	if ok {
    43  		g, ok := opts.Gate.(gate.Gateway)
    44  		if ok {
    45  			m.SetGate(g)
    46  		} else {
    47  			return errors.New("gateway not found")
    48  		}
    49  		m.SetSubscription(opts.Subscription)
    50  	}
    51  
    52  	sb, ok := opts.Subscription.(subscription.Subscribe)
    53  	if ok {
    54  		sb.SetGateInterface(opts.Gate)
    55  	}
    56  	return nil
    57  }
    58  
    59  func bootSubscriptionServer(opts *Options) error {
    60  	server, ok := opts.Subscription.(subscription.Server)
    61  	if !ok {
    62  		return errors.New("subscription server not implemented")
    63  	}
    64  	server.SetGateInterface(opts.Gate)
    65  	return server.Run()
    66  }
    67  
    68  func bootMessagingServer(opts *Options) error {
    69  	server, ok := opts.Messaging.(messaging.Server)
    70  	if !ok {
    71  		return errors.New("messaging does not implement Messaging.impl")
    72  	}
    73  
    74  	manager, ok := opts.Gate.(gate.Gateway)
    75  	if ok {
    76  		server.SetGate(manager)
    77  	}
    78  	server.SetSubscription(opts.Subscription)
    79  	return server.Run()
    80  }
    81  
    82  func bootGatewayServer(opts *Options) error {
    83  
    84  	gateway, ok := opts.Gate.(gate.Server)
    85  	if !ok {
    86  		return errors.New("Gate is not a gateway server")
    87  	}
    88  
    89  	if opts.Messaging == nil {
    90  		return errors.New("can't boot a gateway server without a Messaging interface")
    91  	}
    92  
    93  	gateway.SetMessageHandler(func(cliInfo *gate.Info, message *messages.GlideMessage) {
    94  		err := opts.Messaging.Handle(cliInfo, message)
    95  		if err != nil {
    96  			// TODO: Log error
    97  		}
    98  	})
    99  
   100  	return gateway.Run()
   101  }