2015-01-14 01:31:27 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2015-01-31 17:49:21 +02:00
|
|
|
log "github.com/golang/glog"
|
2015-08-26 13:15:37 +02:00
|
|
|
"github.com/pborman/uuid"
|
2015-01-14 01:31:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server interface {
|
2015-05-26 23:39:48 +02:00
|
|
|
Config() options
|
|
|
|
Init(...Option)
|
2015-06-03 02:25:37 +02:00
|
|
|
Handle(Handler) error
|
|
|
|
NewHandler(interface{}) Handler
|
2015-06-12 20:52:27 +02:00
|
|
|
NewSubscriber(string, interface{}) Subscriber
|
|
|
|
Subscribe(Subscriber) error
|
2015-06-03 02:25:37 +02:00
|
|
|
Register() error
|
|
|
|
Deregister() error
|
2015-01-14 01:31:27 +02:00
|
|
|
Start() error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
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 (
|
2015-05-26 23:39:48 +02:00
|
|
|
DefaultAddress = ":0"
|
|
|
|
DefaultName = "go-server"
|
2015-06-03 02:25:37 +02:00
|
|
|
DefaultVersion = "1.0.0"
|
2015-05-26 23:39:48 +02:00
|
|
|
DefaultId = uuid.NewUUID().String()
|
|
|
|
DefaultServer Server = newRpcServer()
|
2015-01-14 01:31:27 +02:00
|
|
|
)
|
|
|
|
|
2015-05-26 23:39:48 +02:00
|
|
|
func Config() options {
|
|
|
|
return DefaultServer.Config()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init(opt ...Option) {
|
2015-01-14 01:31:27 +02:00
|
|
|
if DefaultServer == nil {
|
2015-05-26 23:39:48 +02:00
|
|
|
DefaultServer = newRpcServer(opt...)
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
2015-05-26 23:39:48 +02:00
|
|
|
DefaultServer.Init(opt...)
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-05-26 23:39:48 +02:00
|
|
|
func NewServer(opt ...Option) Server {
|
|
|
|
return newRpcServer(opt...)
|
2015-05-23 18:40:53 +02:00
|
|
|
}
|
|
|
|
|
2015-06-12 20:52:27 +02:00
|
|
|
func NewSubscriber(topic string, h interface{}) Subscriber {
|
|
|
|
return DefaultServer.NewSubscriber(topic, h)
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
func NewHandler(h interface{}) Handler {
|
|
|
|
return DefaultServer.NewHandler(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Handle(h Handler) error {
|
|
|
|
return DefaultServer.Handle(h)
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-06-12 20:52:27 +02:00
|
|
|
func Subscribe(s Subscriber) error {
|
|
|
|
return DefaultServer.Subscribe(s)
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
func Register() error {
|
|
|
|
return DefaultServer.Register()
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
func Deregister() error {
|
|
|
|
return DefaultServer.Deregister()
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Run() error {
|
|
|
|
if err := Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
if err := DefaultServer.Register(); err != nil {
|
|
|
|
return err
|
2015-05-21 20:24:57 +02:00
|
|
|
}
|
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
|
|
|
|
2015-06-03 02:25:37 +02:00
|
|
|
if err := DefaultServer.Deregister(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-14 01:31:27 +02:00
|
|
|
return Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Start() error {
|
2015-05-26 23:39:48 +02:00
|
|
|
config := DefaultServer.Config()
|
|
|
|
log.Infof("Starting server %s id %s", config.Name(), config.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()
|
|
|
|
}
|