Some network inspiration

This commit is contained in:
Asim Aslam 2019-07-31 15:22:57 +01:00
parent 89fc142e47
commit fca89e06ef
3 changed files with 98 additions and 0 deletions

45
network/default.go Normal file
View File

@ -0,0 +1,45 @@
package network
import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
)
type network struct {
name string
options Options
}
func (n *network) Name() string {
return n.name
}
func (n *network) Connect() error {
return n.options.Server.Start()
}
func (n *network) Close() error {
return n.options.Server.Stop()
}
// NewNetwork returns a new network node
func NewNetwork(opts ...Option) Network {
options := Options{
Name: DefaultName,
Client: client.DefaultClient,
Server: server.DefaultServer,
}
for _, o := range opts {
o(&options)
}
// set the server name
options.Server.Init(
server.Name(options.Name),
)
return &network{
options: options,
}
}

18
network/network.go Normal file
View File

@ -0,0 +1,18 @@
// Package network is for building peer to peer networks
package network
// Network is a
type Network interface {
// Name of the network
Name() string
// Connect starts the network node
Connect() error
// Close shutsdown the network node
Close() error
}
var (
DefaultName = "go.micro.network"
DefaultNetwork = NewNetwork()
)

35
network/options.go Normal file
View File

@ -0,0 +1,35 @@
package network
import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
)
type Option func(*Options)
type Options struct {
Name string
Client client.Client
Server server.Server
}
// The network name
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
}
// The network client
func Client(c client.Client) Option {
return func(o *Options) {
o.Client = c
}
}
// The network server
func Server(s server.Server) Option {
return func(o *Options) {
o.Server = s
}
}