Documentation
¶
Index ¶
- Variables
- type APIError
- func NewAPIError(status int, format string, args ...interface{}) *APIError
- func NewErrBadRequest(msg string, args ...interface{}) *APIError
- func NewErrForbidden(msg string, args ...interface{}) *APIError
- func NewErrNotFound(msg string, args ...interface{}) *APIError
- func NewErrUnauthorized(msg string, args ...interface{}) *APIError
- func ToAPIError(err error) *APIError
- func UnmarshalJSON(r io.ReadCloser, out interface{}) *APIError
- type APIErrorer
- type ErrorResponse
- type HandlerFunc
- type ListenParams
- type MiddlewareFunc
- type ResourceHandlerFunc
- type Server
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
var ErrNotImplemented = NewAPIError(http.StatusNotImplemented, "method not implemented")
Functions ¶
This section is empty.
Types ¶
type APIError ¶
type APIError struct { // Status is HTTP status code Status int `json:"-"` // Message is error message Message string `json:"message"` // Data is optional error data Data interface{} `json:"data,omitempty"` }
APIError is HTTP error returned from API
func NewAPIError ¶
NewAPIError constructs new API error
func NewErrBadRequest ¶
NewErrBadRequest returns a new bad request API error
func NewErrForbidden ¶
NewErrForbidden returns new forbidden error
func NewErrNotFound ¶
NewErrNotFound returns new not found error
func NewErrUnauthorized ¶
NewErrUnauthorized returns a new unauthorized API error
func ToAPIError ¶
ToAPIError constructs APIError from passed error.
If error implements APIErrorer interface, APIError() method will be called.
func UnmarshalJSON ¶
func UnmarshalJSON(r io.ReadCloser, out interface{}) *APIError
UnmarshalJSON unmarshal request payload to destination value.
Returns API error on failure.
type APIErrorer ¶
type APIErrorer interface { // APIError returns api error response APIError() *APIError }
APIErrorer provides and APIError representation of error.
Can be used to implement custom error response.
type ErrorResponse ¶
type ErrorResponse struct { // Error contains server error Error *APIError `json:"error"` }
ErrorResponse is server error response
type HandlerFunc ¶
type HandlerFunc = func(rw http.ResponseWriter, req *http.Request) error
HandlerFunc is http.HandlerFunc extension which can return error.
type ListenParams ¶
type ListenParams struct { // Address is server listen address (socket) Address string // ReadTimeout is request read timeout ReadTimeout time.Duration // WriteTimeout is response write timeout WriteTimeout time.Duration // LimitExpirationTTL is token bucket ttl LimitExpirationTTL time.Duration // ClientRPSQuota is request per second quota for each client (by IP) ClientRPSQuota float64 }
type MiddlewareFunc ¶
MiddlewareFunc is request wrapper
type ResourceHandlerFunc ¶
ResourceHandlerFunc is http.HandlerFunc extension which can return result which will be encoded to JSON or response error.
type Server ¶
func NewServer ¶
func NewServer(p ListenParams) *Server
NewServer constructs new HTTP server with specified params
type Wrapper ¶
type Wrapper struct {
// contains filtered or unexported fields
}
Wrapper is http handler wrapper and composer.
func (Wrapper) MiddlewareFunc ¶
func (w Wrapper) MiddlewareFunc(fn MiddlewareFunc) mux.MiddlewareFunc
MiddlewareFunc wraps web's middleware onto mux.MiddlewareFunc.
func (Wrapper) WrapHandler ¶
func (w Wrapper) WrapHandler(handler HandlerFunc, wrappers ...MiddlewareFunc) http.HandlerFunc
WrapHandler wraps web's HandlerFunc onto http.HandlerFunc.
Accepts optional list of middleware functions to be called before handler.
Examples:
// one handler web.WrapHandler(myHandler) // handler with multiple middlewares web.WrapHandler(getUserData, RequireCORS, RequireAuth)
func (Wrapper) WrapResourceHandler ¶
func (w Wrapper) WrapResourceHandler(h ResourceHandlerFunc, mw ...MiddlewareFunc) http.HandlerFunc
WrapResourceHandler wraps resource handler onto http.HandlerFunc. Use *web.APIError or implement web.APIErrorer to return custom error.
Accepts optional list of middleware functions to be called before handler.
See: WrapHandler