2015-11-26 12:21:00 +00:00
|
|
|
/*
|
|
|
|
Server represents a server instance in go-micro which handles synchronous
|
|
|
|
requests via handlers and asynchronous requests via subscribers that
|
2015-11-26 00:17:05 +00:00
|
|
|
register with a broker.
|
|
|
|
|
2015-11-26 12:21:00 +00:00
|
|
|
The server combines the all the packages in go-micro to create a whole unit
|
|
|
|
used for building applications including discovery, client/server communication
|
2015-11-26 00:17:05 +00:00
|
|
|
and pub/sub.
|
2015-12-03 01:05:16 +00:00
|
|
|
|
|
|
|
import "github.com/micro/go-micro/server"
|
|
|
|
|
|
|
|
type Greeter struct {}
|
|
|
|
|
|
|
|
func (g *Greeter) Hello(ctx context.Context, req *greeter.Request, rsp *greeter.Response) error {
|
|
|
|
rsp.Msg = "Hello " + req.Name
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s := server.NewServer()
|
|
|
|
|
|
|
|
|
|
|
|
s.Handle(
|
|
|
|
s.NewHandler(&Greeter{}),
|
|
|
|
)
|
|
|
|
|
|
|
|
s.Start()
|
|
|
|
|
2015-11-26 00:17:05 +00:00
|
|
|
*/
|
2015-01-13 23:31:27 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2015-01-31 16:49:21 +01:00
|
|
|
log "github.com/golang/glog"
|
2015-08-26 12:15:37 +01:00
|
|
|
"github.com/pborman/uuid"
|
2015-01-13 23:31:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server interface {
|
2015-05-26 22:39:48 +01:00
|
|
|
Config() options
|
|
|
|
Init(...Option)
|
2015-06-03 01:25:37 +01:00
|
|
|
Handle(Handler) error
|
|
|
|
NewHandler(interface{}) Handler
|
2015-06-12 19:52:27 +01:00
|
|
|
NewSubscriber(string, interface{}) Subscriber
|
|
|
|
Subscribe(Subscriber) error
|
2015-06-03 01:25:37 +01:00
|
|
|
Register() error
|
|
|
|
Deregister() error
|
2015-01-13 23:31:27 +00:00
|
|
|
Start() error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
2015-12-02 20:56:50 +00:00
|
|
|
type Publication interface {
|
|
|
|
Topic() string
|
|
|
|
Message() interface{}
|
|
|
|
ContentType() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Request interface {
|
|
|
|
Service() string
|
|
|
|
Method() string
|
|
|
|
ContentType() string
|
|
|
|
Request() interface{}
|
2015-12-02 21:02:46 +00:00
|
|
|
// indicates whether the response should be streaming
|
2015-12-02 20:56:50 +00:00
|
|
|
Stream() bool
|
|
|
|
}
|
|
|
|
|
2015-05-21 19:28:57 +01:00
|
|
|
type Option func(*options)
|
2015-05-21 19:24:57 +01:00
|
|
|
|
2015-01-13 23:31:27 +00:00
|
|
|
var (
|
2015-05-26 22:39:48 +01:00
|
|
|
DefaultAddress = ":0"
|
|
|
|
DefaultName = "go-server"
|
2015-06-03 01:25:37 +01:00
|
|
|
DefaultVersion = "1.0.0"
|
2015-05-26 22:39:48 +01:00
|
|
|
DefaultId = uuid.NewUUID().String()
|
|
|
|
DefaultServer Server = newRpcServer()
|
2015-01-13 23:31:27 +00:00
|
|
|
)
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Returns config options for the default service
|
2015-05-26 22:39:48 +01:00
|
|
|
func Config() options {
|
|
|
|
return DefaultServer.Config()
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Initialises the default server with options passed in
|
2015-05-26 22:39:48 +01:00
|
|
|
func Init(opt ...Option) {
|
2015-01-13 23:31:27 +00:00
|
|
|
if DefaultServer == nil {
|
2015-05-26 22:39:48 +01:00
|
|
|
DefaultServer = newRpcServer(opt...)
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
2015-05-26 22:39:48 +01:00
|
|
|
DefaultServer.Init(opt...)
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Returns a new server with options passed in
|
2015-05-26 22:39:48 +01:00
|
|
|
func NewServer(opt ...Option) Server {
|
|
|
|
return newRpcServer(opt...)
|
2015-05-23 17:40:53 +01:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Creates a new subscriber interface with the given topic
|
|
|
|
// and handler using the default server
|
2015-06-12 19:52:27 +01:00
|
|
|
func NewSubscriber(topic string, h interface{}) Subscriber {
|
|
|
|
return DefaultServer.NewSubscriber(topic, h)
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Creates a new handler interface using the default server
|
2015-12-01 18:41:43 +00:00
|
|
|
// Handlers are required to be a public object with public
|
|
|
|
// methods. Call to a service method such as Foo.Bar expects
|
|
|
|
// the type:
|
|
|
|
//
|
2015-12-01 18:45:52 +00:00
|
|
|
// type Foo struct {}
|
|
|
|
// func (f *Foo) Bar(ctx, req, rsp) error {
|
|
|
|
// return nil
|
|
|
|
// }
|
2015-12-01 18:41:43 +00:00
|
|
|
//
|
2015-06-03 01:25:37 +01:00
|
|
|
func NewHandler(h interface{}) Handler {
|
|
|
|
return DefaultServer.NewHandler(h)
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Registers a handler interface with the default server to
|
|
|
|
// handle inbound requests
|
2015-06-03 01:25:37 +01:00
|
|
|
func Handle(h Handler) error {
|
|
|
|
return DefaultServer.Handle(h)
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Registers a subscriber interface with the default server
|
|
|
|
// which subscribes to specified topic with the broker
|
2015-06-12 19:52:27 +01:00
|
|
|
func Subscribe(s Subscriber) error {
|
|
|
|
return DefaultServer.Subscribe(s)
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Registers the default server with the discovery system
|
2015-06-03 01:25:37 +01:00
|
|
|
func Register() error {
|
|
|
|
return DefaultServer.Register()
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Deregisters the default server from the discovery system
|
2015-06-03 01:25:37 +01:00
|
|
|
func Deregister() error {
|
|
|
|
return DefaultServer.Deregister()
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Blocking run starts the default server and waits for a kill
|
|
|
|
// signal before exiting. Also registers/deregisters the server
|
2015-01-13 23:31:27 +00:00
|
|
|
func Run() error {
|
|
|
|
if err := Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-03 01:25:37 +01:00
|
|
|
if err := DefaultServer.Register(); err != nil {
|
|
|
|
return err
|
2015-05-21 19:24:57 +01:00
|
|
|
}
|
2015-01-13 23:31:27 +00:00
|
|
|
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
|
2015-01-31 16:49:21 +01:00
|
|
|
log.Infof("Received signal %s", <-ch)
|
2015-01-13 23:31:27 +00:00
|
|
|
|
2015-06-03 01:25:37 +01:00
|
|
|
if err := DefaultServer.Deregister(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-13 23:31:27 +00:00
|
|
|
return Stop()
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Starts the default server
|
2015-01-13 23:31:27 +00:00
|
|
|
func Start() error {
|
2015-05-26 22:39:48 +01:00
|
|
|
config := DefaultServer.Config()
|
|
|
|
log.Infof("Starting server %s id %s", config.Name(), config.Id())
|
2015-01-13 23:31:27 +00:00
|
|
|
return DefaultServer.Start()
|
|
|
|
}
|
|
|
|
|
2015-11-26 00:13:17 +00:00
|
|
|
// Stops the default server
|
2015-01-13 23:31:27 +00:00
|
|
|
func Stop() error {
|
2015-01-31 16:49:21 +01:00
|
|
|
log.Infof("Stopping server")
|
2015-01-13 23:31:27 +00:00
|
|
|
return DefaultServer.Stop()
|
|
|
|
}
|