2016-12-14 18:41:48 +03:00
|
|
|
// Package server is an interface for a micro server
|
2015-01-14 02:31:27 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2018-04-17 13:00:22 +03:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-08-21 17:43:46 +03:00
|
|
|
"time"
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2018-11-21 13:13:03 +03:00
|
|
|
"github.com/google/uuid"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/codec"
|
2020-03-11 20:55:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/logger"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2020-04-21 15:00:12 +03:00
|
|
|
signalutil "github.com/micro/go-micro/v2/util/signal"
|
2015-01-14 02:31:27 +03:00
|
|
|
)
|
|
|
|
|
2018-11-26 17:51:42 +03:00
|
|
|
// Server is a simple micro server abstraction
|
2015-01-14 02:31:27 +03:00
|
|
|
type Server interface {
|
2020-05-12 13:32:01 +03:00
|
|
|
// Initialise options
|
2016-01-02 22:12:17 +03:00
|
|
|
Init(...Option) error
|
2020-05-12 13:32:01 +03:00
|
|
|
// Retrieve the options
|
|
|
|
Options() Options
|
|
|
|
// Register a handler
|
2015-06-03 03:25:37 +03:00
|
|
|
Handle(Handler) error
|
2020-05-12 13:32:01 +03:00
|
|
|
// Create a new handler
|
2016-01-08 17:02:32 +03:00
|
|
|
NewHandler(interface{}, ...HandlerOption) Handler
|
2020-05-12 13:32:01 +03:00
|
|
|
// Create a new subscriber
|
2016-01-08 17:02:32 +03:00
|
|
|
NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
|
2020-05-12 13:32:01 +03:00
|
|
|
// Register a subscriber
|
2015-06-12 21:52:27 +03:00
|
|
|
Subscribe(Subscriber) error
|
2020-05-12 13:32:01 +03:00
|
|
|
// Start the server
|
2015-01-14 02:31:27 +03:00
|
|
|
Start() error
|
2020-05-12 13:32:01 +03:00
|
|
|
// Stop the server
|
2015-01-14 02:31:27 +03:00
|
|
|
Stop() error
|
2020-05-12 13:32:01 +03:00
|
|
|
// Server implementation
|
2015-12-20 00:56:14 +03:00
|
|
|
String() string
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
|
|
|
|
2019-01-07 17:44:40 +03:00
|
|
|
// Router handle serving messages
|
|
|
|
type Router interface {
|
2019-11-25 19:31:43 +03:00
|
|
|
// ProcessMessage processes a message
|
|
|
|
ProcessMessage(context.Context, Message) error
|
2019-01-09 19:20:57 +03:00
|
|
|
// ServeRequest processes a request to completion
|
2019-01-09 22:11:47 +03:00
|
|
|
ServeRequest(context.Context, Request, Response) error
|
2019-01-07 17:44:40 +03:00
|
|
|
}
|
|
|
|
|
2018-11-26 17:51:42 +03:00
|
|
|
// Message is an async message interface
|
2018-04-14 20:21:02 +03:00
|
|
|
type Message interface {
|
2019-11-25 19:31:43 +03:00
|
|
|
// Topic of the message
|
2015-12-02 23:56:50 +03:00
|
|
|
Topic() string
|
2019-11-25 19:31:43 +03:00
|
|
|
// The decoded payload value
|
2018-04-14 20:21:02 +03:00
|
|
|
Payload() interface{}
|
2019-11-25 19:31:43 +03:00
|
|
|
// The content type of the payload
|
2015-12-02 23:56:50 +03:00
|
|
|
ContentType() string
|
2019-11-25 19:31:43 +03:00
|
|
|
// The raw headers of the message
|
|
|
|
Header() map[string]string
|
|
|
|
// The raw body of the message
|
|
|
|
Body() []byte
|
|
|
|
// Codec used to decode the message
|
|
|
|
Codec() codec.Reader
|
2015-12-02 23:56:50 +03:00
|
|
|
}
|
|
|
|
|
2018-11-26 17:51:42 +03:00
|
|
|
// Request is a synchronous request interface
|
2015-12-02 23:56:50 +03:00
|
|
|
type Request interface {
|
2019-01-09 19:20:57 +03:00
|
|
|
// Service name requested
|
2015-12-02 23:56:50 +03:00
|
|
|
Service() string
|
2019-01-18 13:12:57 +03:00
|
|
|
// The action requested
|
|
|
|
Method() string
|
2019-01-11 00:25:31 +03:00
|
|
|
// Endpoint name requested
|
|
|
|
Endpoint() string
|
2019-01-09 19:20:57 +03:00
|
|
|
// Content type provided
|
2015-12-02 23:56:50 +03:00
|
|
|
ContentType() string
|
2019-01-09 22:11:47 +03:00
|
|
|
// Header of the request
|
|
|
|
Header() map[string]string
|
2019-02-04 16:13:03 +03:00
|
|
|
// Body is the initial decoded value
|
|
|
|
Body() interface{}
|
2019-01-09 22:11:47 +03:00
|
|
|
// Read the undecoded request body
|
|
|
|
Read() ([]byte, error)
|
|
|
|
// The encoded message stream
|
2019-01-09 22:28:13 +03:00
|
|
|
Codec() codec.Reader
|
2019-01-09 19:20:57 +03:00
|
|
|
// Indicates whether its a stream
|
2015-12-02 23:56:50 +03:00
|
|
|
Stream() bool
|
|
|
|
}
|
|
|
|
|
2019-01-09 22:11:47 +03:00
|
|
|
// Response is the response writer for unencoded messages
|
|
|
|
type Response interface {
|
2019-01-09 22:28:13 +03:00
|
|
|
// Encoded writer
|
|
|
|
Codec() codec.Writer
|
2019-01-09 22:11:47 +03:00
|
|
|
// Write the header
|
|
|
|
WriteHeader(map[string]string)
|
|
|
|
// write a response directly to the client
|
|
|
|
Write([]byte) error
|
|
|
|
}
|
|
|
|
|
2018-04-14 20:15:09 +03:00
|
|
|
// Stream represents a stream established with a client.
|
2015-12-17 23:37:35 +03:00
|
|
|
// A stream can be bidirectional which is indicated by the request.
|
|
|
|
// The last error will be left in Error().
|
2018-11-26 17:51:42 +03:00
|
|
|
// EOF indicates end of the stream.
|
2018-04-14 20:15:09 +03:00
|
|
|
type Stream interface {
|
2015-12-17 23:37:35 +03:00
|
|
|
Context() context.Context
|
|
|
|
Request() Request
|
|
|
|
Send(interface{}) error
|
|
|
|
Recv(interface{}) error
|
|
|
|
Error() error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2018-11-26 17:51:42 +03:00
|
|
|
// Handler interface represents a request handler. It's generated
|
2019-01-11 00:25:31 +03:00
|
|
|
// by passing any type of public concrete object with endpoints into server.NewHandler.
|
2018-11-26 17:51:42 +03:00
|
|
|
// Most will pass in a struct.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// type Greeter struct {}
|
|
|
|
//
|
|
|
|
// func (g *Greeter) Hello(context, request, response) error {
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
type Handler interface {
|
|
|
|
Name() string
|
|
|
|
Handler() interface{}
|
|
|
|
Endpoints() []*registry.Endpoint
|
|
|
|
Options() HandlerOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscriber interface represents a subscription to a given topic using
|
2020-05-12 13:32:01 +03:00
|
|
|
// a specific subscriber function or object with endpoints. It mirrors
|
|
|
|
// the handler in its behaviour.
|
2018-11-26 17:51:42 +03:00
|
|
|
type Subscriber interface {
|
|
|
|
Topic() string
|
|
|
|
Subscriber() interface{}
|
|
|
|
Endpoints() []*registry.Endpoint
|
|
|
|
Options() SubscriberOptions
|
|
|
|
}
|
|
|
|
|
2015-12-31 21:11:46 +03:00
|
|
|
type Option func(*Options)
|
2015-05-21 21:24:57 +03:00
|
|
|
|
2015-01-14 02:31:27 +03:00
|
|
|
var (
|
2019-09-23 19:59:34 +03:00
|
|
|
DefaultAddress = ":0"
|
|
|
|
DefaultName = "go.micro.server"
|
2020-01-22 20:07:56 +03:00
|
|
|
DefaultVersion = "latest"
|
2019-09-23 19:59:34 +03:00
|
|
|
DefaultId = uuid.New().String()
|
|
|
|
DefaultServer Server = newRpcServer()
|
|
|
|
DefaultRouter = newRpcRouter()
|
|
|
|
DefaultRegisterCheck = func(context.Context) error { return nil }
|
|
|
|
DefaultRegisterInterval = time.Second * 30
|
|
|
|
DefaultRegisterTTL = time.Minute
|
2020-01-18 22:53:51 +03:00
|
|
|
|
|
|
|
// NewServer creates a new server
|
|
|
|
NewServer func(...Option) Server = newRpcServer
|
2020-03-11 20:55:39 +03:00
|
|
|
log = logger.NewHelper(logger.DefaultLogger).WithFields(map[string]interface{}{"service": "server"})
|
2015-01-14 02:31:27 +03:00
|
|
|
)
|
|
|
|
|
2018-04-17 13:00:22 +03: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...)
|
|
|
|
}
|
|
|
|
|
2019-07-31 18:36:53 +03:00
|
|
|
// NewRouter returns a new router
|
|
|
|
func NewRouter() *router {
|
|
|
|
return newRpcRouter()
|
|
|
|
}
|
|
|
|
|
2018-04-17 13:00:22 +03: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
|
2019-01-11 00:25:31 +03:00
|
|
|
// endpoints. Call to a service endpoint such as Foo.Bar expects
|
2018-04-17 13:00:22 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan os.Signal, 1)
|
2020-04-21 15:14:20 +03:00
|
|
|
signal.Notify(ch, signalutil.Shutdown()...)
|
|
|
|
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.InfoLevel, log) {
|
|
|
|
log.Infof("Received signal %s", <-ch)
|
|
|
|
}
|
2018-04-17 13:00:22 +03:00
|
|
|
return Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the default server
|
|
|
|
func Start() error {
|
|
|
|
config := DefaultServer.Options()
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.InfoLevel, log) {
|
|
|
|
log.Infof("Starting server %s id %s", config.Name, config.Id)
|
|
|
|
}
|
2018-04-17 13:00:22 +03:00
|
|
|
return DefaultServer.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the default server
|
|
|
|
func Stop() error {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.InfoLevel, log) {
|
|
|
|
log.Infof("Stopping server")
|
|
|
|
}
|
2018-04-17 13:00:22 +03:00
|
|
|
return DefaultServer.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns name of Server implementation
|
|
|
|
func String() string {
|
|
|
|
return DefaultServer.String()
|
|
|
|
}
|