Documentation
¶
Overview ¶
Package server is an interface for a micro server
Index ¶
- Variables
- func Deregister() error
- func Handle(h Handler) error
- func Init(opt ...Option)
- func NewContext(ctx context.Context, s Server) context.Context
- func Register() error
- func Run() error
- func Start() error
- func Stop() error
- func String() string
- func Subscribe(s Subscriber) error
- type Debug
- type Handler
- type HandlerFunc
- type HandlerOption
- type HandlerOptions
- type HandlerWrapper
- type Message
- type Option
- func Address(a string) Option
- func Advertise(a string) Option
- func Broker(b broker.Broker) Option
- func Codec(contentType string, c codec.NewCodec) Option
- func DebugHandler(d debug.DebugHandler) Option
- func Id(id string) Option
- func Metadata(md map[string]string) Option
- func Name(n string) Option
- func RegisterTTL(t time.Duration) Option
- func Registry(r registry.Registry) Option
- func Transport(t transport.Transport) Option
- func Version(v string) Option
- func Wait(b bool) Option
- func WithRouter(r Router) Option
- func WrapHandler(w HandlerWrapper) Option
- func WrapSubscriber(w SubscriberWrapper) Option
- type Options
- type Request
- type Response
- type Router
- type Server
- type Stream
- type StreamWrapper
- type Subscriber
- type SubscriberFunc
- type SubscriberOption
- type SubscriberOptions
- type SubscriberWrapper
Constants ¶
This section is empty.
Variables ¶
var ( DefaultContentType = "application/protobuf" DefaultCodecs = map[string]codec.NewCodec{ "application/grpc": grpc.NewCodec, "application/grpc+json": grpc.NewCodec, "application/grpc+proto": grpc.NewCodec, "application/json": json.NewCodec, "application/json-rpc": jsonrpc.NewCodec, "application/protobuf": proto.NewCodec, "application/proto-rpc": protorpc.NewCodec, "application/octet-stream": raw.NewCodec, } )
var ( DefaultAddress = ":0" DefaultName = "go-server" DefaultVersion = "1.0.0" DefaultId = uuid.New().String() DefaultServer Server = newRpcServer() DefaultRouter = newRpcRouter() )
Functions ¶
func Deregister ¶
func Deregister() error
Deregister deregisters the default server from the discovery system
func Handle ¶
Handle registers a handler interface with the default server to handle inbound requests
func Register ¶
func Register() error
Register registers the default server with the discovery system
func Run ¶
func Run() error
Run starts the default server and waits for a kill signal before exiting. Also registers/deregisters the server
func Subscribe ¶
func Subscribe(s Subscriber) error
Subscribe registers a subscriber interface with the default server which subscribes to specified topic with the broker
Types ¶
type Debug ¶
type Debug struct {
debug.DebugHandler
}
We use this to wrap any debug handlers so we preserve the signature Debug.{Method}
type Handler ¶
type Handler interface {
Name() string
Handler() interface{}
Endpoints() []*registry.Endpoint
Options() HandlerOptions
}
Handler interface represents a request handler. It's generated by passing any type of public concrete object with endpoints into server.NewHandler. Most will pass in a struct.
Example:
type Greeter struct {}
func (g *Greeter) Hello(context, request, response) error {
return nil
}
func NewHandler ¶
func NewHandler(h interface{}, opts ...HandlerOption) Handler
NewHandler creates a new handler interface using the default server Handlers are required to be a public object with public endpoints. Call to a service endpoint such as Foo.Bar expects the type:
type Foo struct {}
func (f *Foo) Bar(ctx, req, rsp) error {
return nil
}
type HandlerFunc ¶
HandlerFunc represents a single method of a handler. It's used primarily for the wrappers. What's handed to the actual method is the concrete request and response types.
type HandlerOption ¶
type HandlerOption func(*HandlerOptions)
func EndpointMetadata ¶
func EndpointMetadata(name string, md map[string]string) HandlerOption
EndpointMetadata is a Handler option that allows metadata to be added to individual endpoints.
func InternalHandler ¶
func InternalHandler(b bool) HandlerOption
Internal Handler options specifies that a handler is not advertised to the discovery system. In the future this may also limit request to the internal network or authorised user.
type HandlerOptions ¶
type HandlerWrapper ¶
type HandlerWrapper func(HandlerFunc) HandlerFunc
HandlerWrapper wraps the HandlerFunc and returns the equivalent
type Option ¶
type Option func(*Options)
func WithRouter ¶ added in v0.20.0
WithRouter sets the request router
func WrapHandler ¶
func WrapHandler(w HandlerWrapper) Option
Adds a handler Wrapper to a list of options passed into the server
func WrapSubscriber ¶
func WrapSubscriber(w SubscriberWrapper) Option
Adds a subscriber Wrapper to a list of options passed into the server
type Options ¶
type Options struct {
Codecs map[string]codec.NewCodec
Broker broker.Broker
Registry registry.Registry
Transport transport.Transport
Metadata map[string]string
Name string
Address string
Advertise string
Id string
Version string
HdlrWrappers []HandlerWrapper
SubWrappers []SubscriberWrapper
// The register expiry time
RegisterTTL time.Duration
// The router for requests
Router Router
// Debug Handler which can be set by a user
DebugHandler debug.DebugHandler
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
}
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns config options for the default service
type Request ¶
type Request interface {
// Service name requested
Service() string
// Endpoint name requested
Endpoint() string
// Content type provided
ContentType() string
// Header of the request
Header() map[string]string
// Read the undecoded request body
Read() ([]byte, error)
// The encoded message stream
Codec() codec.Reader
// Indicates whether its a stream
Stream() bool
}
Request is a synchronous request interface
type Response ¶ added in v0.20.0
type Response interface {
// Encoded writer
Codec() codec.Writer
// Write the header
WriteHeader(map[string]string)
// write a response directly to the client
Write([]byte) error
}
Response is the response writer for unencoded messages
type Router ¶ added in v0.20.0
type Router interface {
// ServeRequest processes a request to completion
ServeRequest(context.Context, Request, Response) error
}
Router handle serving messages
type Server ¶
type Server interface {
Options() Options
Init(...Option) error
Handle(Handler) error
NewHandler(interface{}, ...HandlerOption) Handler
NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
Subscribe(Subscriber) error
Register() error
Deregister() error
Start() error
Stop() error
String() string
}
Server is a simple micro server abstraction
type Stream ¶ added in v0.8.0
type Stream interface {
Context() context.Context
Request() Request
Send(interface{}) error
Recv(interface{}) error
Error() error
Close() error
}
Stream 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 indicates end of the stream.
type StreamWrapper ¶ added in v0.8.0
StreamWrapper wraps a Stream interface and returns the equivalent. Because streams exist for the lifetime of a method invocation this is a convenient way to wrap a Stream as its in use for trace, monitoring, metrics, etc.
type Subscriber ¶
type Subscriber interface {
Topic() string
Subscriber() interface{}
Endpoints() []*registry.Endpoint
Options() SubscriberOptions
}
Subscriber interface represents a subscription to a given topic using a specific subscriber function or object with endpoints.
func NewSubscriber ¶
func NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber
NewSubscriber creates a new subscriber interface with the given topic and handler using the default server
type SubscriberFunc ¶
SubscriberFunc represents a single method of a subscriber. It's used primarily for the wrappers. What's handed to the actual method is the concrete publication message.
type SubscriberOption ¶
type SubscriberOption func(*SubscriberOptions)
func InternalSubscriber ¶
func InternalSubscriber(b bool) SubscriberOption
Internal Subscriber options specifies that a subscriber is not advertised to the discovery system.
func SubscriberQueue ¶
func SubscriberQueue(n string) SubscriberOption
Shared queue name distributed messages across subscribers
type SubscriberOptions ¶
type SubscriberWrapper ¶
type SubscriberWrapper func(SubscriberFunc) SubscriberFunc
SubscriberWrapper wraps the SubscriberFunc and returns the equivalent