From 9b74bc52d6a35f979ba37acd09ea948b6806ee18 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Sun, 19 Jul 2020 19:48:11 +0100 Subject: [PATCH] Move defaults to defaults.go --- cmd/cmd.go | 133 ++++++++-------------------------------------------- defaults.go | 119 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 133 insertions(+), 119 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index db2d010a..82515a4d 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -10,21 +10,20 @@ import ( "strings" "time" + "github.com/micro/cli/v2" "github.com/micro/go-micro/v2/auth" "github.com/micro/go-micro/v2/broker" + brokerSrv "github.com/micro/go-micro/v2/broker/service" "github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/client/grpc" "github.com/micro/go-micro/v2/config" - configSrc "github.com/micro/go-micro/v2/config/source" - 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" "github.com/micro/go-micro/v2/debug/trace" "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/registry" registrySrv "github.com/micro/go-micro/v2/registry/service" "github.com/micro/go-micro/v2/router" + srvRouter "github.com/micro/go-micro/v2/router/service" "github.com/micro/go-micro/v2/runtime" "github.com/micro/go-micro/v2/selector" "github.com/micro/go-micro/v2/server" @@ -33,58 +32,8 @@ import ( authutil "github.com/micro/go-micro/v2/util/auth" "github.com/micro/go-micro/v2/util/wrapper" - // clients - cgrpc "github.com/micro/go-micro/v2/client/grpc" - cmucp "github.com/micro/go-micro/v2/client/mucp" - - // servers - "github.com/micro/cli/v2" - - sgrpc "github.com/micro/go-micro/v2/server/grpc" - smucp "github.com/micro/go-micro/v2/server/mucp" - - // brokers - brokerHttp "github.com/micro/go-micro/v2/broker/http" - "github.com/micro/go-micro/v2/broker/memory" - "github.com/micro/go-micro/v2/broker/nats" - brokerSrv "github.com/micro/go-micro/v2/broker/service" - - // registries - "github.com/micro/go-micro/v2/registry/etcd" - "github.com/micro/go-micro/v2/registry/mdns" - rmem "github.com/micro/go-micro/v2/registry/memory" - regSrv "github.com/micro/go-micro/v2/registry/service" - - // routers - dnsRouter "github.com/micro/go-micro/v2/router/dns" - regRouter "github.com/micro/go-micro/v2/router/registry" - srvRouter "github.com/micro/go-micro/v2/router/service" - staticRouter "github.com/micro/go-micro/v2/router/static" - - // runtimes - kRuntime "github.com/micro/go-micro/v2/runtime/kubernetes" - lRuntime "github.com/micro/go-micro/v2/runtime/local" - srvRuntime "github.com/micro/go-micro/v2/runtime/service" - - // selectors - randSelector "github.com/micro/go-micro/v2/selector/random" - roundSelector "github.com/micro/go-micro/v2/selector/roundrobin" - - // transports - thttp "github.com/micro/go-micro/v2/transport/http" - tmem "github.com/micro/go-micro/v2/transport/memory" - - // stores - memStore "github.com/micro/go-micro/v2/store/memory" - svcStore "github.com/micro/go-micro/v2/store/service" - - // tracers - // jTracer "github.com/micro/go-micro/v2/debug/trace/jaeger" - memTracer "github.com/micro/go-micro/v2/debug/trace/memory" - - // auth - jwtAuth "github.com/micro/go-micro/v2/auth/jwt" - svcAuth "github.com/micro/go-micro/v2/auth/service" + configSrc "github.com/micro/go-micro/v2/config/source" + configSrv "github.com/micro/go-micro/v2/config/source/service" ) type Cmd interface { @@ -350,76 +299,31 @@ var ( }, } - DefaultBrokers = map[string]func(...broker.Option) broker.Broker{ - "service": brokerSrv.NewBroker, - "memory": memory.NewBroker, - "nats": nats.NewBroker, - "http": brokerHttp.NewBroker, - } + DefaultBrokers = map[string]func(...broker.Option) broker.Broker{} - DefaultClients = map[string]func(...client.Option) client.Client{ - "mucp": cmucp.NewClient, - "grpc": cgrpc.NewClient, - } + DefaultClients = map[string]func(...client.Option) client.Client{} - DefaultRegistries = map[string]func(...registry.Option) registry.Registry{ - "service": regSrv.NewRegistry, - "etcd": etcd.NewRegistry, - "mdns": mdns.NewRegistry, - "memory": rmem.NewRegistry, - } + DefaultRegistries = map[string]func(...registry.Option) registry.Registry{} - DefaultRouters = map[string]func(...router.Option) router.Router{ - "dns": dnsRouter.NewRouter, - "registry": regRouter.NewRouter, - "static": staticRouter.NewRouter, - "service": srvRouter.NewRouter, - } + DefaultRouters = map[string]func(...router.Option) router.Router{} - DefaultSelectors = map[string]func(...selector.Option) selector.Selector{ - "random": randSelector.NewSelector, - "roundrobin": roundSelector.NewSelector, - } + DefaultSelectors = map[string]func(...selector.Option) selector.Selector{} - DefaultServers = map[string]func(...server.Option) server.Server{ - "mucp": smucp.NewServer, - "grpc": sgrpc.NewServer, - } + DefaultServers = map[string]func(...server.Option) server.Server{} - DefaultTransports = map[string]func(...transport.Option) transport.Transport{ - "memory": tmem.NewTransport, - "http": thttp.NewTransport, - } + DefaultTransports = map[string]func(...transport.Option) transport.Transport{} - DefaultRuntimes = map[string]func(...runtime.Option) runtime.Runtime{ - "local": lRuntime.NewRuntime, - "service": srvRuntime.NewRuntime, - "kubernetes": kRuntime.NewRuntime, - } + DefaultRuntimes = map[string]func(...runtime.Option) runtime.Runtime{} - DefaultStores = map[string]func(...store.Option) store.Store{ - "memory": memStore.NewStore, - "service": svcStore.NewStore, - } + DefaultStores = map[string]func(...store.Option) store.Store{} - DefaultTracers = map[string]func(...trace.Option) trace.Tracer{ - "memory": memTracer.NewTracer, - // "jaeger": jTracer.NewTracer, - } + DefaultTracers = map[string]func(...trace.Option) trace.Tracer{} - DefaultAuths = map[string]func(...auth.Option) auth.Auth{ - "service": svcAuth.NewAuth, - "jwt": jwtAuth.NewAuth, - } + DefaultAuths = map[string]func(...auth.Option) auth.Auth{} - DefaultProfiles = map[string]func(...profile.Option) profile.Profile{ - "http": http.NewProfile, - "pprof": pprof.NewProfile, - } + DefaultProfiles = map[string]func(...profile.Option) profile.Profile{} - DefaultConfigs = map[string]func(...config.Option) (config.Config, error){ - "service": config.NewConfig, - } + DefaultConfigs = map[string]func(...config.Option) (config.Config, error){} ) func init() { @@ -616,6 +520,7 @@ func (c *cmd) Before(ctx *cli.Context) error { // Setup broker options. brokerOpts := []broker.Option{brokerSrv.Client(microClient)} + if len(ctx.String("broker_address")) > 0 { brokerOpts = append(brokerOpts, broker.Addrs(ctx.String("broker_address"))) } diff --git a/defaults.go b/defaults.go index 1e991e4d..815e591e 100644 --- a/defaults.go +++ b/defaults.go @@ -2,24 +2,133 @@ package micro import ( "github.com/micro/go-micro/v2/client" + "github.com/micro/go-micro/v2/cmd" + "github.com/micro/go-micro/v2/config" + "github.com/micro/go-micro/v2/debug/profile/http" + "github.com/micro/go-micro/v2/debug/profile/pprof" "github.com/micro/go-micro/v2/debug/trace" "github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/store" - // set defaults + // clients gcli "github.com/micro/go-micro/v2/client/grpc" - memTrace "github.com/micro/go-micro/v2/debug/trace/memory" + cmucp "github.com/micro/go-micro/v2/client/mucp" + gsrv "github.com/micro/go-micro/v2/server/grpc" - memoryStore "github.com/micro/go-micro/v2/store/memory" + smucp "github.com/micro/go-micro/v2/server/mucp" + + // brokers + brokerHttp "github.com/micro/go-micro/v2/broker/http" + "github.com/micro/go-micro/v2/broker/memory" + "github.com/micro/go-micro/v2/broker/nats" + brokerSrv "github.com/micro/go-micro/v2/broker/service" + + // registries + "github.com/micro/go-micro/v2/registry/etcd" + "github.com/micro/go-micro/v2/registry/mdns" + rmem "github.com/micro/go-micro/v2/registry/memory" + regSrv "github.com/micro/go-micro/v2/registry/service" + + // routers + dnsRouter "github.com/micro/go-micro/v2/router/dns" + regRouter "github.com/micro/go-micro/v2/router/registry" + srvRouter "github.com/micro/go-micro/v2/router/service" + staticRouter "github.com/micro/go-micro/v2/router/static" + + // runtimes + kRuntime "github.com/micro/go-micro/v2/runtime/kubernetes" + lRuntime "github.com/micro/go-micro/v2/runtime/local" + srvRuntime "github.com/micro/go-micro/v2/runtime/service" + + // selectors + randSelector "github.com/micro/go-micro/v2/selector/random" + roundSelector "github.com/micro/go-micro/v2/selector/roundrobin" + + // transports + thttp "github.com/micro/go-micro/v2/transport/http" + tmem "github.com/micro/go-micro/v2/transport/memory" + + // stores + memStore "github.com/micro/go-micro/v2/store/memory" + svcStore "github.com/micro/go-micro/v2/store/service" + + // tracers + // jTracer "github.com/micro/go-micro/v2/debug/trace/jaeger" + memTracer "github.com/micro/go-micro/v2/debug/trace/memory" + + // auth + jwtAuth "github.com/micro/go-micro/v2/auth/jwt" + svcAuth "github.com/micro/go-micro/v2/auth/service" ) func init() { + // set defaults + // default client client.DefaultClient = gcli.NewClient() // default server server.DefaultServer = gsrv.NewServer() // default store - store.DefaultStore = memoryStore.NewStore() + store.DefaultStore = memStore.NewStore() // set default trace - trace.DefaultTracer = memTrace.NewTracer() + trace.DefaultTracer = memTracer.NewTracer() + + // import all the plugins + + // auth + cmd.DefaultAuths["service"] = svcAuth.NewAuth + cmd.DefaultAuths["jwt"] = jwtAuth.NewAuth + + // broker + cmd.DefaultBrokers["service"] = brokerSrv.NewBroker + cmd.DefaultBrokers["memory"] = memory.NewBroker + cmd.DefaultBrokers["nats"] = nats.NewBroker + cmd.DefaultBrokers["http"] = brokerHttp.NewBroker + + // config + cmd.DefaultConfigs["service"] = config.NewConfig + + // client + cmd.DefaultClients["mucp"] = cmucp.NewClient + cmd.DefaultClients["grpc"] = gcli.NewClient + + // profiler + cmd.DefaultProfiles["http"] = http.NewProfile + cmd.DefaultProfiles["pprof"] = pprof.NewProfile + + // registry + cmd.DefaultRegistries["service"] = regSrv.NewRegistry + cmd.DefaultRegistries["etcd"] = etcd.NewRegistry + cmd.DefaultRegistries["mdns"] = mdns.NewRegistry + cmd.DefaultRegistries["memory"] = rmem.NewRegistry + + // runtime + cmd.DefaultRuntimes["local"] = lRuntime.NewRuntime + cmd.DefaultRuntimes["service"] = srvRuntime.NewRuntime + cmd.DefaultRuntimes["kubernetes"] = kRuntime.NewRuntime + + // router + cmd.DefaultRouters["dns"] = dnsRouter.NewRouter + cmd.DefaultRouters["registry"] = regRouter.NewRouter + cmd.DefaultRouters["static"] = staticRouter.NewRouter + cmd.DefaultRouters["service"] = srvRouter.NewRouter + + // selector + cmd.DefaultSelectors["random"] = randSelector.NewSelector + cmd.DefaultSelectors["roundrobin"] = roundSelector.NewSelector + + // server + cmd.DefaultServers["mucp"] = smucp.NewServer + cmd.DefaultServers["grpc"] = gsrv.NewServer + + // store + cmd.DefaultStores["memory"] = memStore.NewStore + cmd.DefaultStores["service"] = svcStore.NewStore + + // trace + cmd.DefaultTracers["memory"] = memTracer.NewTracer + + // transport + cmd.DefaultTransports["memory"] = tmem.NewTransport + cmd.DefaultTransports["http"] = thttp.NewTransport }