web

package
v0.0.0-...-7c1bd43 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func NewAPIError(status int, format string, args ...interface{}) *APIError

NewAPIError constructs new API error

func NewErrBadRequest

func NewErrBadRequest(msg string, args ...interface{}) *APIError

NewErrBadRequest returns a new bad request API error

func NewErrForbidden

func NewErrForbidden(msg string, args ...interface{}) *APIError

NewErrForbidden returns new forbidden error

func NewErrNotFound

func NewErrNotFound(msg string, args ...interface{}) *APIError

NewErrNotFound returns new not found error

func NewErrUnauthorized

func NewErrUnauthorized(msg string, args ...interface{}) *APIError

NewErrUnauthorized returns a new unauthorized API error

func ToAPIError

func ToAPIError(err error) *APIError

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.

func (APIError) Error

func (err APIError) Error() string

Error implements error interface

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

type MiddlewareFunc = func(rw http.ResponseWriter, req *http.Request) (*http.Request, error)

MiddlewareFunc is request wrapper

type ResourceHandlerFunc

type ResourceHandlerFunc = func(req *http.Request) (interface{}, error)

ResourceHandlerFunc is http.HandlerFunc extension which can return result which will be encoded to JSON or response error.

type Server

type Server struct {
	*http.Server
	Router *mux.Router
}

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 NewWrapper

func NewWrapper(log *zap.Logger) *Wrapper

NewWrapper is Wrapper constructor

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL