Documentation ¶
Overview ¶
Commons for HTTP handling
Index ¶
- Constants
- Variables
- func EventSubscriber(eventSub types.EventSubscriber) func(*wsConnection)
- func GetParam(r *http.Request, param string) string
- func GetParamByteSlice(r *http.Request, param string) ([]byte, error)
- func GetParamFloat64(r *http.Request, param string) (float64, error)
- func GetParamInt32(r *http.Request, param string) (int32, error)
- func GetParamInt64(r *http.Request, param string) (int64, error)
- func GetParamRegexp(r *http.Request, param string, re *regexp.Regexp) (string, error)
- func GetParamUint(r *http.Request, param string) (uint, error)
- func GetParamUint64(r *http.Request, param string) (uint64, error)
- func Listen(addr string, config Config) (listener net.Listener, err error)
- func NewWSConnection(baseConn *websocket.Conn, funcMap map[string]*RPCFunc, cdc *amino.Codec, ...) *wsConnection
- func PingPeriod(pingPeriod time.Duration) func(*wsConnection)
- func ReadWait(readWait time.Duration) func(*wsConnection)
- func RecoverAndLogHandler(handler http.Handler, logger log.Logger) http.Handler
- func RegisterRPCFuncs(mux *http.ServeMux, funcMap map[string]*RPCFunc, cdc *amino.Codec, ...)
- func StartHTTPAndTLSServer(listener net.Listener, handler http.Handler, certFile, keyFile string, ...) error
- func StartHTTPServer(listener net.Listener, handler http.Handler, logger log.Logger) error
- func WriteChanCapacity(cap int) func(*wsConnection)
- func WriteRPCResponseHTTP(w http.ResponseWriter, res types.RPCResponse)
- func WriteRPCResponseHTTPError(w http.ResponseWriter, httpCode int, res types.RPCResponse)
- func WriteWait(writeWait time.Duration) func(*wsConnection)
- type Config
- type RPCFunc
- type ResponseWriterWrapper
- type WebsocketManager
Constants ¶
const ( // Timeouts for reading/writing to the http connection. // Public so handlers can read them - // /broadcast_tx_commit has it's own timeout, which should // be less than the WriteTimeout here. // TODO: use a config instead. ReadTimeout = 3 * time.Second WriteTimeout = 20 * time.Second )
Variables ¶
var ( RE_INT = regexp.MustCompile(`^-?[0-9]+$`) RE_HEX = regexp.MustCompile(`^(?i)[a-f0-9]+$`) RE_EMAIL = regexp.MustCompile(`^(?i)(` + dotAtom + `)@(` + dotAtom + `)$`) RE_ADDRESS = regexp.MustCompile(`^(?i)[a-z0-9]{25,34}$`) RE_HOST = regexp.MustCompile(`^(?i)(` + domain + `)$`) )
Functions ¶
func EventSubscriber ¶
func EventSubscriber(eventSub types.EventSubscriber) func(*wsConnection)
EventSubscriber sets object that is used to subscribe / unsubscribe from events - not Goroutine-safe. If none given, default node's eventBus will be used.
func GetParamRegexp ¶
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 NewWSConnection ¶
func NewWSConnection( baseConn *websocket.Conn, funcMap map[string]*RPCFunc, cdc *amino.Codec, 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 PingPeriod ¶
PingPeriod sets the duration for sending websocket pings. 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 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, cdc *amino.Codec, logger log.Logger)
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 popualted with every RPCResponse
func StartHTTPAndTLSServer ¶
func StartHTTPAndTLSServer( listener net.Listener, handler http.Handler, certFile, keyFile string, logger log.Logger, ) 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 ¶
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 WriteChanCapacity ¶
func WriteChanCapacity(cap int) func(*wsConnection)
WriteChanCapacity sets the capacity of the websocket write channel. It should only be used in the constructor - not Goroutine-safe.
func WriteRPCResponseHTTP ¶
func WriteRPCResponseHTTP(w http.ResponseWriter, res types.RPCResponse)
func WriteRPCResponseHTTPError ¶
func WriteRPCResponseHTTPError( w http.ResponseWriter, httpCode int, res types.RPCResponse, )
Types ¶
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) 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, cdc *amino.Codec, 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 log.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.