httputil

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrUnauthorized represents failure when authenticating a request.
	ErrUnauthorized = AuthError("Unauthorized")

	// ErrForbidden represents failure when authorizing a request.
	ErrForbidden = AuthError("Forbidden")

	// ErrBasicAuthenticate is designed to trigger an HTTP Basic Auth challenge.
	ErrBasicAuthenticate = AuthError("ErrWWWAuthenticate")
)
View Source
const InternalErrorFormat = "Internal Server Error: %d"

InternalErrorFormat is the default error message returned for unhandled errors.

Variables

View Source
var (
	// ContentType header value.
	ContentType = "Content-Type"
	// ApplicationJSON content-type.
	ApplicationJSON = "application/json"
	// TextHTML content-type.
	TextHTML = "text/html"
)

Functions

func DumpBody added in v0.0.4

func DumpBody(req *http.Request) ([]byte, error)

func DumpHeader added in v0.0.4

func DumpHeader(req *http.Request) map[string]string

func ErrorHandler

func ErrorHandler(w http.ResponseWriter, r *http.Request, err error)

ErrorHandler provides some standard handling for errors in an http request flow.

Maps: json.SyntaxError to "BadRequest", body is the JSON string of the error message. validation.Errors to "BadRequest", body is the JSON of the error object (map of field name to list of errors). AuthError to "Forbidden" or "Unauthorized" as defined by the err instance. In addition ErrBasicAuthenticate issues a basic auth challenge using default realm of "Restricted". To override handle in your custom error handlers instead.

Unhandled errors are added to the ctx and return "Internal Server Error" with the request ID to aid with troubleshooting.

func GetID

func GetID(ctx context.Context) int64

GetID returns the request ID logged to the context, otherwise 0.

func GetOp

func GetOp(ctx context.Context) string

GetOp returns the operation label logged to the context, otherwise "".

func JSONWrite

func JSONWrite(w http.ResponseWriter, code int, obj interface{}) error

JSONWrite is a simple helper utility to return the json encoded obj with appropriate content-type and code.

func RapiDoc added in v0.0.6

func RapiDoc(opts RapiDocOpts) func(http.Handler) http.Handler

func SetID

func SetID(ctx context.Context, id int64) context.Context

SetID sets the request ID logged to the context.

func SetOp

func SetOp(ctx context.Context, op string) context.Context

SetOp stores a label for the current request. Useful for developer friendly log messages.

Types

type AuthError

type AuthError string

AuthError encompasses Authentication and Authorization errors.

func (AuthError) Error

func (e AuthError) Error() string

type ContextKey

type ContextKey string

type ErrorHandlerFunc

type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)

ErrorHandlerFunc is useful to standardize the exception management of requests.

type RapiDocOpts added in v0.0.6

type RapiDocOpts struct {
	// BasePath for the UI path, defaults to: /
	BasePath string
	// Path combines with BasePath for the full UI path, defaults to: docs
	Path string
	// SpecURL the url to find the spec for
	SpecURL string
	// RapiDocURL for the js that generates the rapidoc site, defaults to: https://unpkg.com/rapidoc/dist/rapidoc-min.js
	RapiDocURL string
	// Title for the documentation site, default to: API documentation
	Title string
}

RapiDocOpts configures the RapiDoc middlewares.

func (*RapiDocOpts) Defaults added in v0.0.7

func (r *RapiDocOpts) Defaults()

Defaults for all options.

type ResponseWrapper added in v0.0.5

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

func NewWrapResponse added in v0.0.5

func NewWrapResponse(w http.ResponseWriter) *ResponseWrapper

NewWrapResponse wraps http.ResponseWriter and tracks status and bytes written.

func (*ResponseWrapper) Bytes added in v0.0.5

func (rw *ResponseWrapper) Bytes() int

func (*ResponseWrapper) Status added in v0.0.5

func (rw *ResponseWrapper) Status() int

func (*ResponseWrapper) Write added in v0.0.5

func (rw *ResponseWrapper) Write(buf []byte) (int, error)

func (*ResponseWrapper) WriteHeader added in v0.0.5

func (rw *ResponseWrapper) WriteHeader(code int)

Jump to

Keyboard shortcuts

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