httpserverutils

package
v0.2.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2020 License: ISC Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRequestMetadataMiddleware

func AddRequestMetadataMiddleware(next http.Handler) http.Handler

AddRequestMetadataMiddleware is a middleware that adds some metadata to the context of every request.

func LoggingMiddleware

func LoggingMiddleware(next http.Handler) http.Handler

LoggingMiddleware is a middleware that writes logs for every request.

func MakeHandler

func MakeHandler(handler HandlerFunc) func(http.ResponseWriter, *http.Request)

MakeHandler is a wrapper function that takes a handler in the form of HandlerFunc and returns a function that can be used as a handler in mux.Router.HandleFunc.

func NewErrorFromDBErrors

func NewErrorFromDBErrors(prefix string, dbErrors []error) error

NewErrorFromDBErrors takes a slice of database errors and a prefix, and returns an error with all of the database errors formatted to one string with the given prefix

func NewHandlerError

func NewHandlerError(code int, err error) error

NewHandlerError returns a HandlerError with the given code and message.

func NewHandlerErrorWithCustomClientMessage

func NewHandlerErrorWithCustomClientMessage(code int, err error, clientMessage string) error

NewHandlerErrorWithCustomClientMessage returns a HandlerError with the given code, message and client error message.

func NewInternalServerHandlerError

func NewInternalServerHandlerError(err error) error

NewInternalServerHandlerError returns a HandlerError with the given message, and the http.StatusInternalServerError status text as client message.

func RecoveryMiddleware

func RecoveryMiddleware(h http.Handler) http.Handler

RecoveryMiddleware is a middleware that recovers from panics, log it, and sends Internal Server Error to the client.

func SendErr

func SendErr(ctx *ServerContext, w http.ResponseWriter, err error)

SendErr takes a HandlerError and create a ClientError out of it that is sent to the http client.

func SendJSONResponse

func SendJSONResponse(w http.ResponseWriter, response interface{})

SendJSONResponse encodes the given response to JSON format and sends it to the client

func SetJSONMiddleware

func SetJSONMiddleware(h http.Handler) http.Handler

SetJSONMiddleware is a middleware that sets the content type of every request to be application/json.

Types

type ClientError

type ClientError struct {
	ErrorCode    int    `json:"errorCode"`
	ErrorMessage string `json:"errorMessage"`
}

ClientError is the http response that is sent to the client in case of an error.

func (*ClientError) Error

func (err *ClientError) Error() string

type HandlerError

type HandlerError struct {
	Code          int
	Cause         error
	ClientMessage string
}

HandlerError is an error returned from a rest route handler or a middleware.

func (*HandlerError) Error

func (hErr *HandlerError) Error() string

type HandlerFunc

type HandlerFunc func(ctx *ServerContext, r *http.Request, routeParams map[string]string, queryParams map[string]string, requestBody []byte) (
	interface{}, error)

HandlerFunc is a handler function that is passed to the MakeHandler wrapper and gets the relevant request fields from it.

type ServerContext

type ServerContext struct {
	context.Context
}

ServerContext is a context.Context wrapper that enables custom logs with request ID.

func ToServerContext

func ToServerContext(ctx context.Context) *ServerContext

ToServerContext takes a context.Context instance and converts it to *ServerContext.

func (*ServerContext) Criticalf

func (ctx *ServerContext) Criticalf(format string, params ...interface{})

Criticalf writes a customized formatted context related log with log level 'Critical'.

func (*ServerContext) Debugf

func (ctx *ServerContext) Debugf(format string, params ...interface{})

Debugf writes a customized formatted context related log with log level 'Debug'.

func (*ServerContext) Errorf

func (ctx *ServerContext) Errorf(format string, params ...interface{})

Errorf writes a customized formatted context related log with log level 'Error'.

func (*ServerContext) Infof

func (ctx *ServerContext) Infof(format string, params ...interface{})

Infof writes a customized formatted context related log with log level 'Info'.

func (*ServerContext) SetRequestID

func (ctx *ServerContext) SetRequestID(requestID uint64) context.Context

SetRequestID associates a request ID for the context.

func (*ServerContext) Tracef

func (ctx *ServerContext) Tracef(format string, params ...interface{})

Tracef writes a customized formatted context related log with log level 'Trace'.

func (*ServerContext) Warnf

func (ctx *ServerContext) Warnf(format string, params ...interface{})

Warnf writes a customized formatted context related log with log level 'Warn'.

Jump to

Keyboard shortcuts

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