Documentation
¶
Overview ¶
Commons for HTTP handling
Index ¶
- func GetParam(r *http.Request, param string) string
- func Listen(addr string, config *Config) (listener net.Listener, err error)
- func MaxBatchRequests(maxBatchRequests int) func(*wsConnection)
- func NewWSConnection(baseConn *websocket.Conn, funcMap map[string]*RPCFunc, ...) *wsConnection
- func OnDisconnect(onDisconnect func(remoteAddr string)) func(*wsConnection)
- func ReadLimit(readLimit int64) func(*wsConnection)
- func RecoverAndLogHandler(handler http.Handler, logger *slog.Logger) http.Handler
- func RegisterRPCFuncs(mux *http.ServeMux, funcMap map[string]*RPCFunc, logger *slog.Logger, ...)
- func StartHTTPAndTLSServer(listener net.Listener, handler http.Handler, certFile, keyFile string, ...) error
- func StartHTTPServer(listener net.Listener, handler http.Handler, logger *slog.Logger, ...) error
- func WriteRPCResponseHTTP(w http.ResponseWriter, res types.RPCResponse) error
- func WriteRPCResponseHTTPError(w http.ResponseWriter, httpCode int, res types.RPCResponse) error
- func WriteStreamingRPCResponseHTTP(r *http.Request, w http.ResponseWriter, id types.JSONRPCID, ...) error
- func WriteWait(writeWait time.Duration) func(*wsConnection)
- type Config
- type RPCFunc
- type ResponseWriterWrapper
- type WebsocketManager
Constants ¶
This section is empty.
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 MaxBatchRequests ¶
func MaxBatchRequests(maxBatchRequests int) func(*wsConnection)
MaxBatchRequests sets the maximum number of requests in one JSON-RPC batch; 0 admits any batch. It should only be used in the constructor - not Goroutine-safe.
func NewWSConnection ¶
func NewWSConnection( baseConn *websocket.Conn, funcMap map[string]*RPCFunc, options ...func(*wsConnection), ) *wsConnection
NewWSConnection wraps websocket.Conn.
See the commentary on the func(*wsConnection) functions for a detailed description of how to configure ping period and pong wait time. NOTE: if the write buffer is full, pongs may be dropped, which may cause clients to disconnect. see https://github.com/gorilla/websocket/issues/97
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 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 RecoverAndLogHandler ¶
RecoverAndLogHandler wraps an HTTP handler, adding error logging. If the inner function panics, the outer function recovers, logs, sends an HTTP 500 error response.
func RegisterRPCFuncs ¶
func RegisterRPCFuncs(mux *http.ServeMux, funcMap map[string]*RPCFunc, logger *slog.Logger, maxBatchRequests int)
RegisterRPCFuncs adds a route for each function in the funcMap, as well as general jsonrpc and websocket handlers for all functions. "result" is the interface on which the result objects are registered, and is populated with every RPCResponse A JSON-RPC batch of more than maxBatchRequests requests is refused whole; 0 admits any batch.
func StartHTTPAndTLSServer ¶
func StartHTTPAndTLSServer( listener net.Listener, handler http.Handler, certFile, keyFile string, logger *slog.Logger, config *Config, ) error
StartHTTPAndTLSServer takes a listener and starts an HTTPS server with the given handler. It wraps handler with RecoverAndLogHandler. NOTE: This function blocks - you may want to call it in a go-routine.
func StartHTTPServer ¶
func StartHTTPServer(listener net.Listener, handler http.Handler, logger *slog.Logger, config *Config) error
StartHTTPServer takes a listener and starts an HTTP server with the given handler. It wraps handler with RecoverAndLogHandler. NOTE: This function blocks - you may want to call it in a go-routine.
func WriteRPCResponseHTTP ¶
func WriteRPCResponseHTTP(w http.ResponseWriter, res types.RPCResponse) error
func WriteRPCResponseHTTPError ¶
func WriteRPCResponseHTTPError( w http.ResponseWriter, httpCode int, res types.RPCResponse, ) error
func WriteStreamingRPCResponseHTTP ¶
func WriteStreamingRPCResponseHTTP(r *http.Request, w http.ResponseWriter, id types.JSONRPCID, result types.StreamableResult) error
WriteStreamingRPCResponseHTTP writes a successful JSON-RPC response whose result body is produced incrementally by result.StreamJSON. The envelope (jsonrpc / id) is written around the streamed body without buffering the whole result in memory. Returns the first write error encountered (no panics), mirroring the contract of WriteRPCResponseHTTP.
r's context is forwarded to result.StreamJSON so that long streams can be aborted when the client disconnects.
A HEAD request gets the status and headers only: net/http discards a HEAD body without sending it, so nothing would bound the work of streaming it.
Types ¶
type Config ¶
type Config struct {
// see netutil.LimitListener
MaxOpenConnections int
// mirrors http.Server#ReadTimeout
ReadTimeout time.Duration
// mirrors http.Server#WriteTimeout
WriteTimeout time.Duration
// mirrors http.Server#IdleTimeout: zero falls back to ReadTimeout,
// per net/http.
IdleTimeout time.Duration
// MaxBodyBytes controls the maximum number of bytes the
// server will read parsing the request body.
MaxBodyBytes int64
// mirrors 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 ¶
NewRPCFunc wraps a function for introspection. f is the function, args are comma separated argument names
func NewWSRPCFunc ¶
NewWSRPCFunc wraps a function for introspection and use in the websockets.
type ResponseWriterWrapper ¶
type ResponseWriterWrapper struct {
Status int
http.ResponseWriter
}
Remember the status for logging
func (*ResponseWriterWrapper) Hijack ¶
func (w *ResponseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error)
implements http.Hijacker
func (*ResponseWriterWrapper) Unwrap ¶
func (w *ResponseWriterWrapper) Unwrap() http.ResponseWriter
Unwrap lets http.ResponseController reach the wrapped writer, so a batch can flush each answer.
func (*ResponseWriterWrapper) WriteHeader ¶
func (w *ResponseWriterWrapper) WriteHeader(status int)
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(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) SetLogger ¶
func (wm *WebsocketManager) SetLogger(l *slog.Logger)
SetLogger sets the logger.
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.