move network initialiser code

This commit is contained in:
Asim Aslam 2019-07-01 22:59:11 +01:00
parent 032c3134c6
commit ebe3633082
2 changed files with 44 additions and 43 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/micro/go-micro/codec/proto" "github.com/micro/go-micro/codec/proto"
"github.com/micro/go-micro/config/options" "github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/network/proxy" "github.com/micro/go-micro/network/proxy"
"github.com/micro/go-micro/network/proxy/mucp"
"github.com/micro/go-micro/network/resolver" "github.com/micro/go-micro/network/resolver"
"github.com/micro/go-micro/network/router" "github.com/micro/go-micro/network/router"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
@ -683,3 +684,45 @@ func (s *socket) recv(m *Message, v interface{}) error {
// return unmarshalled // return unmarshalled
return s.codec.Unmarshal(m.Body, v.(gproto.Message)) return s.codec.Unmarshal(m.Body, v.(gproto.Message))
} }
// newNetwork returns a new network interface
func newNetwork(opts ...options.Option) *network {
options := options.NewOptions(opts...)
// new network instance
net := &network{
id: DefaultId,
}
// get network id
id, ok := options.Values().Get("network.id")
if ok {
net.id = id.(string)
}
// get router
r, ok := options.Values().Get("network.router")
if ok {
net.router = r.(router.Router)
} else {
net.router = router.DefaultRouter
}
// get proxy
p, ok := options.Values().Get("network.proxy")
if ok {
net.proxy = p.(proxy.Proxy)
} else {
net.proxy = new(mucp.Proxy)
}
// get resolver
res, ok := options.Values().Get("network.resolver")
if ok {
net.resolver = res.(resolver.Resolver)
} else {
net.resolver = new(nreg.Resolver)
}
return net
}

View File

@ -3,11 +3,6 @@ package network
import ( import (
"github.com/micro/go-micro/config/options" "github.com/micro/go-micro/config/options"
"github.com/micro/go-micro/network/proxy"
"github.com/micro/go-micro/network/proxy/mucp"
"github.com/micro/go-micro/network/resolver"
"github.com/micro/go-micro/network/resolver/registry"
"github.com/micro/go-micro/network/router"
) )
// Network defines a network interface. The network is a single // Network defines a network interface. The network is a single
@ -67,42 +62,5 @@ var (
// NewNetwork returns a new network interface // NewNetwork returns a new network interface
func NewNetwork(opts ...options.Option) Network { func NewNetwork(opts ...options.Option) Network {
options := options.NewOptions(opts...) return newNetwork(opts...)
// new network instance
net := &network{
id: DefaultId,
}
// get network id
id, ok := options.Values().Get("network.id")
if ok {
net.id = id.(string)
}
// get router
r, ok := options.Values().Get("network.router")
if ok {
net.router = r.(router.Router)
} else {
net.router = router.DefaultRouter
}
// get proxy
p, ok := options.Values().Get("network.proxy")
if ok {
net.proxy = p.(proxy.Proxy)
} else {
net.proxy = new(mucp.Proxy)
}
// get resolver
res, ok := options.Values().Get("network.resolver")
if ok {
net.resolver = res.(resolver.Resolver)
} else {
net.resolver = new(registry.Resolver)
}
return net
} }