micro/server/server.go

169 lines
4.0 KiB
Go
Raw Normal View History

2016-12-14 15:41:48 +00:00
// Package server is an interface for a micro server
2015-01-13 23:31:27 +00:00
package server
import (
2018-03-03 11:53:52 +00:00
"context"
2018-04-17 11:00:22 +01:00
"os"
"os/signal"
"syscall"
2015-01-13 23:31:27 +00:00
2018-04-17 11:00:22 +01:00
"github.com/micro/go-log"
2015-08-26 12:15:37 +01:00
"github.com/pborman/uuid"
2015-01-13 23:31:27 +00:00
)
type Server interface {
Options() Options
2016-01-02 19:12:17 +00:00
Init(...Option) error
Handle(Handler) error
NewHandler(interface{}, ...HandlerOption) Handler
NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
Subscribe(Subscriber) error
Register() error
Deregister() error
2015-01-13 23:31:27 +00:00
Start() error
Stop() error
2015-12-19 21:56:14 +00:00
String() string
2015-01-13 23:31:27 +00:00
}
2018-04-14 18:21:02 +01:00
type Message interface {
2015-12-02 20:56:50 +00:00
Topic() string
2018-04-14 18:21:02 +01:00
Payload() interface{}
2015-12-02 20:56:50 +00:00
ContentType() string
}
type Request interface {
Service() string
Method() string
ContentType() string
Request() interface{}
2015-12-17 20:37:35 +00:00
// indicates whether the request will be streamed
2015-12-02 20:56:50 +00:00
Stream() bool
}
2018-04-14 18:15:09 +01:00
// Stream represents a stream established with a client.
2015-12-17 20:37:35 +00:00
// 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.
2018-04-14 18:15:09 +01:00
type Stream interface {
2015-12-17 20:37:35 +00:00
Context() context.Context
Request() Request
Send(interface{}) error
Recv(interface{}) error
Error() error
Close() error
}
type Option func(*Options)
2015-05-21 19:24:57 +01:00
type HandlerOption func(*HandlerOptions)
type SubscriberOption func(*SubscriberOptions)
2015-01-13 23:31:27 +00:00
var (
2015-05-26 22:39:48 +01:00
DefaultAddress = ":0"
DefaultName = "go-server"
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
)
2018-04-17 11:00:22 +01:00
// DefaultOptions returns config options for the default service
func DefaultOptions() Options {
return DefaultServer.Options()
}
// Init initialises the default server with options passed in
func Init(opt ...Option) {
if DefaultServer == nil {
DefaultServer = newRpcServer(opt...)
}
DefaultServer.Init(opt...)
}
2016-04-06 18:03:27 +01:00
// NewServer 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
}
2018-04-17 11:00:22 +01:00
// NewSubscriber creates a new subscriber interface with the given topic
// and handler using the default server
func NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber {
return DefaultServer.NewSubscriber(topic, h, opts...)
}
// NewHandler creates a new handler interface using the default server
// Handlers are required to be a public object with public
// methods. Call to a service method such as Foo.Bar expects
// the type:
//
// type Foo struct {}
// func (f *Foo) Bar(ctx, req, rsp) error {
// return nil
// }
//
func NewHandler(h interface{}, opts ...HandlerOption) Handler {
return DefaultServer.NewHandler(h, opts...)
}
// Handle registers a handler interface with the default server to
// handle inbound requests
func Handle(h Handler) error {
return DefaultServer.Handle(h)
}
// Subscribe registers a subscriber interface with the default server
// which subscribes to specified topic with the broker
func Subscribe(s Subscriber) error {
return DefaultServer.Subscribe(s)
}
// Register registers the default server with the discovery system
func Register() error {
return DefaultServer.Register()
}
// Deregister deregisters the default server from the discovery system
func Deregister() error {
return DefaultServer.Deregister()
}
// Run starts the default server and waits for a kill
// signal before exiting. Also registers/deregisters the server
func Run() error {
if err := Start(); err != nil {
return err
}
if err := DefaultServer.Register(); err != nil {
return err
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
log.Logf("Received signal %s", <-ch)
if err := DefaultServer.Deregister(); err != nil {
return err
}
return Stop()
}
// Start starts the default server
func Start() error {
config := DefaultServer.Options()
log.Logf("Starting server %s id %s", config.Name, config.Id)
return DefaultServer.Start()
}
// Stop stops the default server
func Stop() error {
log.Logf("Stopping server")
return DefaultServer.Stop()
}
// String returns name of Server implementation
func String() string {
return DefaultServer.String()
}