...

Package server

import "github.com/gonitro/nitro/app/server"
Overview
Index
Subdirectories

Overview ▾

Package server is an interface for a micro server

Index ▾

Variables
type Handler
type HandlerFunc
type HandlerOption
    func EndpointMetadata(name string, md map[string]string) HandlerOption
    func InternalHandler(b bool) HandlerOption
type HandlerOptions
type HandlerWrapper
type Message
type Option
    func AddCheck(fn func(context.Context) error) Option
    func AddInterval(t time.Duration) Option
    func AddTTL(t time.Duration) Option
    func Address(a string) Option
    func Advertise(a string) Option
    func Auth(a crypto.Auth) Option
    func Broker(b event.Broker) Option
    func Codec(contentType string, c codec.NewCodec) Option
    func Context(ctx context.Context) Option
    func Id(id string) Option
    func Metadata(md map[string]string) Option
    func Name(n string) Option
    func Namespace(n string) Option
    func Registry(r registry.Table) Option
    func TLSConfig(t *tls.Config) Option
    func Transport(t network.Transport) Option
    func Version(v string) Option
    func Wait(wg *sync.WaitGroup) 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
    func DisableAutoAck() SubscriberOption
    func InternalSubscriber(b bool) SubscriberOption
    func SubscriberContext(ctx context.Context) SubscriberOption
    func SubscriberQueue(n string) SubscriberOption
type SubscriberOptions
    func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions
type SubscriberWrapper

Package files

handler.go options.go server.go wrapper.go

Variables

var (
    DefaultAddress     = "unix:///tmp/nitro.sock"
    DefaultName        = "nitro"
    DefaultVersion     = "latest"
    DefaultId          = uuid.New().String()
    DefaultAddCheck    = func(context.Context) error { return nil }
    DefaultAddInterval = time.Second * 30
    DefaultAddTTL      = time.Second * 90
)

type Handler

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 Handler interface {
    Name() string
    Handler() interface{}
    Endpoints() []*registry.Endpoint
    Options() HandlerOptions
}

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 HandlerFunc func(ctx context.Context, req Request, rsp interface{}) error

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

HandlerWrapper wraps the HandlerFunc and returns the equivalent

type HandlerWrapper func(HandlerFunc) HandlerFunc

type Message

Message is an async message interface

type Message interface {
    // Event of the message
    Event() string
    // The decoded payload value
    Payload() interface{}
    // The content type of the payload
    ContentType() string
    // 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
}

type Option

type Option func(*Options)

func AddCheck

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

AddCheck run func before registry service

func AddInterval

func AddInterval(t time.Duration) Option

Add the service with at interval

func AddTTL

func AddTTL(t time.Duration) Option

Add the service with a TTL

func Address

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 Auth

func Auth(a crypto.Auth) Option

Auth mechanism for role based access control

func Broker

func Broker(b event.Broker) Option

Broker to use for pub/sub

func Codec

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

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

func Context

func Context(ctx context.Context) Option

Context specifies a context for the service. Can be used to signal shutdown of the service Can be used for extra option values.

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 Namespace

func Namespace(n string) Option

Namespace to register handlers in

func Registry

func Registry(r registry.Table) Option

Registry used for discovery

func TLSConfig

func TLSConfig(t *tls.Config) Option

TLSConfig specifies a *tls.Config

func Transport

func Transport(t network.Transport) Option

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

func Version

func Version(v string) Option

Version of the service

func Wait

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

func WithRouter(r Router) Option

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       event.Broker
    Registry     registry.Table
    Auth         crypto.Auth
    Transport    network.Transport
    Metadata     map[string]string
    Name         string
    Address      string
    Advertise    string
    Id           string
    Namespace    string
    Version      string
    HdlrWrappers []HandlerWrapper
    SubWrappers  []SubscriberWrapper

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

    // The router for requests
    Router Router

    // TLSConfig specifies tls.Config for secure serving
    TLSConfig *tls.Config

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

type Request

Request is a synchronous request interface

type Request interface {
    // App name requested
    App() 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
}

type Response

Response is the response writer for unencoded messages

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
}

type Router

Router handle serving messages

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

type Server

Server is a simple micro server abstraction

type Server interface {
    // Initialise options
    Init(...Option) error
    // Retrieve the options
    Options() Options
    // Add a handler
    Handle(Handler) error
    // Create a new handler
    NewHandler(interface{}, ...HandlerOption) Handler
    // Create a new subscriber
    NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
    // Add a subscriber
    Subscribe(Subscriber) error
    // Start the server
    Start() error
    // Stop the server
    Stop() error
    // Server implementation
    String() string
}

type Stream

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 Stream interface {
    Context() context.Context
    Request() Request
    Send(interface{}) error
    Recv(interface{}) error
    Error() error
    Close() error
}

type StreamWrapper

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 StreamWrapper func(Stream) Stream

type Subscriber

Subscriber interface represents a subscription to a given event using a specific subscriber function or object with endpoints. It mirrors the handler in its behaviour.

type Subscriber interface {
    Event() string
    Subscriber() interface{}
    Endpoints() []*registry.Endpoint
    Options() SubscriberOptions
}

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 SubscriberFunc func(ctx context.Context, msg Message) error

type SubscriberOption

type SubscriberOption func(*SubscriberOptions)

func DisableAutoAck

func DisableAutoAck() SubscriberOption

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

func InternalSubscriber

func InternalSubscriber(b bool) SubscriberOption

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

func SubscriberContext

func SubscriberContext(ctx context.Context) SubscriberOption

SubscriberContext set context options to allow event SubscriberOption passed

func SubscriberQueue

func SubscriberQueue(n string) SubscriberOption

Shared queue name distributed messages across subscribers

type SubscriberOptions

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

func NewSubscriberOptions(opts ...SubscriberOption) SubscriberOptions

type SubscriberWrapper

SubscriberWrapper wraps the SubscriberFunc and returns the equivalent

type SubscriberWrapper func(SubscriberFunc) SubscriberFunc

Subdirectories

Name Synopsis
..
rpc Package rpc provides a network agnostic RPC server