go-micro: github.com/micro/go-micro/server Index | Files | Directories

package server

import "github.com/micro/go-micro/server"

Package server is an interface for a micro server


Package Files

context.go extractor.go handler.go options.go rpc_codec.go rpc_handler.go rpc_request.go rpc_response.go rpc_router.go rpc_server.go rpc_stream.go server.go subscriber.go wrapper.go


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.micro.server"
    DefaultVersion                 = time.Now().Format("2006.")
    DefaultId                      = uuid.New().String()
    DefaultServer           Server = newRpcServer()
    DefaultRouter                  = newRpcRouter()
    DefaultRegisterCheck           = func(context.Context) error { return nil }
    DefaultRegisterInterval        = time.Second * 30
    DefaultRegisterTTL             = time.Minute

func Handle Uses

func Handle(h Handler) error

Handle registers a handler interface with the default server to handle inbound requests

func Init Uses

func Init(opt ...Option)

Init initialises the default server with options passed in

func NewContext Uses

func NewContext(ctx context.Context, s Server) context.Context

func NewRouter Uses

func NewRouter() *router

NewRouter returns a new router

func Run Uses

func Run() error

Run starts the default server and waits for a kill signal before exiting. Also registers/deregisters the server

func Start Uses

func Start() error

Start starts the default server

func Stop Uses

func Stop() error

Stop stops the default server

func String Uses

func String() string

String returns name of Server implementation

func Subscribe Uses

func Subscribe(s Subscriber) error

Subscribe registers a subscriber interface with the default server which subscribes to specified topic with the broker

type Handler Uses

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.


type Greeter struct {}

func (g *Greeter) Hello(context, request, response) error {
        return nil

func NewHandler Uses

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 Uses

type HandlerFunc func(ctx context.Context, req Request, rsp interface{}) error

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 Uses

type HandlerOption func(*HandlerOptions)

func EndpointMetadata Uses

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 Uses

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 Uses

type HandlerOptions struct {
    Internal bool
    Metadata map[string]map[string]string

type HandlerWrapper Uses

type HandlerWrapper func(HandlerFunc) HandlerFunc

HandlerWrapper wraps the HandlerFunc and returns the equivalent

type Message Uses

type Message interface {
    Topic() string
    Payload() interface{}
    ContentType() string

Message is an async message interface

type Option Uses

type Option func(*Options)

func Address Uses

func Address(a string) Option

Address to bind to - host:port

func Advertise(a string) Option

The address to advertise for discovery - host:port

func Broker Uses

func Broker(b broker.Broker) Option

Broker to use for pub/sub

func Codec Uses

func Codec(contentType string, c codec.NewCodec) Option

Codec to use to encode/decode requests for a given content type

func Id Uses

func Id(id string) Option

Unique server id

func Metadata Uses

func Metadata(md map[string]string) Option

Metadata associated with the server

func Name Uses

func Name(n string) Option

Server name

func RegisterCheck Uses

func RegisterCheck(fn func(context.Context) error) Option

RegisterCheck run func before registry service

func RegisterInterval Uses

func RegisterInterval(t time.Duration) Option

Register the service with at interval

func RegisterTTL Uses

func RegisterTTL(t time.Duration) Option

Register the service with a TTL

func Registry Uses

func Registry(r registry.Registry) Option

Registry used for discovery

func Transport Uses

func Transport(t transport.Transport) Option

Transport mechanism for communication e.g http, rabbitmq, etc

func Version Uses

func Version(v string) Option

Version of the service

func Wait Uses

func Wait(wg *sync.WaitGroup) Option

Wait tells the server to wait for requests to finish before exiting If `wg` is nil, server only wait for completion of rpc handler. For user need finer grained control, pass a concrete `wg` here, server will wait against it on stop.

func WithRouter Uses

func WithRouter(r Router) Option

WithRouter sets the request router

func WrapHandler Uses

func WrapHandler(w HandlerWrapper) Option

Adds a handler Wrapper to a list of options passed into the server

func WrapSubscriber Uses

func WrapSubscriber(w SubscriberWrapper) Option

Adds a subscriber Wrapper to a list of options passed into the server

type Options Uses

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

    // RegisterCheck runs a check function before registering the service
    RegisterCheck func(context.Context) error
    // The register expiry time
    RegisterTTL time.Duration
    // The interval on which to register
    RegisterInterval time.Duration

    // The router for requests
    Router Router

    // Other options for implementations of the interface
    // can be stored in a context
    Context context.Context

func DefaultOptions Uses

func DefaultOptions() Options

DefaultOptions returns config options for the default service

type Request Uses

type Request interface {
    // Service name requested
    Service() string
    // The action requested
    Method() string
    // Endpoint name requested
    Endpoint() string
    // Content type provided
    ContentType() string
    // Header of the request
    Header() map[string]string
    // Body is the initial decoded value
    Body() interface{}
    // 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 Uses

type Response interface {
    // Encoded writer
    Codec() codec.Writer
    // Write the header
    // write a response directly to the client
    Write([]byte) error

Response is the response writer for unencoded messages

type Router Uses

type Router interface {
    // ServeRequest processes a request to completion
    ServeRequest(context.Context, Request, Response) error

Router handle serving messages

type Server Uses

type Server interface {
    Options() Options
    Init(...Option) error
    Handle(Handler) error
    NewHandler(interface{}, ...HandlerOption) Handler
    NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
    Subscribe(Subscriber) error
    Start() error
    Stop() error
    String() string

Server is a simple micro server abstraction

func FromContext Uses

func FromContext(ctx context.Context) (Server, bool)

func NewServer Uses

func NewServer(opt ...Option) Server

NewServer returns a new server with options passed in

type Stream Uses

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 Uses

type StreamWrapper func(Stream) Stream

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 Uses

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 Uses

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 Uses

type SubscriberFunc func(ctx context.Context, msg Message) error

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 Uses

type SubscriberOption func(*SubscriberOptions)

func DisableAutoAck Uses

func DisableAutoAck() SubscriberOption

DisableAutoAck will disable auto acking of messages after they have been handled.

func InternalSubscriber Uses

func InternalSubscriber(b bool) SubscriberOption

Internal Subscriber options specifies that a subscriber is not advertised to the discovery system.

func SubscriberContext Uses

func SubscriberContext(ctx context.Context) SubscriberOption

SubscriberContext set context options to allow broker SubscriberOption passed

func SubscriberQueue Uses

func SubscriberQueue(n string) SubscriberOption

Shared queue name distributed messages across subscribers

type SubscriberOptions Uses

type SubscriberOptions struct {
    // AutoAck defaults to true. When a handler returns
    // with a nil error the message is acked.
    AutoAck  bool
    Queue    string
    Internal bool
    Context  context.Context

func NewSubscriberOptions Uses

func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions

type SubscriberWrapper Uses

type SubscriberWrapper func(SubscriberFunc) SubscriberFunc

SubscriberWrapper wraps the SubscriberFunc and returns the equivalent


grpcPackage grpc provides a grpc server
mucpPackage mucp provides an mucp server

Package server imports 37 packages (graph) and is imported by 470 packages. Updated 2019-09-27. Refresh now. Tools for package owners.