fix: use registry from opts not use default directly:(#1436) (#1468)

web: use passed user registry, or default
This commit is contained in:
Edward 2020-04-05 18:15:38 +08:00 committed by GitHub
parent bc7579f1d8
commit 31a1ea6fae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/micro/go-micro/v2/registry"
)
//Options for web
type Options struct {
Name string
Version string
@ -67,7 +68,9 @@ func newOptions(opts ...Option) Options {
for _, o := range opts {
o(&opt)
}
if opt.Registry == nil {
opt.Registry = registry.DefaultRegistry
}
if opt.RegisterCheck == nil {
opt.RegisterCheck = DefaultRegisterCheck
}
@ -75,7 +78,7 @@ func newOptions(opts ...Option) Options {
return opt
}
// Server name
// Name of Web
func Name(n string) Option {
return func(o *Options) {
o.Name = n
@ -92,7 +95,7 @@ func Icon(ico string) Option {
}
}
// Unique server id
//Id for Unique server id
func Id(id string) Option {
return func(o *Options) {
o.Id = id
@ -120,7 +123,7 @@ func Address(a string) Option {
}
}
// The address to advertise for discovery - host:port
//Advertise The address to advertise for discovery - host:port
func Advertise(a string) Option {
return func(o *Options) {
o.Advertise = a
@ -143,26 +146,28 @@ func Registry(r registry.Registry) Option {
}
}
// Register the service with a TTL
//RegisterTTL Register the service with a TTL
func RegisterTTL(t time.Duration) Option {
return func(o *Options) {
o.RegisterTTL = t
}
}
// Register the service with at interval
//RegisterInterval Register the service with at interval
func RegisterInterval(t time.Duration) Option {
return func(o *Options) {
o.RegisterInterval = t
}
}
//Handler for custom handler
func Handler(h http.Handler) Option {
return func(o *Options) {
o.Handler = h
}
}
//Server for custom Server
func Server(srv *http.Server) Option {
return func(o *Options) {
o.Server = srv

View File

@ -268,7 +268,7 @@ func (s *service) stop() error {
func (s *service) Client() *http.Client {
rt := mhttp.NewRoundTripper(
mhttp.WithRegistry(registry.DefaultRegistry),
mhttp.WithRegistry(s.opts.Registry),
)
return &http.Client{
Transport: rt,

View File

@ -20,8 +20,10 @@ type Service interface {
Run() error
}
//Option for web
type Option func(o *Options)
//Web basic Defaults
var (
// For serving
DefaultName = "go-web"