Add Config to service options (#1336)

Co-authored-by: Ben Toogood <ben@micro.mu>
Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
ben-toogood
2020-03-12 18:13:03 +00:00
committed by GitHub
parent 1b4e881d74
commit 47f1203e97
6 changed files with 60 additions and 24 deletions

View File

@@ -7,12 +7,13 @@ import (
"strings"
"time"
"github.com/micro/go-micro/v2/auth/provider"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/auth/provider"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/config"
configSrv "github.com/micro/go-micro/v2/config/source/service"
"github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/profile/http"
"github.com/micro/go-micro/v2/debug/profile/pprof"
@@ -305,6 +306,11 @@ var (
EnvVars: []string{"MICRO_AUTH_PROVIDER_SCOPE"},
Usage: "The scope to be used for oauth",
},
&cli.StringFlag{
Name: "config",
EnvVars: []string{"MICRO_CONFIG"},
Usage: "The source of the config to be used to get configuration",
},
}
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{
@@ -373,6 +379,10 @@ var (
"http": http.NewProfile,
"pprof": pprof.NewProfile,
}
DefaultConfigs = map[string]func(...config.Option) (config.Config, error){
"service": config.NewConfig,
}
)
func init() {
@@ -392,6 +402,7 @@ func newCmd(opts ...Option) Cmd {
Store: &store.DefaultStore,
Tracer: &trace.DefaultTracer,
Profile: &profile.DefaultProfile,
Config: &config.DefaultConfig,
Brokers: DefaultBrokers,
Clients: DefaultClients,
@@ -404,6 +415,7 @@ func newCmd(opts ...Option) Cmd {
Tracers: DefaultTracers,
Auths: DefaultAuths,
Profiles: DefaultProfiles,
Configs: DefaultConfigs,
}
for _, o := range opts {
@@ -700,6 +712,13 @@ func (c *cmd) Before(ctx *cli.Context) error {
}
}
if ctx.String("config") == "service" {
opt := config.WithSource(configSrv.NewSource())
if err := (*c.opts.Config).Init(opt); err != nil {
logger.Fatalf("Error configuring config: %v", err)
}
}
// client opts
if r := ctx.Int("client_retries"); r >= 0 {
clientOpts = append(clientOpts, client.Retries(r))

View File

@@ -7,6 +7,7 @@ import (
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/config"
"github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/trace"
"github.com/micro/go-micro/v2/registry"
@@ -34,6 +35,7 @@ type Options struct {
Tracer *trace.Tracer
Auth *auth.Auth
Profile *profile.Profile
Config *config.Config
Brokers map[string]func(...broker.Option) broker.Broker
Clients map[string]func(...client.Option) client.Client
@@ -46,6 +48,7 @@ type Options struct {
Tracers map[string]func(...trace.Option) trace.Tracer
Auths map[string]func(...auth.Option) auth.Auth
Profiles map[string]func(...profile.Option) profile.Profile
Configs map[string]func(...config.Option) (config.Config, error)
// Other options for implementations of the interface
// can be stored in a context