server

package
v0.0.0-...-0436fc7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package server is an interface for a micro server

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultAddress = ":0"
	DefaultName    = "server-lite"
	DefaultVersion = time.Now().Format("2006.01.02.15.04")
	DefaultId      = uuid.New().String()
)

Functions

This section is empty.

Types

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
}

type HandlerFunc

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

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 HandlerOptions struct {
	Internal bool
	Metadata map[string]map[string]string
}

type HandlerWrapper

type HandlerWrapper func(HandlerFunc) HandlerFunc

HandlerWrapper wraps the HandlerFunc and returns the equivalent

type Option

type Option func(*Options)

func Address

func Address(a string) Option

Address to bind to - host:port

func Advertise(a string) Option

用于服务发现的地址,host:port

func Broker

func Broker(b broker.Broker) Option

Broker to use for pub/sub

func DebugHandler

func DebugHandler(d debug.DebugHandler) Option

DebugHandler for this server

func Id

func Id(id string) Option

Unique server id

func Metadata

func Metadata(md map[string]string) Option

Metadata associated with the server

func Name

func Name(n string) Option

Server name

func RegisterInterval

func RegisterInterval(t time.Duration) Option

Register the service with at interval

func RegisterTTL

func RegisterTTL(t time.Duration) Option

Register the service with a TTL

func Registry

func Registry(r registry.Registry) Option

Registry used for discovery

func Version

func Version(v string) Option

Version of the service

func Wait

func Wait(b bool) Option

Wait tells the server to wait for requests to finish before exiting

func WrapHandler

func WrapHandler(w HandlerWrapper) Option

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

type Options

type Options struct {
	Broker       broker.Broker
	Registry     registry.Registry
	Metadata     map[string]string
	Name         string
	Address      string
	Advertise    string
	Id           string
	Version      string
	HdlrWrappers []HandlerWrapper
	// The register expiry time
	RegisterTTL time.Duration
	// The interval on which to register
	RegisterInterval time.Duration
	// 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
}

type Request

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)
	// Indicates whether its a stream
	Stream() bool
}

Request is a synchronous request interface

type Response

type Response interface {
	// 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 Server

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

Server is a simple micro server abstraction

type Stream

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

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.

Directories

Path Synopsis
proto
Package debug is a generated protocol buffer package.
Package debug is a generated protocol buffer package.
Package grpc provides a grpc server
Package grpc provides a grpc server
Package rpc provides an rpc server
Package rpc provides an rpc server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL