2016-12-14 17:41:48 +02:00
|
|
|
// Package server is an interface for a micro server
|
2015-01-14 01:31:27 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2017-05-11 21:43:42 +02:00
|
|
|
"github.com/micro/go-log"
|
2015-08-26 13:15:37 +02:00
|
|
|
"github.com/pborman/uuid"
|
2015-12-17 22:37:35 +02:00
|
|
|
"golang.org/x/net/context"
|
2015-01-14 01:31:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server interface {
|
2015-12-31 20:11:46 +02:00
|
|
|
Options() Options
|
2016-01-02 21:12:17 +02:00
|
|
|
Init(...Option) error
|
2015-06-03 02:25:37 +02:00
|
|
|
Handle(Handler) error
|
2016-01-08 16:02:32 +02:00
|
|
|
NewHandler(interface{}, ...HandlerOption) Handler
|
|
|
|
NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
|
2015-06-12 20:52:27 +02:00
|
|
|
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-12-19 23:56:14 +02:00
|
|
|
String() string
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2015-12-02 22:56:50 +02:00
|
|
|
type Publication interface {
|
|
|
|
Topic() string
|
|
|
|
Message() interface{}
|
|
|
|
ContentType() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Request interface {
|
|
|
|
Service() string
|
|
|
|
Method() string
|
|
|
|
ContentType() string
|
|
|
|
Request() interface{}
|
2015-12-17 22:37:35 +02:00
|
|
|
// indicates whether the request will be streamed
|
2015-12-02 22:56:50 +02:00
|
|
|
Stream() bool
|
|
|
|
}
|
|
|
|
|
2015-12-17 22:37:35 +02:00
|
|
|
// Streamer represents a stream established with a client.
|
|
|
|
// A stream can be bidirectional which is indicated by the request.
|
|
|
|
// The last error will be left in Error().
|
|
|
|
// EOF indicated end of the stream.
|
|
|
|
type Streamer interface {
|
|
|
|
Context() context.Context
|
|
|
|
Request() Request
|
|
|
|
Send(interface{}) error
|
|
|
|
Recv(interface{}) error
|
|
|
|
Error() error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2015-12-31 20:11:46 +02:00
|
|
|
type Option func(*Options)
|
2015-05-21 20:24:57 +02:00
|
|
|
|
2016-01-08 16:02:32 +02:00
|
|
|
type HandlerOption func(*HandlerOptions)
|
|
|
|
|
|
|
|
type SubscriberOption func(*SubscriberOptions)
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// DefaultOptions returns config options for the default service
|
2015-12-31 20:11:46 +02:00
|
|
|
func DefaultOptions() Options {
|
|
|
|
return DefaultServer.Options()
|
2015-05-26 23:39:48 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// Init initialises the default server with options passed in
|
2015-05-26 23:39:48 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// NewServer returns a new server with options passed in
|
2015-05-26 23:39:48 +02:00
|
|
|
func NewServer(opt ...Option) Server {
|
|
|
|
return newRpcServer(opt...)
|
2015-05-23 18:40:53 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// NewSubscriber creates a new subscriber interface with the given topic
|
2015-11-26 02:13:17 +02:00
|
|
|
// and handler using the default server
|
2016-01-08 16:02:32 +02:00
|
|
|
func NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber {
|
|
|
|
return DefaultServer.NewSubscriber(topic, h, opts...)
|
2015-06-12 20:52:27 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// NewHandler creates a new handler interface using the default server
|
2015-12-01 20:41:43 +02: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 20:45:52 +02:00
|
|
|
// type Foo struct {}
|
|
|
|
// func (f *Foo) Bar(ctx, req, rsp) error {
|
|
|
|
// return nil
|
|
|
|
// }
|
2015-12-01 20:41:43 +02:00
|
|
|
//
|
2016-01-08 16:02:32 +02:00
|
|
|
func NewHandler(h interface{}, opts ...HandlerOption) Handler {
|
|
|
|
return DefaultServer.NewHandler(h, opts...)
|
2015-06-03 02:25:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// Handle registers a handler interface with the default server to
|
2015-11-26 02:13:17 +02:00
|
|
|
// handle inbound requests
|
2015-06-03 02:25:37 +02:00
|
|
|
func Handle(h Handler) error {
|
|
|
|
return DefaultServer.Handle(h)
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// Subscribe registers a subscriber interface with the default server
|
2015-11-26 02:13:17 +02:00
|
|
|
// which subscribes to specified topic with the broker
|
2015-06-12 20:52:27 +02:00
|
|
|
func Subscribe(s Subscriber) error {
|
|
|
|
return DefaultServer.Subscribe(s)
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// Register registers the default server with the discovery system
|
2015-06-03 02:25:37 +02:00
|
|
|
func Register() error {
|
|
|
|
return DefaultServer.Register()
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// Deregister deregisters the default server from the discovery system
|
2015-06-03 02:25:37 +02:00
|
|
|
func Deregister() error {
|
|
|
|
return DefaultServer.Deregister()
|
2015-01-14 01:31:27 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// Run starts the default server and waits for a kill
|
2015-11-26 02:13:17 +02:00
|
|
|
// signal before exiting. Also registers/deregisters the server
|
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)
|
2017-05-11 21:43:42 +02:00
|
|
|
log.Logf("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()
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// Start starts the default server
|
2015-01-14 01:31:27 +02:00
|
|
|
func Start() error {
|
2015-12-31 20:11:46 +02:00
|
|
|
config := DefaultServer.Options()
|
2017-05-11 21:43:42 +02:00
|
|
|
log.Logf("Starting server %s id %s", config.Name, config.Id)
|
2015-01-14 01:31:27 +02:00
|
|
|
return DefaultServer.Start()
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// Stop stops the default server
|
2015-01-14 01:31:27 +02:00
|
|
|
func Stop() error {
|
2017-05-11 21:43:42 +02:00
|
|
|
log.Logf("Stopping server")
|
2015-01-14 01:31:27 +02:00
|
|
|
return DefaultServer.Stop()
|
|
|
|
}
|
2015-12-19 23:56:14 +02:00
|
|
|
|
2016-04-06 19:03:27 +02:00
|
|
|
// String returns name of Server implementation
|
2015-12-19 23:56:14 +02:00
|
|
|
func String() string {
|
|
|
|
return DefaultServer.String()
|
|
|
|
}
|