2015-01-14 02:31:27 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"code.google.com/p/go-uuid/uuid"
|
|
|
|
"github.com/asim/go-micro/registry"
|
2015-01-31 18:49:21 +03:00
|
|
|
log "github.com/golang/glog"
|
2015-01-14 02:31:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server interface {
|
|
|
|
Address() string
|
|
|
|
Init() error
|
|
|
|
NewReceiver(interface{}) Receiver
|
|
|
|
NewNamedReceiver(string, interface{}) Receiver
|
|
|
|
Register(Receiver) error
|
|
|
|
Start() error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-02-15 02:33:21 +03:00
|
|
|
Address string
|
2015-01-14 02:31:27 +03:00
|
|
|
Name string
|
|
|
|
Id string
|
|
|
|
DefaultServer Server
|
|
|
|
)
|
|
|
|
|
|
|
|
func Init() error {
|
2015-01-31 18:49:21 +03:00
|
|
|
defer log.Flush()
|
2015-01-14 02:31:27 +03:00
|
|
|
|
|
|
|
if len(Name) == 0 {
|
|
|
|
Name = "go-server"
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(Id) == 0 {
|
|
|
|
Id = Name + "-" + uuid.NewUUID().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if DefaultServer == nil {
|
2015-02-15 02:33:21 +03:00
|
|
|
DefaultServer = NewRpcServer(Address)
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return DefaultServer.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
parts := strings.Split(DefaultServer.Address(), ":")
|
|
|
|
host := strings.Join(parts[:len(parts)-1], ":")
|
|
|
|
port, _ := strconv.Atoi(parts[len(parts)-1])
|
|
|
|
|
|
|
|
// register service
|
|
|
|
node := registry.NewNode(Id, host, port)
|
|
|
|
service := registry.NewService(Name, node)
|
|
|
|
|
2015-01-31 18:49:21 +03:00
|
|
|
log.Infof("Registering %s", node.Id())
|
2015-01-14 02:31:27 +03:00
|
|
|
registry.Register(service)
|
|
|
|
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
|
2015-01-31 18:49:21 +03:00
|
|
|
log.Infof("Received signal %s", <-ch)
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-01-31 18:49:21 +03:00
|
|
|
log.Infof("Deregistering %s", node.Id())
|
2015-01-14 02:31:27 +03:00
|
|
|
registry.Deregister(service)
|
|
|
|
|
|
|
|
return Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Start() error {
|
2015-01-31 18:49:21 +03:00
|
|
|
log.Infof("Starting server %s id %s", Name, Id)
|
2015-01-14 02:31:27 +03:00
|
|
|
return DefaultServer.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Stop() error {
|
2015-01-31 18:49:21 +03:00
|
|
|
log.Infof("Stopping server")
|
2015-01-14 02:31:27 +03:00
|
|
|
return DefaultServer.Stop()
|
|
|
|
}
|