httphelper

package
v0.0.0-...-8488f5b Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2016 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CORSAllowAll = &cors.Options{
	AllowAllOrigins:  true,
	AllowMethods:     []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"},
	AllowHeaders:     []string{"Authorization", "Accept", "Content-Type", "If-Match", "If-None-Match"},
	ExposeHeaders:    []string{"ETag", "Content-Disposition"},
	AllowCredentials: true,
	MaxAge:           time.Hour,
}

Functions

func ConflictError

func ConflictError(w http.ResponseWriter, message string)

func ContextInjector

func ContextInjector(componentName string, handler http.Handler) http.Handler

func DecodeJSON

func DecodeJSON(req *http.Request, i interface{}) error

func Error

func Error(w http.ResponseWriter, err error)

func IsObjectExistsError

func IsObjectExistsError(err error) bool

func IsObjectNotFoundError

func IsObjectNotFoundError(err error) bool

func IsPreconditionFailedError

func IsPreconditionFailedError(err error) bool

func IsRetryableError

func IsRetryableError(err error) bool

IsRetryableError indicates whether a HTTP request can be safely retried.

func IsValidationError

func IsValidationError(err error) bool

func JSON

func JSON(w http.ResponseWriter, status int, v interface{})

func NewRequestLogger

func NewRequestLogger(handler http.Handler) http.Handler

func NewRequestLoggerCustom

func NewRequestLoggerCustom(handler http.Handler, loggerFn RequestLoggerFn) http.Handler

func ObjectExistsErr

func ObjectExistsErr(message string) error

func ObjectExistsError

func ObjectExistsError(w http.ResponseWriter, message string)

func ObjectNotFoundError

func ObjectNotFoundError(w http.ResponseWriter, message string)

func PreconditionFailedErr

func PreconditionFailedErr(message string) error

func ServiceUnavailableError

func ServiceUnavailableError(w http.ResponseWriter, message string)

func ValidationError

func ValidationError(w http.ResponseWriter, field, message string)

func WrapHandler

func WrapHandler(handler HandlerFunc) httprouter.Handle

Types

type ErrorCode

type ErrorCode string
const (
	NotFoundErrorCode           ErrorCode = "not_found"
	ObjectNotFoundErrorCode     ErrorCode = "object_not_found"
	ObjectExistsErrorCode       ErrorCode = "object_exists"
	ConflictErrorCode           ErrorCode = "conflict"
	SyntaxErrorCode             ErrorCode = "syntax_error"
	ValidationErrorCode         ErrorCode = "validation_error"
	PreconditionFailedErrorCode ErrorCode = "precondition_failed"
	UnauthorizedErrorCode       ErrorCode = "unauthorized"
	UnknownErrorCode            ErrorCode = "unknown_error"
	RatelimitedErrorCode        ErrorCode = "ratelimited"
	ServiceUnavailableErrorCode ErrorCode = "service_unavailable"
)

type FlushWriter

type FlushWriter struct {
	io.Writer
	Enabled bool
}

func (FlushWriter) Write

func (f FlushWriter) Write(p []byte) (int, error)

type Handler

type Handler interface {
	ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request)
}

Handler is an extended version of http.Handler that also takes a context argument ctx.

type HandlerFunc

type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request)

The HandlerFunc type is an adapter to allow the use of ordinary functions as Handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.

func (HandlerFunc) ServeHTTP

func (f HandlerFunc) ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request)

ServeHTTP calls f(ctx, w, r).

type JSONError

type JSONError struct {
	Code    ErrorCode       `json:"code"`
	Message string          `json:"message"`
	Detail  json.RawMessage `json:"detail,omitempty"`
	Retry   bool            `json:"retry"`
}

func (JSONError) Error

func (jsonError JSONError) Error() string

type RequestLoggerFn

type RequestLoggerFn func(handler http.Handler, logger log.Logger, clientIP string, rw *ResponseWriter, req *http.Request)

type ResponseWriter

type ResponseWriter struct {
	// contains filtered or unexported fields
}

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter, ctx context.Context) *ResponseWriter

func (*ResponseWriter) CloseNotify

func (r *ResponseWriter) CloseNotify() <-chan bool

func (*ResponseWriter) Context

func (r *ResponseWriter) Context() context.Context

func (*ResponseWriter) Flush

func (r *ResponseWriter) Flush()

func (*ResponseWriter) Header

func (r *ResponseWriter) Header() http.Header

func (*ResponseWriter) Hijack

func (r *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*ResponseWriter) Status

func (r *ResponseWriter) Status() int

func (*ResponseWriter) Write

func (r *ResponseWriter) Write(b []byte) (int, error)

func (*ResponseWriter) WriteHeader

func (r *ResponseWriter) WriteHeader(s int)

func (*ResponseWriter) Written

func (r *ResponseWriter) Written() bool

Jump to

Keyboard shortcuts

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