server

package
v0.0.0-...-cbea63e Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2021 License: Apache-2.0 Imports: 48 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Name is the name of the service such as com.hailocab.example
	Name string
	// Description is the human readable version for the Name
	Description string
	// Version is the timestamp of the release
	Version uint64
	// Source is the URL of the source code, eg: github.com/hailo-platform/H2O/foo
	Source string
	// OwnerEmail is the email address of the person who is responsible for this service
	OwnerEmail string
	// OwnerMobile is the international mobile number of the person responsible, eg: +44791111111
	OwnerMobile string
	// OwnerTeam is the name of the team who is responsible for this service, e.g. platform
	OwnerTeam string
	// InstanceID is the unique id of this running instance
	InstanceID string
	// ConcurrentRequests is how many concurrent requests we want to serve per calling service
	ConcurrentRequests int = 1000
)
View Source
var (
	PublishErrors = true // Services default to publishing errors to NSQ

)

Functions

func BadRoleError

func BadRoleError(req *Request) errors.Error

Given a request, returns a "bad role" error. This is used both by Authorise() and is useful within services which do row-level permission checking.

func BindAndRun

func BindAndRun()

BindAndRun enables self-binding for this service

func DisableServiceToServiceAuth

func DisableServiceToServiceAuth()

DisableServiceToServiceAuth stops the service attempting to load auth rules for service-to-service calls where it is not needed.

func Exit

func Exit(code int)

Exit provides a wrapper to os.Exit so that we can disconnect, cleanup and shutdown in a graceful manner.

func HandleRequest

func HandleRequest(req *Request)

HandleRequest and send back response

func HealthCheck

func HealthCheck(id string, checker slhc.Checker)

HealthCheck registers a standard health check for this server

func Init

func Init()

Init is a local init call that handles setup

func PriorityHealthCheck

func PriorityHealthCheck(id string, checker slhc.Checker, priority slhc.Priority)

PriorityHealthCheck is a healthcheck with a configurable priority for this server

func Register

func Register(eps ...*Endpoint) (err error)

Register an endpoint with the registry

func RegisterCleanupHandler

func RegisterCleanupHandler(ch CleanupHandler)

RegisterCleanupHandler adds a cleanup handler to the map so we can run it

func RegisterMiddleware

func RegisterMiddleware(mws ...Middleware) (err error)

func RegisterPostConnectHandler

func RegisterPostConnectHandler(pch PostConnectHandler)

RegisterPostConnectHandler adds a post connect handler to the map so we can run it

func Run

func Run()

Run the server by listening for messages

func RunWithOptions

func RunWithOptions(opts *Options)

RunWithOptions starts the server with user defined options

func ScopedRequest

func ScopedRequest(service, endpoint string, payload proto.Message) (*client.Request, error)

ScopedRequest returns a client request, prepared with scoping information of the service _making_ the call - such that service-to-service auth can work

func Scoper

func Scoper(req ...*Request) *serverScoper

Scoper mints something that is able to yield a scoped request for this server

Types

type Authoriser

type Authoriser interface {
	// Authorise should check the request and then return an error if problems
	Authorise(req *Request) errors.Error
}

Authoriser is an interface that anything that wants to authorise endpoints must satisfy

var DefaultAuthoriser Authoriser = RoleAuthoriser([]string{"ADMIN"})

func OpenToTheWorldAuthoriser

func OpenToTheWorldAuthoriser() Authoriser

OpenToTheWorldAuthoriser means ANYONE (in the whole world) can call a service

func RoleAuthoriser

func RoleAuthoriser(roles []string) Authoriser

RoleAuthoriser requires a service or user calling an endpoint to have ANY of these roles

func SignInAuthoriser

func SignInAuthoriser() Authoriser

SignInAuthoriser requires a real person to be signed in, but doesn't care about roles

func SignInRoleAuthoriser

func SignInRoleAuthoriser(roles []string) Authoriser

SignInRoleAuthoriser requires a real person signed in calling an endpoint to have ANY of these roles

type CleanupHandler

type CleanupHandler func()

CleanupHandler repesents a function we run after the service has been interrupted

type Endpoint

type Endpoint struct {
	// Name is the endpoint name, which should just be a single word, eg: "register"
	Name string
	// Mean is the mean average response time (time to generate response) promised for this endpoint
	Mean int32
	// Upper95 is 95th percentile response promised for this endpoint
	Upper95 int32
	// Handler is the function that will be fed requests to respond to
	Handler Handler
	// RequestProtocol is a struct type into which an inbound request for this endpoint can be unmarshaled
	RequestProtocol proto.Message
	// ResponseProtocol is the struct type defining the response format for this endpoint
	ResponseProtocol proto.Message
	// Subscribe indicates this endpoint should subscribe to a PUB stream - and gives us the stream address to SUB from
	//(topic name)
	Subscribe string
	// Authoriser is something that can check authorisation for this endpoint -- defaulting to ADMIN only (if nothing
	//specified by service)
	Authoriser Authoriser
	// contains filtered or unexported fields
}

Endpoint containing the name and handler to call with the request

func (*Endpoint) GetMean

func (ep *Endpoint) GetMean() int32

func (*Endpoint) GetName

func (ep *Endpoint) GetName() string

func (*Endpoint) GetUpper95

func (ep *Endpoint) GetUpper95() int32

func (*Endpoint) ProtoTypes

func (ep *Endpoint) ProtoTypes() (reflect.Type, reflect.Type)

