1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-30 08:06:40 +02:00
go-micro/server/server.go

133 lines
2.4 KiB
Go
Raw Normal View History

2015-01-14 01:31:27 +02:00
package server
import (
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"code.google.com/p/go-uuid/uuid"
2015-01-31 17:49:21 +02:00
log "github.com/golang/glog"
2015-05-05 20:05:06 +02:00
"github.com/myodc/go-micro/registry"
2015-05-21 20:24:57 +02:00
"github.com/myodc/go-micro/transport"
2015-01-14 01:31:27 +02:00
)
type Server interface {
Address() string
Init() error
NewReceiver(interface{}) Receiver
NewNamedReceiver(string, interface{}) Receiver
Register(Receiver) error
Start() error
Stop() error
}
2015-05-21 20:24:57 +02:00
type options struct {
transport transport.Transport
}
2015-05-21 20:28:57 +02:00
type Option func(*options)
2015-05-21 20:24:57 +02:00
2015-01-14 01:31:27 +02:00
var (
Address string
2015-01-14 01:31:27 +02:00
Name string
Id string
DefaultServer Server
)
2015-05-21 20:28:57 +02:00
func Transport(t transport.Transport) Option {
return func(o *options) {
o.transport = t
}
}
2015-01-14 01:31:27 +02:00
func Init() error {
2015-01-31 17:49:21 +02:00
defer log.Flush()
2015-01-14 01:31:27 +02:00
if len(Name) == 0 {
Name = "go-server"
}
if len(Id) == 0 {
Id = Name + "-" + uuid.NewUUID().String()
}
if DefaultServer == nil {
2015-05-23 18:40:53 +02:00
DefaultServer = newRpcServer(Address)
2015-01-14 01:31:27 +02:00
}
return DefaultServer.Init()
}
func NewServer(address string, opt ...Option) Server {
2015-05-23 18:40:53 +02:00
return newRpcServer(address, opt...)
}
2015-01-14 01:31:27 +02:00
func NewReceiver(handler interface{}) Receiver {
return DefaultServer.NewReceiver(handler)
}
func NewNamedReceiver(path string, handler interface{}) Receiver {
return DefaultServer.NewNamedReceiver(path, handler)
}
func Register(r Receiver) error {
return DefaultServer.Register(r)
}
func Run() error {
if err := Start(); err != nil {
return err
}
// parse address for host, port
2015-05-21 20:24:57 +02:00
var host string
var port int
2015-01-14 01:31:27 +02:00
parts := strings.Split(DefaultServer.Address(), ":")
2015-05-21 20:24:57 +02:00
if len(parts) > 1 {
host = strings.Join(parts[:len(parts)-1], ":")
port, _ = strconv.Atoi(parts[len(parts)-1])
} else {
host = parts[0]
}
2015-01-14 01:31:27 +02:00
// register service
node := &registry.Node{
Id: Id,
Address: host,
Port: port,
}
service := &registry.Service{
Name: Name,
Nodes: []*registry.Node{node},
}
2015-01-14 01:31:27 +02:00
log.Infof("Registering node: %s", node.Id)
2015-05-21 20:24:57 +02:00
err := registry.Register(service)
if err != nil {
log.Fatal("Failed to register: %v", err)
}
2015-01-14 01:31:27 +02:00
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
2015-01-31 17:49:21 +02:00
log.Infof("Received signal %s", <-ch)
2015-01-14 01:31:27 +02:00
log.Infof("Deregistering %s", node.Id)
2015-01-14 01:31:27 +02:00
registry.Deregister(service)
return Stop()
}
func Start() error {
2015-01-31 17:49:21 +02:00
log.Infof("Starting server %s id %s", Name, Id)
2015-01-14 01:31:27 +02:00
return DefaultServer.Start()
}
func Stop() error {
2015-01-31 17:49:21 +02:00
log.Infof("Stopping server")
2015-01-14 01:31:27 +02:00
return DefaultServer.Stop()
}