2015-01-14 02:31:27 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"code.google.com/p/go-uuid/uuid"
|
2015-01-31 18:49:21 +03:00
|
|
|
log "github.com/golang/glog"
|
2015-05-05 21:05:06 +03:00
|
|
|
"github.com/myodc/go-micro/registry"
|
2015-05-21 21:24:57 +03:00
|
|
|
"github.com/myodc/go-micro/transport"
|
2015-01-14 02:31:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server interface {
|
2015-05-27 00:39:48 +03:00
|
|
|
Config() options
|
|
|
|
Init(...Option)
|
2015-01-14 02:31:27 +03:00
|
|
|
NewReceiver(interface{}) Receiver
|
|
|
|
NewNamedReceiver(string, interface{}) Receiver
|
|
|
|
Register(Receiver) error
|
|
|
|
Start() error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:28:57 +03:00
|
|
|
type Option func(*options)
|
2015-05-21 21:24:57 +03:00
|
|
|
|
2015-01-14 02:31:27 +03:00
|
|
|
var (
|
2015-05-27 00:39:48 +03:00
|
|
|
DefaultAddress = ":0"
|
|
|
|
DefaultName = "go-server"
|
|
|
|
DefaultId = uuid.NewUUID().String()
|
|
|
|
DefaultServer Server = newRpcServer()
|
2015-01-14 02:31:27 +03:00
|
|
|
)
|
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
func Name(n string) Option {
|
2015-05-21 21:28:57 +03:00
|
|
|
return func(o *options) {
|
2015-05-27 00:39:48 +03:00
|
|
|
o.name = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Id(id string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.id = id
|
2015-05-21 21:28:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
func Address(a string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.address = a
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
func Registry(r registry.Registry) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.registry = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
func Transport(t transport.Transport) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.transport = t
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
2015-05-27 00:39:48 +03:00
|
|
|
}
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
func Metadata(md map[string]string) Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.metadata = md
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
2015-05-27 00:39:48 +03:00
|
|
|
}
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
func Config() options {
|
|
|
|
return DefaultServer.Config()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init(opt ...Option) {
|
2015-01-14 02:31:27 +03:00
|
|
|
if DefaultServer == nil {
|
2015-05-27 00:39:48 +03:00
|
|
|
DefaultServer = newRpcServer(opt...)
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
2015-05-27 00:39:48 +03:00
|
|
|
DefaultServer.Init(opt...)
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
|
|
|
|
2015-05-27 00:39:48 +03:00
|
|
|
func NewServer(opt ...Option) Server {
|
|
|
|
return newRpcServer(opt...)
|
2015-05-23 19:40:53 +03:00
|
|
|
}
|
|
|
|
|
2015-01-14 02:31:27 +03: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-27 00:39:48 +03:00
|
|
|
config := DefaultServer.Config()
|
2015-05-21 21:24:57 +03:00
|
|
|
var host string
|
|
|
|
var port int
|
2015-05-27 00:39:48 +03:00
|
|
|
parts := strings.Split(config.Address(), ":")
|
2015-05-21 21:24:57 +03: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 02:31:27 +03:00
|
|
|
|
|
|
|
// register service
|
2015-05-26 00:14:28 +03:00
|
|
|
node := ®istry.Node{
|
2015-05-27 00:39:48 +03:00
|
|
|
Id: config.Id(),
|
|
|
|
Address: host,
|
|
|
|
Port: port,
|
|
|
|
Metadata: config.Metadata(),
|
2015-05-26 00:14:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
service := ®istry.Service{
|
2015-05-27 00:39:48 +03:00
|
|
|
Name: config.Name(),
|
2015-05-26 00:14:28 +03:00
|
|
|
Nodes: []*registry.Node{node},
|
|
|
|
}
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2015-05-26 00:14:28 +03:00
|
|
|
log.Infof("Registering node: %s", node.Id)
|
2015-05-21 21:24:57 +03:00
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
err := config.registry.Register(service)
|
2015-05-21 21:24:57 +03:00
|
|
|
if err != nil {
|
2015-06-01 20:55:27 +03:00
|
|
|
log.Fatalf("Failed to register: %v", err)
|
2015-05-21 21:24:57 +03:00
|
|
|
}
|
2015-01-14 02:31:27 +03:00
|
|
|
|
|
|
|
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-05-26 00:14:28 +03:00
|
|
|
log.Infof("Deregistering %s", node.Id)
|
2015-06-01 20:55:27 +03:00
|
|
|
config.registry.Deregister(service)
|
2015-01-14 02:31:27 +03:00
|
|
|
|
|
|
|
return Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Start() error {
|
2015-05-27 00:39:48 +03:00
|
|
|
config := DefaultServer.Config()
|
|
|
|
log.Infof("Starting server %s id %s", config.Name(), config.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()
|
|
|
|
}
|