Version: v1.5.0 Latest Latest

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

Go to latest
Published: Apr 7, 2022 License: Apache-2.0 Imports: 18 Imported by: 33




View Source
const (
	// HeaderContentType is the HTTP Content Type header.
	HeaderContentType = "Content-Type"

	// ContentTypeJSON is the Content-Type value for a JSON response.
	ContentTypeJSON = "application/json"

	// ContentTypeFormURLEncoded is the Content-Type value for a URL-encoded form.
	ContentTypeFormURLEncoded = "application/x-www-form-urlencoded"

	// ContentTypeHTMLUTF8 is the Content-Type value for UTF8-encoded HTML.
	ContentTypeHTMLUTF8 = "text/html; charset=utf-8"

	// ContentTypeProtobuf is the Content-Type value for a Protobuf message.
	ContentTypeProtobuf = "application/x-protobuf"

	// ContentTypeOctetStream is the Content-Type value for binary data.
	ContentTypeOctetStream = "application/octet-stream"


This section is empty.


func DurationToNanosBytes

func DurationToNanosBytes(r io.Reader) ([]byte, error)

DurationToNanosBytes transforms a json byte slice with Duration keys into Nanos

func DurationToNanosMap

func DurationToNanosMap(input map[string]interface{}) (map[string]interface{}, error)

DurationToNanosMap transforms keys with a Duration into Nanos

func IsClientError added in v1.0.1

func IsClientError(err error) bool

IsClientError returns true if this error would result in 4xx status code.

func NanosToDurationBytes added in v0.10.0

func NanosToDurationBytes(r io.Reader) (map[string]interface{}, error)

NanosToDurationBytes transforms a json byte slice with Nano keys into Duration keys.

func NanosToDurationMap added in v0.10.0

func NanosToDurationMap(input map[string]interface{}) (map[string]interface{}, error)

NanosToDurationMap transforms keys with Nanos into Duration.

func NewHTTPClient added in v0.13.0

func NewHTTPClient(o HTTPClientOptions) *http.Client

NewHTTPClient constructs a new HTTP Client.

func WriteError added in v1.0.0

func WriteError(w http.ResponseWriter, err error, opts ...WriteErrorOption)

WriteError will serve an HTTP error.

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, data interface{}, logger *zap.Logger)

WriteJSONResponse writes generic data to the ResponseWriter

func WriteProtoMsgJSONResponse

func WriteProtoMsgJSONResponse(w http.ResponseWriter, data proto.Message, logger *zap.Logger)

WriteProtoMsgJSONResponse writes a protobuf message to the ResponseWriter. This uses jsonpb for json marshalling, which encodes fields with default values, even with the omitempty tag.

func WriteUninitializedResponse

func WriteUninitializedResponse(w http.ResponseWriter, logger *zap.Logger)

WriteUninitializedResponse writes a protobuf message to the ResponseWriter


type Error

type Error interface {
	// Fulfill error interface.

	// Embedding ContainedError allows for the inner error
	// to be retrieved with all existing error helpers.

	// Code returns the status code to return to end users.
	Code() int

Error is an HTTP JSON error that also sets a return status code.

func NewError added in v1.0.0

func NewError(err error, status int) Error

NewError creates a new error with an explicit status code which will override any wrapped error to return specifically the exact error code desired.

type ErrorResponse added in v0.7.0

type ErrorResponse struct {
	Status string `json:"status"`
	Error  string `json:"error"`

ErrorResponse is a generic response for an HTTP error.

type ErrorRewriteFn added in v1.0.1

type ErrorRewriteFn func(error) error

ErrorRewriteFn is a function for rewriting response error.

func SetErrorRewriteFn added in v1.0.1

func SetErrorRewriteFn(f ErrorRewriteFn) ErrorRewriteFn

SetErrorRewriteFn sets error rewrite function.

type HTTPClientOptions added in v0.13.0

type HTTPClientOptions struct {
	RequestTimeout     time.Duration `yaml:"requestTimeout"`
	ConnectTimeout     time.Duration `yaml:"connectTimeout"`
	KeepAlive          time.Duration `yaml:"keepAlive"`
	IdleConnTimeout    time.Duration `yaml:"idleConnTimeout"`
	MaxIdleConns       int           `yaml:"maxIdleConns"`
	DisableCompression bool          `yaml:"disableCompression"`
	Proxy              ProxyFunc     `yaml:"proxy"`

HTTPClientOptions specify HTTP Client options.

func DefaultHTTPClientOptions added in v0.13.0

func DefaultHTTPClientOptions() HTTPClientOptions

DefaultHTTPClientOptions returns default options.

type ProxyFunc added in v1.0.1

type ProxyFunc func(*http.Request) (*url.URL, error)

ProxyFunc is a type alias for the proxy function used by the standard library's http.Transport struct.

type WriteErrorOption added in v1.0.0

type WriteErrorOption func(*options)

WriteErrorOption is an option to pass to WriteError.

func WithErrorResponse added in v1.0.0

func WithErrorResponse(b []byte) WriteErrorOption

WithErrorResponse specifies a response to add the WriteError method.


Path Synopsis
Package cors handles cross-origin HTTP requests (CORS).
Package cors handles cross-origin HTTP requests (CORS).

Jump to

Keyboard shortcuts

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