httpserver

package
v2.92.0 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 10 Imported by: 22

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrHandler

func ErrHandler(ctx context.Context, statusCode int, err error)

ErrHandler is an ErrorHandler that creates a log in the provided context's svc1log logger when an error is received. The log output is printed at the ERROR level if the status code is >= 500; otherwise, it is printed at INFO level. This preserves request-scoped logging configuration added by wrouter.

func NewJSONHandler

func NewJSONHandler(fn func(http.ResponseWriter, *http.Request) error, statusFn StatusMapper, errorFn ErrorHandler) http.Handler

NewJSONHandler returns a http.Handler which will convert a returned error into a corresponding status code, and handle the error according to the provided ErrorHandler. The provided 'fn' function is not expected to write a response in the http.ResponseWriter if it returns a non-nil error. If a non-nil error is returned, the mapped status code from the provided StatusMapper will be returned.

func ParseBearerTokenHeader

func ParseBearerTokenHeader(req *http.Request) (string, error)

ParseBearerTokenHeader parses a bearer token value out of the Authorization header. It expects a header with a key of 'Authorization' and a value of 'bearer {token}'. ParseBearerTokenHeader will return the token value, or an error if the Authorization header is missing, an empty string, or is not in the format expected.

func SecretStringEqual added in v2.31.0

func SecretStringEqual(a, b string) bool

SecretStringEqual will compare two strings and return true if they are equal. The time taken for the comparison does not depend on the string contents.

func StatusCodeMapper

func StatusCodeMapper(err error) int

StatusCodeMapper maps a provided error to an HTTP status code. If the error's RootCause is a conjure error, the status mapping to the errorCode field is used. If the provided error is a contains the legacy httpStatusCode parameter, that value is used. Otherwise, returns http.StatusInternalServerError (500).

func WriteJSONResponse

func WriteJSONResponse(w http.ResponseWriter, obj interface{}, status int)

WriteJSONResponse marshals the provided object to JSON using a JSON encoder with SetEscapeHTML(false) and writes the resulting JSON as a JSON response to the provided http.ResponseWriter with the provided status code. If marshaling the provided object as JSON results in an error, writes a 500 response with the text content of the error.

Types

type ErrorHandler

type ErrorHandler func(ctx context.Context, statusCode int, err error)

type StatusMapper

type StatusMapper func(err error) int

Jump to

Keyboard shortcuts

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