micro/server/server.go

167 lines
4.5 KiB
Go
Raw Permalink Normal View History

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"
"time"
2015-01-14 02:31:27 +03:00
"github.com/google/uuid"
"github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/metadata"
"github.com/unistack-org/micro/v3/register"
2015-01-14 02:31:27 +03:00
)
var (
// DefaultServer default server
DefaultServer Server = NewServer()
)
var (
// DefaultAddress will be used if no address passed
DefaultAddress = ":0"
// DefaultName will be used if no name passed
DefaultName = "server"
// DefaultVersion will be used if no version passed
DefaultVersion = "latest"
// DefaultId will be used if no id passed
DefaultId = uuid.New().String()
// DefaultRegisterCheck holds func that run before register server
DefaultRegisterCheck = func(context.Context) error { return nil }
// DefaultRegisterInterval holds interval for register
DefaultRegisterInterval = time.Second * 30
// DefaultRegisterTTL holds register record ttl, must be multiple of DefaultRegisterInterval
DefaultRegisterTTL = time.Second * 90
// DefaultNamespace will be used if no namespace passed
DefaultNamespace = "micro"
// DefaultMaxMsgSize holds default max msg ssize
DefaultMaxMsgSize = 1024 * 1024 * 4 // 4Mb
// DefaultMaxMsgRecvSize holds default max recv size
DefaultMaxMsgRecvSize = 1024 * 1024 * 4 // 4Mb
// DefaultMaxMsgSendSize holds default max send size
DefaultMaxMsgSendSize = 1024 * 1024 * 4 // 4Mb
)
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 {
// Name returns server name
Name() string
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
Handle(h Handler) error
2020-05-12 13:32:01 +03:00
// Create a new handler
NewHandler(h interface{}, opts ...HandlerOption) Handler
2020-05-12 13:32:01 +03:00
// Create a new subscriber
NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber
2020-05-12 13:32:01 +03:00
// Register a subscriber
Subscribe(s 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 {
// ProcessMessage processes a message
ProcessMessage(ctx context.Context, msg Message) error
2019-01-09 19:20:57 +03:00
// ServeRequest processes a request to completion
ServeRequest(ctx context.Context, req Request, rsp Response) error
2019-01-07 17:44:40 +03:00
}
*/
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 {
// Topic of the message
2015-12-02 23:56:50 +03:00
Topic() string
// The decoded payload value
2018-04-14 20:21:02 +03:00
Payload() interface{}
// The content type of the payload
2015-12-02 23:56:50 +03:00
ContentType() string
// The raw headers of the message
Header() metadata.Metadata
// The raw body of the message
Body() []byte
// Codec used to decode the message
Codec() codec.Codec
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() metadata.Metadata
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
Codec() codec.Codec
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.Codec
2019-01-09 22:11:47 +03:00
// Write the header
WriteHeader(md metadata.Metadata)
2019-01-09 22:11:47 +03:00
// 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(msg interface{}) error
Recv(msg interface{}) error
2015-12-17 23:37:35 +03:00
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() []*register.Endpoint
2018-11-26 17:51:42 +03:00
Options() HandlerOptions
}
// Subscriber interface represents a subscription to a given topic using
// a specific subscriber function or object with endpoints. It mirrors
2020-05-12 13:32:01 +03:00
// the handler in its behaviour.
2018-11-26 17:51:42 +03:00
type Subscriber interface {
Topic() string
Subscriber() interface{}
Endpoints() []*register.Endpoint
2018-11-26 17:51:42 +03:00
Options() SubscriberOptions
}