Skip to content

Commit

Permalink
Use port instead of address for listening on options
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Nov 29, 2020
1 parent d579222 commit 66c515f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
23 changes: 12 additions & 11 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (

// Options holds the configuration for a service instance
type Options struct {
Name string
ListenAddr string
Logger logger.Logger
Codec codec.Codec
Transport transport.Transport
Router router.Router
Broker broker.Broker
Config config.Config
Name string
RPCPort int16

Logger logger.Logger
Codec codec.Codec
Transport transport.Transport
Router router.Router
Broker broker.Broker
Config config.Config
}

// Option represents a function that can be used to mutate an Options object
Expand All @@ -31,10 +32,10 @@ func Name(name string) Option {
}
}

// ListenAddr sets the address in which the gRPC server will listen on
func ListenAddr(addr string) Option {
// RPCPort sets the port in which this service's RPC will listen on, as well as the port in which other services' RPC servers are listening on
func RPCPort(port int16) Option {
return func(o *Options) {
o.ListenAddr = addr
o.RPCPort = port
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewServer(opts *options.Options) Server {
func (s *server) Start() error {
ctx := context.Background()

l, err := s.opts.Transport.Listen(ctx, s.opts.ListenAddr)
l, err := s.opts.Transport.Listen(ctx, fmt.Sprintf(":%d", s.opts.RPCPort))
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type service struct {
func NewService(opts ...options.Option) Service {
svc := &service{}
svc.options.Logger = logger.NewStdoutLogger()
svc.options.RPCPort = 7070

svc.Apply(opts...)

Expand Down Expand Up @@ -73,9 +74,6 @@ func (s *service) Start() error {
if s.options.Name == "" {
return errors.New("missing service name")
}
if s.options.ListenAddr == "" {
return errors.New("missing listen address")
}

if s.options.Broker != nil {
if err := s.options.Broker.Connect(context.Background()); err != nil {
Expand Down

0 comments on commit 66c515f

Please sign in to comment.