Add runtime to service.Options()
This commit is contained in:
@@ -3,6 +3,8 @@ package runtime
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/micro/go-micro/v2/client"
|
||||
)
|
||||
|
||||
type Option func(o *Options)
|
||||
@@ -17,6 +19,8 @@ type Options struct {
|
||||
Source string
|
||||
// Base image to use
|
||||
Image string
|
||||
// Client to use when making requests
|
||||
Client client.Client
|
||||
}
|
||||
|
||||
// WithSource sets the base image / repository
|
||||
@@ -47,6 +51,13 @@ func WithImage(t string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WithClient sets the client to use
|
||||
func WithClient(c client.Client) Option {
|
||||
return func(o *Options) {
|
||||
o.Client = c
|
||||
}
|
||||
}
|
||||
|
||||
type CreateOption func(o *CreateOptions)
|
||||
|
||||
type ReadOption func(o *ReadOptions)
|
||||
|
@@ -24,6 +24,9 @@ func (s *svc) Init(opts ...runtime.Option) error {
|
||||
o(&s.options)
|
||||
}
|
||||
|
||||
// reset the runtime as the client could have changed
|
||||
s.runtime = pb.NewRuntimeService(runtime.DefaultName, s.options.Client)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -278,19 +281,17 @@ func (s *svc) String() string {
|
||||
|
||||
// NewRuntime creates new service runtime and returns it
|
||||
func NewRuntime(opts ...runtime.Option) runtime.Runtime {
|
||||
// get default options
|
||||
options := runtime.Options{}
|
||||
var options runtime.Options
|
||||
|
||||
// apply requested options
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
// create default client
|
||||
cli := client.DefaultClient
|
||||
if options.Client == nil {
|
||||
options.Client = client.DefaultClient
|
||||
}
|
||||
|
||||
return &svc{
|
||||
options: options,
|
||||
runtime: pb.NewRuntimeService(runtime.DefaultName, cli),
|
||||
runtime: pb.NewRuntimeService(runtime.DefaultName, options.Client),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user