Documentation ¶
Index ¶
- Variables
- type CallHandler
- type Config
- type Context
- type Error
- type Headers
- type InfChannel
- type Middleware
- type NextFunc
- type Request
- type RequestTarget
- type RequestType
- type Response
- type ResponseChannel
- type Router
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) SetErrorPostProc(fn func(error))
- func (r *Router) SetErrorPreProc(fn func(error) error)
- func (r *Router) SetHandler(method string, handler CallHandler, middleware ...Middleware)
- func (r *Router) SetStream(method string, handler StreamHandler, middleware ...Middleware)
- func (r *Router) Start(address string) error
- func (r *Router) Use(middleware ...Middleware)
- type StreamHandler
Constants ¶
This section is empty.
Variables ¶
var ( // ErrChanClosed is returned when writing to or reading from a closed channel ErrChanClosed = errors.New("channel closed") )
var ( // ErrContextCancelled is returned when detecting a cancelled context ErrContextCancelled = errors.New("context cancelled") )
Functions ¶
This section is empty.
Types ¶
type CallHandler ¶
CallHandler is used to register a handler for RPCs which require exactly one response.
type Config ¶
type Config struct { // Specifies the allowed origins that can initiate a websocket connection Origins []string }
type Context ¶
type Context interface { context.Context Request() *Request Response() *Response NewResponse() *Response HttpRequest() *http.Request WithValue(key interface{}, value interface{}) Context }
Context is passed to request handlers with data that might be needed to handle the request. It fulfills the Context interface and adds a few wsrpc specific methods as well.
type Error ¶
type Error struct { Code int `json:"code"` Message string `json:"message"` Data json.RawMessage `json:"data"` // contains filtered or unexported fields }
Error contains all the necessary info when handling or returning an error in the wsrpc server.
func IdIsRequiredError ¶
func IdIsRequiredError() *Error
IdIsRequiredError is called when an id is missing from a request.
func MethodNotFoundError ¶
MethodNotFoundError is called when a non existing method is requested.
func ServerError ¶
ServerError repackages any regular error message into a wsrpc error which can be passed to a response.
func TypeNotFoundError ¶
func TypeNotFoundError(t RequestType) *Error
TypeNotFoundError is called when an invalid request type is requested.
type Headers ¶
type Headers map[string]interface{}
Headers is the wsrpc equivalent of HTTP headers. They work much the same way.
type InfChannel ¶
type InfChannel struct {
// contains filtered or unexported fields
}
InfChannel is used to pass data of unspecified types inside the wsrpc server. It is not meant to be made available inside a request handler like it's sibling ResponseChannel is
type Middleware ¶
Middleware is passed to middlewares to allow chaining of multiple actions before and/or after calling the handler.
type Request ¶
type Request struct { Id int `json:"id"` // Legacy JobId uuid.UUID `json:"jobId"` Method string `json:"method"` Type RequestType `json:"type"` Params json.RawMessage `json:"params"` Header Headers `json:"header"` }
Request contains information about the job requested by the client.
func (*Request) MarshalJSON ¶
func (*Request) UnmarshalJSON ¶
type RequestTarget ¶
type RequestTarget Request
type RequestType ¶
type RequestType string
RequestType is used to denote what kind of job is being requested
const ( // TypeStream refers to jobs of streaming character. TypeStream RequestType = `STREAM` // TypeCall refers to jobs of on demand requests character. TypeCall RequestType = `CALL` )
type Response ¶
type Response struct { Id int `json:"id"` // Legacy JobId uuid.UUID `json:"jobId"` Result json.RawMessage `json:"result"` Header Headers `json:"header"` Error *Error `json:"error"` }
Response is serialized and passed back to the requester.
type ResponseChannel ¶
type ResponseChannel struct {
// contains filtered or unexported fields
}
ResponseChannel is passed to stream handlers to allow the implementer an easy way of sending responses back to the requester.
func NewResponseChannel ¶
func NewResponseChannel(size int) *ResponseChannel
NewResponseChannel returns a new ResponseChannel.
func (*ResponseChannel) Closed ¶
func (c *ResponseChannel) Closed() bool
Closed checks wether or not a ResponseChannel is closed.
func (*ResponseChannel) Write ¶
func (c *ResponseChannel) Write(msg *Response) (err error)
Write sends a message through the ResponseChannel. Stream handlers are configured to pass this message back to the requester if possible.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is used to demux incoming RPCs to the appropriate handlers.
func NewRouterFromConfig ¶
NewRouterFromConfig returns a new router from a custom Config.
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP is responsible for interpreting incoming HTTP requests and if appropriate upgrade the connection to web sockets. In either case it attempts to run the requested handler func.
func (*Router) SetErrorPostProc ¶
SettErrorPostProc is called on errors that are propagated out of the system. Outside of middlewares this is the last chance to handle the error.
func (*Router) SetErrorPreProc ¶
SetErrorPreProc is called on an error before it is propagated back out.
func (*Router) SetHandler ¶
func (r *Router) SetHandler(method string, handler CallHandler, middleware ...Middleware)
SetHandler registers a call handler func.
func (*Router) SetStream ¶
func (r *Router) SetStream(method string, handler StreamHandler, middleware ...Middleware)
SetStream registers a stream handler func.
func (*Router) Use ¶
func (r *Router) Use(middleware ...Middleware)
Use applies middleware to router
type StreamHandler ¶
type StreamHandler func(ctx Context, ch *ResponseChannel) (err error)
StreamHandler is sued to register handlers which need to be able to send an unknown number of responses for any given request.