xhttp

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2020 License: ISC Imports: 12 Imported by: 0

README

xhttp

This package provides utilities for HTTP servers and clients.

Item Description
xhttp.Error An error type capturing context and information about a failed http request.
xhttp.ResponseWriter An implementation of standard xhttp.ResponseWriter for recording status code.
xhttp.ClientMiddleware A client-side middleware providing wrappers for logging, metrics, tracing, etc.
xhttp.ServerMiddleware A server-side middleware providing wrappers for http handlers for logging, metrics, tracing, etc.

Quick Start

You can see an example of using the server and client middleware here.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextForTest added in v0.2.2

func ContextForTest(ctx context.Context) context.Context

ContextForTest takes in a request context and inserts a RequestID as well as a new Void Logger. For use in tests only, to test functions which expect a logger and RequestID to have been added by the middleware.

Types

type ClientMiddleware

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

ClientMiddleware is an http client middleware for logging, metrics, tracing, etc.

func NewClientMiddleware

func NewClientMiddleware(opts ...ClientMiddlewareOption) *ClientMiddleware

NewClientMiddleware creates a new instance of http client middleware.

func (*ClientMiddleware) Logging

func (m *ClientMiddleware) Logging(next Doer) Doer

Logging takes care of logging for outgoing http requests. Request id will be read from reqeust context if present.

func (*ClientMiddleware) Metrics

func (m *ClientMiddleware) Metrics(next Doer) Doer

Metrics takes care of metrics for outgoing http requests.

func (*ClientMiddleware) RequestID

func (m *ClientMiddleware) RequestID(next Doer) Doer

RequestID ensures outgoing requests have unique ids. This middleware ensures the request headers and context have a unique id. A new request id will be generated if needed.

func (*ClientMiddleware) Tracing

func (m *ClientMiddleware) Tracing(next Doer) Doer

Tracing takes care of tracing for outgoing http requests. Trace information will be read from reqeust context if present.

type ClientMiddlewareOption added in v0.2.2

type ClientMiddlewareOption func(*ClientMiddleware)

ClientMiddlewareOption sets optional parameters for client middleware.

func ClientLogging added in v0.2.2

func ClientLogging(logger *log.Logger) ClientMiddlewareOption

ClientLogging is the option for client middleware to enable logging for every request.

func ClientMetrics added in v0.2.2

func ClientMetrics(mf *metrics.Factory) ClientMiddlewareOption

ClientMetrics is the option for client middleware to enable metrics for every request.

func ClientTracing added in v0.2.2

func ClientTracing(tracer opentracing.Tracer) ClientMiddlewareOption

ClientTracing is the option for client middleware to enable tracing for every request.

type Doer

type Doer func(*http.Request) (*http.Response, error)

Doer is the interface for standard http.Client Do method.

type Error

type Error struct {
	Request    *http.Request
	StatusCode int
	Message    string
}

Error is an http error.

func NewError

func NewError(res *http.Response) *Error

NewError creates a new instance of Error.

func (*Error) Error

func (e *Error) Error() string

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	StatusCode  int
	StatusClass string
}

ResponseWriter extends the functionality of standard http.ResponseWriter.

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) *ResponseWriter

NewResponseWriter creates a new response writer.

func (*ResponseWriter) WriteHeader

func (r *ResponseWriter) WriteHeader(statusCode int)

WriteHeader overrides the default implementation of http.WriteHeader.

type ServerMiddleware

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

ServerMiddleware is an http server middleware for logging, metrics, tracing, etc.

func NewServerMiddleware

func NewServerMiddleware(opts ...ServerMiddlewareOption) *ServerMiddleware

NewServerMiddleware creates a new instance of http server middleware.

func (*ServerMiddleware) Logging

Logging takes care of logging for incoming http requests. Request id will be read from reqeust headers if present.

func (*ServerMiddleware) Metrics

Metrics takes care of metrics for incoming http requests.

func (*ServerMiddleware) RequestID

func (m *ServerMiddleware) RequestID(next http.HandlerFunc) http.HandlerFunc

RequestID ensures incoming requests have unique ids. This middleware ensures the request headers and context have a unique id. A new request id will be generated if needed.

func (*ServerMiddleware) Tracing

Tracing takes care of tracing for incoming http requests. Trace information will be read from reqeust headers if present.

type ServerMiddlewareOption added in v0.2.2

type ServerMiddlewareOption func(*ServerMiddleware)

ServerMiddlewareOption sets optional parameters for server middleware.

func ServerLogging added in v0.2.2

func ServerLogging(logger *log.Logger) ServerMiddlewareOption

ServerLogging is the option for server middleware to enable logging for every request.

func ServerMetrics added in v0.2.2

func ServerMetrics(mf *metrics.Factory) ServerMiddlewareOption

ServerMetrics is the option for server middleware to enable metrics for every request.

func ServerTracing added in v0.2.2

func ServerTracing(tracer opentracing.Tracer) ServerMiddlewareOption

ServerTracing is the option for server middleware to enable tracing for every request.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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