ProtoTypes returns the Types of the registered request and response protocols

type Handler

type Handler func(req *Request) (proto.Message, errors.Error)

Handler interface

type Middleware

type Middleware func(*Endpoint, Handler) Handler

Middleware wraps a handler to provide additional features

type Options

type Options struct {
	SelfBind bool // bind self in rabbit
	Die      bool // don't die when discovery heartbeating fails
}

type PostConnectHandler

type PostConnectHandler func()

PostConnectHandler repesents a function we run after connecting to RabbitMQ

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request just wraps an AMQP delivery

func NewRequestFromDelivery

func NewRequestFromDelivery(d amqp.Delivery) *Request

NewRequestFromDelivery creates the Request object based on an AMQP delivery object

func NewRequestFromProto

func NewRequestFromProto(req proto.Message) *Request

func (*Request) Auth

func (self *Request) Auth() auth.Scope

Auth returns a fully-initialised authentication scope, from which you can determine if anyone is authenticated and who they are

func (*Request) Authorised

func (self *Request) Authorised() bool

Authorised returns the whether the request has already been authorised

func (*Request) Context

func (self *Request) Context() string

Context returns some string that is designed for us to base error messages from

func (*Request) Data

func (self *Request) Data() proto.Message

Data returns the unmarshalled inbound payload

func (*Request) Destination

func (self *Request) Destination() string

Destination returns the name of the server and endpoint that the request was directed at, for example: com.hailocab.service.customer.retrieve

func (*Request) Endpoint

func (self *Request) Endpoint() string

Endpoint returns the name of the endpoint part of the destination

func (*Request) From

func (self *Request) From() string

From returns information about which service sent this message @todo make this such that we can cryptographically verify this

func (*Request) IsPublication

func (self *Request) IsPublication() bool

IsPublication returns whether this is a publication, i.e. has a topic

func (*Request) MessageID

func (self *Request) MessageID() string

MessageID returns the ID of the underlying message transport system, which is unique per delivery, and only really useful for debugging

func (*Request) MessageType

func (self *Request) MessageType() string

func (*Request) ParentMessageID

func (self *Request) ParentMessageID() string

ParentMessageID returns the message ID of a message (if any) that was received and triggered this message In other words, we use this to build up the call stack / hierarchy

func (*Request) Payload

func (self *Request) Payload() []byte

func (*Request) RemoteAddr

func (self *Request) RemoteAddr() string

RemoteAddr returns the IP Address of the client, this may not be set if the request did not originate from the thinapi.

func (*Request) ReplyTo

func (self *Request) ReplyTo() string

ReplyTo gets the AMQP channel that we should be replying to

func (*Request) ScopedJsonRequest

func (self *Request) ScopedJsonRequest(service, endpoint string, payload []byte) (*client.Request, error)

ScopedJsonRequest does just the same as ScopedRequest but with JSON payload

func (*Request) ScopedRequest

func (self *Request) ScopedRequest(service, endpoint string, payload proto.Message) (*client.Request, error)

ScopedRequest returns a client request, prepared with any scoping information from _this_ inbound server request (in other words we are forwarding all the scope information such as trace ID, session ID etc.. with our new client request). This includes the scope of the service _making_ the call.

func (*Request) Service

func (self *Request) Service() string

Service returns the name of the service part of the destination

func (*Request) SessionID

func (self *Request) SessionID() string

SessionID returns the security context session ID, if there is one

func (*Request) SetAuth

func (self *Request) SetAuth(s auth.Scope)

SetAuth is useful for mocking/testing

func (*Request) SetData

func (self *Request) SetData(data proto.Message)

Sets what the expected unmarshaled data should be

func (*Request) SetSessionID

func (self *Request) SetSessionID(sessionID string) *Request

SetSessionID allows changing of the session ID if it changes during a request

func (*Request) String

func (self *Request) String() string

func (*Request) Topic

func (self *Request) Topic() string

Topic returns the topic that a Pub/Sub request goes to

func (*Request) TraceID

func (self *Request) TraceID() string

TraceID returns the trace ID, if there is one

func (*Request) TraceShouldPersist

func (self *Request) TraceShouldPersist() bool

TraceShouldPersist returns if the trace should be stored persistently

func (*Request) Unmarshal

func (self *Request) Unmarshal(into proto.Message) (err error)

Unmarshal the raw bytes payload of this request (into a protobuf)

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response wraps an AMQP delivery, with a payload & message type

func ErrorResponse

func ErrorResponse(replyTo *Request, err errors.Error) (*Response, error)

ErrorResponse wraps a normal response with a messageType of "error"

func PongResponse

func PongResponse(replyTo *Request) *Response

PongResponse sends a PONG message

func ReplyResponse

func ReplyResponse(replyTo *Request, payload proto.Message) (*Response, error)

ReplyResponse sends a normal response

func (*Response) ContentType

func (self *Response) ContentType() string

ContentType returns the content type of the delivery

func (*Response) MessageID

func (self *Response) MessageID() string

MessageID returns the message ID of the delivery

func (*Response) MessageType

func (self *Response) MessageType() string

MessageType returns the message type of the delivery (error, heartbeat etc)

func (*Response) Payload

func (self *Response) Payload() []byte

Payload returns the payload data

func (*Response) ReplyTo

func (self *Response) ReplyTo() string

ReplyTo returns the queue we should send the response to

Jump to

Keyboard shortcuts

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