Move defaults to defaults.go

This commit is contained in:
Asim Aslam 2020-07-19 19:48:11 +01:00
parent 05f3e1a125
commit 9b74bc52d6
2 changed files with 133 additions and 119 deletions

View File

@ -10,21 +10,20 @@ import (
"strings" "strings"
"time" "time"
"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2/auth" "github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/broker" "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"
"github.com/micro/go-micro/v2/client/grpc" "github.com/micro/go-micro/v2/client/grpc"
"github.com/micro/go-micro/v2/config" "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"
"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/debug/trace"
"github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/registry" "github.com/micro/go-micro/v2/registry"
registrySrv "github.com/micro/go-micro/v2/registry/service" registrySrv "github.com/micro/go-micro/v2/registry/service"
"github.com/micro/go-micro/v2/router" "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/runtime"
"github.com/micro/go-micro/v2/selector" "github.com/micro/go-micro/v2/selector"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/server"
@ -33,58 +32,8 @@ import (
authutil "github.com/micro/go-micro/v2/util/auth" authutil "github.com/micro/go-micro/v2/util/auth"
"github.com/micro/go-micro/v2/util/wrapper" "github.com/micro/go-micro/v2/util/wrapper"
// clients configSrc "github.com/micro/go-micro/v2/config/source"
cgrpc "github.com/micro/go-micro/v2/client/grpc" configSrv "github.com/micro/go-micro/v2/config/source/service"
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"
) )
type Cmd interface { type Cmd interface {
@ -350,76 +299,31 @@ var (
}, },
} }
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{ DefaultBrokers = map[string]func(...broker.Option) broker.Broker{}
"service": brokerSrv.NewBroker,
"memory": memory.NewBroker,
"nats": nats.NewBroker,
"http": brokerHttp.NewBroker,
}
DefaultClients = map[string]func(...client.Option) client.Client{ DefaultClients = map[string]func(...client.Option) client.Client{}
"mucp": cmucp.NewClient,
"grpc": cgrpc.NewClient,
}
DefaultRegistries = map[string]func(...registry.Option) registry.Registry{ DefaultRegistries = map[string]func(...registry.Option) registry.Registry{}
"service": regSrv.NewRegistry,
"etcd": etcd.NewRegistry,
"mdns": mdns.NewRegistry,
"memory": rmem.NewRegistry,
}
DefaultRouters = map[string]func(...router.Option) router.Router{ DefaultRouters = map[string]func(...router.Option) router.Router{}
"dns": dnsRouter.NewRouter,
"registry": regRouter.NewRouter,
"static": staticRouter.NewRouter,
"service": srvRouter.NewRouter,
}
DefaultSelectors = map[string]func(...selector.Option) selector.Selector{ DefaultSelectors = map[string]func(...selector.Option) selector.Selector{}
"random": randSelector.NewSelector,
"roundrobin": roundSelector.NewSelector,
}
DefaultServers = map[string]func(...server.Option) server.Server{ DefaultServers = map[string]func(...server.Option) server.Server{}
"mucp": smucp.NewServer,
"grpc": sgrpc.NewServer,
}
DefaultTransports = map[string]func(...transport.Option) transport.Transport{ DefaultTransports = map[string]func(...transport.Option) transport.Transport{}
"memory": tmem.NewTransport,
"http": thttp.NewTransport,
}
DefaultRuntimes = map[string]func(...runtime.Option) runtime.Runtime{ DefaultRuntimes = map[string]func(...runtime.Option) runtime.Runtime{}
"local": lRuntime.NewRuntime,
"service": srvRuntime.NewRuntime,
"kubernetes": kRuntime.NewRuntime,
}
DefaultStores = map[string]func(...store.Option) store.Store{ DefaultStores = map[string]func(...store.Option) store.Store{}
"memory": memStore.NewStore,
"service": svcStore.NewStore,
}
DefaultTracers = map[string]func(...trace.Option) trace.Tracer{ DefaultTracers = map[string]func(...trace.Option) trace.Tracer{}
"memory": memTracer.NewTracer,
// "jaeger": jTracer.NewTracer,
}
DefaultAuths = map[string]func(...auth.Option) auth.Auth{ DefaultAuths = map[string]func(...auth.Option) auth.Auth{}
"service": svcAuth.NewAuth,
"jwt": jwtAuth.NewAuth,
}
DefaultProfiles = map[string]func(...profile.Option) profile.Profile{ DefaultProfiles = map[string]func(...profile.Option) profile.Profile{}
"http": http.NewProfile,
"pprof": pprof.NewProfile,
}
DefaultConfigs = map[string]func(...config.Option) (config.Config, error){ DefaultConfigs = map[string]func(...config.Option) (config.Config, error){}
"service": config.NewConfig,
}
) )
func init() { func init() {
@ -616,6 +520,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
// Setup broker options. // Setup broker options.
brokerOpts := []broker.Option{brokerSrv.Client(microClient)} brokerOpts := []broker.Option{brokerSrv.Client(microClient)}
if len(ctx.String("broker_address")) > 0 { if len(ctx.String("broker_address")) > 0 {
brokerOpts = append(brokerOpts, broker.Addrs(ctx.String("broker_address"))) brokerOpts = append(brokerOpts, broker.Addrs(ctx.String("broker_address")))
} }

View File

@ -2,24 +2,133 @@ package micro
import ( import (
"github.com/micro/go-micro/v2/client" "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/debug/trace"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/store"
// set defaults // clients
gcli "github.com/micro/go-micro/v2/client/grpc" 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" 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() { func init() {
// set defaults
// default client // default client
client.DefaultClient = gcli.NewClient() client.DefaultClient = gcli.NewClient()
// default server // default server
server.DefaultServer = gsrv.NewServer() server.DefaultServer = gsrv.NewServer()
// default store // default store
store.DefaultStore = memoryStore.NewStore() store.DefaultStore = memStore.NewStore()
// set default trace // 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
} }