added options to grpc init

This commit is contained in:
A.Unger
2020-02-04 12:37:23 +01:00
parent ed8b266bf8
commit 582170605f
3 changed files with 91 additions and 12 deletions

View File

@@ -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()

View File

@@ -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()

74
pkg/micro/grpc/options.go Normal file
View File

@@ -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
}
}