Documentation ¶
Overview ¶
Commons for HTTP handling
Index ¶
- Constants
- func Listen(addr string, maxOpenConnections int) (listener net.Listener, err error)
- func MaxBytesHandler(h http.Handler, maxBytes int64) http.Handler
- func OnDisconnect(onDisconnect func(remoteAddr string)) func(*wsConnection)
- func PingPeriod(pingPeriod time.Duration) func(*wsConnection)
- func ReadLimit(readLimit int64) func(*wsConnection)
- func ReadWait(readWait time.Duration) func(*wsConnection)
- func RegisterRPCFuncs(mux *http.ServeMux, funcMap map[string]*RPCFunc, logger log.Logger)
- func Serve(ctx context.Context, listener net.Listener, handler http.Handler, ...) error
- func ServeTLS(ctx context.Context, listener net.Listener, handler http.Handler, ...) error
- type Config
- type RPCFunc
- type WebsocketManager
Constants ¶
const DefaultRPCTimeout = 60 * time.Second
DefaultRPCTimeout is the default context timeout for calls to any RPC method that does not override it with a more specific timeout.
Variables ¶
This section is empty.
Functions ¶
func Listen ¶
Listen starts a new net.Listener on the given address. It returns an error if the address is invalid or the call to Listen() fails.
func MaxBytesHandler ¶
MaxBytesHandler wraps h in a handler that limits the size of the request body to at most maxBytes. If maxBytes <= 0, the request body is not limited.
func OnDisconnect ¶
func OnDisconnect(onDisconnect func(remoteAddr string)) func(*wsConnection)
OnDisconnect sets a callback which is used upon disconnect - not Goroutine-safe. Nop by default.
func PingPeriod ¶
PingPeriod sets the duration for sending websocket pings. It should only be used in the constructor - not Goroutine-safe.
func ReadLimit ¶
func ReadLimit(readLimit int64) func(*wsConnection)
ReadLimit sets the maximum size for reading message. It should only be used in the constructor - not Goroutine-safe.
func ReadWait ¶
ReadWait sets the amount of time to wait before a websocket read times out. It should only be used in the constructor - not Goroutine-safe.
func RegisterRPCFuncs ¶
RegisterRPCFuncs adds a route to mux for each non-websocket function in the funcMap, and also a root JSON-RPC POST handler.
func Serve ¶
func Serve(ctx context.Context, listener net.Listener, handler http.Handler, logger log.Logger, config *Config) error
Serve creates a http.Server and calls Serve with the given listener. It wraps handler to recover panics and limit the request body size.
func ServeTLS ¶
func ServeTLS(ctx context.Context, listener net.Listener, handler http.Handler, certFile, keyFile string, logger log.Logger, config *Config) error
Serve creates a http.Server and calls ServeTLS with the given listener, certFile and keyFile. It wraps handler to recover panics and limit the request body size.
Types ¶
type Config ¶
type Config struct { // The maximum number of connections that will be accepted by the listener. // See https://godoc.org/golang.org/x/net/netutil#LimitListener MaxOpenConnections int // Used to set the HTTP server's per-request read timeout. // See https://godoc.org/net/http#Server.ReadTimeout ReadTimeout time.Duration // Used to set the HTTP server's per-request write timeout. Note that this // affects ALL methods on the server, so it should not be set too low. This // should be used as a safety valve, not a resource-control timeout. // // See https://godoc.org/net/http#Server.WriteTimeout WriteTimeout time.Duration // Controls the maximum number of bytes the server will read parsing the // request body. MaxBodyBytes int64 // Controls the maximum size of a request header. // See https://godoc.org/net/http#Server.MaxHeaderBytes MaxHeaderBytes int }
Config is a RPC server configuration.
type RPCFunc ¶
type RPCFunc struct {
// contains filtered or unexported fields
}
RPCFunc contains the introspected type information for a function.
func NewRPCFunc ¶
func NewRPCFunc(f interface{}) *RPCFunc
NewRPCFunc constructs an RPCFunc for f, which must be a function whose type signature matches one of these schemes:
func(context.Context) error func(context.Context) (R, error) func(context.Context, *T) error func(context.Context, *T) (R, error)
for an arbitrary struct type T and type R. NewRPCFunc will panic if f does not have one of these forms. A newly-constructed RPCFunc has a default timeout of DefaultRPCTimeout; use the Timeout method to adjust this as needed.
func NewWSRPCFunc ¶
func NewWSRPCFunc(f interface{}) *RPCFunc
NewWSRPCFunc behaves as NewRPCFunc, but marks the resulting function for use via websocket.
type WebsocketManager ¶
WebsocketManager provides a WS handler for incoming connections and passes a map of functions along with any additional params to new connections. NOTE: The websocket path is defined externally, e.g. in node/node.go
func NewWebsocketManager ¶
func NewWebsocketManager(logger log.Logger, funcMap map[string]*RPCFunc, wsConnOptions ...func(*wsConnection)) *WebsocketManager
NewWebsocketManager returns a new WebsocketManager that passes a map of functions, connection options and logger to new WS connections.
func (*WebsocketManager) WebsocketHandler ¶
func (wm *WebsocketManager) WebsocketHandler(w http.ResponseWriter, r *http.Request)
WebsocketHandler upgrades the request/response (via http.Hijack) and starts the wsConnection.