http

package
v2.0.0-beta.8 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2020 License: MIT Imports: 15 Imported by: 2

Documentation

Index

Constants

View Source
const PlatformErrorCodeHeader = "X-Platform-Error-Code"

PlatformErrorCodeHeader shows the error code of platform error.

Variables

This section is empty.

Functions

func SetCORS

func SetCORS(next http.Handler) http.Handler

func SkipOptions

func SkipOptions(next http.Handler) http.Handler

func UserAgent

func UserAgent(r *http.Request) string

Types

type API

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

API provides a consolidated means for handling API interface concerns. Concerns such as decoding/encoding request and response bodies as well as adding headers for content type and content encoding.

func NewAPI

func NewAPI(opts ...APIOptFn) *API

NewAPI creates a new API type.

func (*API) DecodeGob

func (a *API) DecodeGob(r io.Reader, v interface{}) error

DecodeGob decodes reader with gob.

func (*API) DecodeJSON

func (a *API) DecodeJSON(r io.Reader, v interface{}) error

DecodeJSON decodes reader with json.

func (*API) Err

func (a *API) Err(w http.ResponseWriter, err error)

Err is used for writing an error to the response.

func (*API) Respond

func (a *API) Respond(w http.ResponseWriter, status int, v interface{})

Respond writes to the response writer, handling all errors in writing.

type APIOptFn

type APIOptFn func(*API)

APIOptFn is a functional option for setting fields on the API type.

func WithEncodeGZIP

func WithEncodeGZIP() APIOptFn

WithEncodeGZIP sets the encoder to gzip contents.

func WithErrFn

func WithErrFn(fn func(err error) (interface{}, int, error)) APIOptFn

WithErrFn sets the err handling func for issues when writing to the response body.

func WithLog

func WithLog(logger *zap.Logger) APIOptFn

WithLog sets the logger.

func WithOKErrFn

func WithOKErrFn(fn func(err error) error) APIOptFn

WithOKErrFn is an error handler for failing validation for request bodies.

func WithPrettyJSON

func WithPrettyJSON(b bool) APIOptFn

WithPrettyJSON sets the json encoder to marshal indent or not.

func WithUnmarshalErrFn

func WithUnmarshalErrFn(fn func(encoding string, err error) error) APIOptFn

WithUnmarshalErrFn sets the error handler for errors that occur when unmarshaling the request body.

type ErrBody

type ErrBody struct {
	Code string `json:"code"`
	Msg  string `json:"message"`
}

ErrBody is an err response body.

type ErrorHandler

type ErrorHandler int

ErrorHandler is the error handler in http package.

func (ErrorHandler) HandleHTTPError

func (h ErrorHandler) HandleHTTPError(ctx context.Context, err error, w http.ResponseWriter)

HandleHTTPError encodes err with the appropriate status code and format, sets the X-Platform-Error-Code headers on the response. We're no longer using X-Influx-Error and X-Influx-Reference. and sets the response status to the corresponding status code.

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware constructor.

func Metrics

func Metrics(name string, reqMetric *prometheus.CounterVec, durMetric *prometheus.HistogramVec) Middleware

func Trace

func Trace(name string) Middleware

type ResourceHandler

type ResourceHandler interface {
	Prefix() string
	http.Handler
}

ResourceHandler is an HTTP handler for a resource. The prefix describes the url path prefix that relates to the handler endpoints.

type StatusResponseWriter

type StatusResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func NewStatusResponseWriter

func NewStatusResponseWriter(w http.ResponseWriter) *StatusResponseWriter

func (*StatusResponseWriter) Code

func (w *StatusResponseWriter) Code() int

func (*StatusResponseWriter) ResponseBytes

func (w *StatusResponseWriter) ResponseBytes() int

func (*StatusResponseWriter) StatusCodeClass

func (w *StatusResponseWriter) StatusCodeClass() string

func (*StatusResponseWriter) Write

func (w *StatusResponseWriter) Write(b []byte) (int, error)

func (*StatusResponseWriter) WriteHeader

func (w *StatusResponseWriter) WriteHeader(statusCode int)

WriteHeader writes the header and captures the status code.

Jump to

Keyboard shortcuts

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