diff --git a/pkg/command/server.go b/pkg/command/server.go index d27b9d01d..2644b1bc0 100644 --- a/pkg/command/server.go +++ b/pkg/command/server.go @@ -43,7 +43,14 @@ func Server(cfg *config.Config) *cli.Command { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - service := grpc.NewService(ctx, cfg) + service := grpc.NewService( + // TODO read all this from cfg + grpc.Context(ctx), + grpc.Config(cfg), + grpc.Name("accounts"), + grpc.Namespace("com.owncloud"), + grpc.Address("localhost:9999"), + ) gr.Add(func() error { return service.Run() diff --git a/pkg/micro/grpc/grpc.go b/pkg/micro/grpc/grpc.go index be538834c..547789d47 100644 --- a/pkg/micro/grpc/grpc.go +++ b/pkg/micro/grpc/grpc.go @@ -1,25 +1,23 @@ package grpc import ( - "context" - - "github.com/owncloud/ocis-accounts/pkg/config" "github.com/owncloud/ocis-accounts/pkg/proto/v0" svc "github.com/owncloud/ocis-accounts/pkg/service/v0" "github.com/owncloud/ocis-pkg/service/grpc" ) -// NewService creates a grpc service -func NewService(c context.Context, cfg *config.Config) grpc.Service { +// NewService initializes a new go-micro service ready to run +func NewService(opts ...Option) grpc.Service { + options := newOptions(opts...) + service := grpc.NewService( - // TODO options come from configuration - grpc.Name("accounts"), - grpc.Namespace("com.owncloud"), - grpc.Address("localhost:9999"), - grpc.Context(c), + grpc.Name(options.Name), + grpc.Context(options.Context), + grpc.Address(options.Address), + grpc.Namespace(options.Namespace), ) - hdlr := svc.New(cfg) + hdlr := svc.New(options.Config) proto.RegisterSettingsServiceHandler(service.Server(), hdlr) service.Init() diff --git a/pkg/micro/grpc/options.go b/pkg/micro/grpc/options.go new file mode 100644 index 000000000..1e92d61ba --- /dev/null +++ b/pkg/micro/grpc/options.go @@ -0,0 +1,74 @@ +package grpc + +import ( + "context" + + "github.com/owncloud/ocis-accounts/pkg/config" + "github.com/owncloud/ocis-pkg/log" +) + +// Option defines a single option function. +type Option func(o *Options) + +// Options defines the available options for this package. +type Options struct { + Name string + Address string + Logger log.Logger + Context context.Context + Config *config.Config + Namespace string +} + +// newOptions initializes the available default options. +func newOptions(opts ...Option) Options { + opt := Options{} + + for _, o := range opts { + o(&opt) + } + + return opt +} + +// Logger provides a function to set the logger option. +func Logger(val log.Logger) Option { + return func(o *Options) { + o.Logger = val + } +} + +// Name provides a name for the service. +func Name(val string) Option { + return func(o *Options) { + o.Name = val + } +} + +// Address provides an address for the service. +func Address(val string) Option { + return func(o *Options) { + o.Address = val + } +} + +// Context provides a function to set the context option. +func Context(val context.Context) Option { + return func(o *Options) { + o.Context = val + } +} + +// Config provides a function to set the config option. +func Config(val *config.Config) Option { + return func(o *Options) { + o.Config = val + } +} + +// Namespace provides a function to set the namespace option. +func Namespace(val string) Option { + return func(o *Options) { + o.Namespace = val + } +}