nelly

package module
v0.0.0-...-e6322aa Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2020 License: MIT Imports: 20 Imported by: 0

README

nelly

Build Status GoDoc Go Report Card Coverage

A high performance and modern HTTP Middleware for Golang (inspired by Kubernetes API server)

nelly

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DroppedRequests = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "nelly_dropped_requests_total",
			Help: "Number of requests dropped with 'Try again later' response",
		},
		[]string{"requestKind"},
	)
)

Functions

func DefaultStacktracePred

func DefaultStacktracePred(status int) bool

func LogOf

func LogOf(req *http.Request, w http.ResponseWriter) logger

LogOf returns the logger hiding in w. If there is not an existing logger then a passthroughLogger will be created which will log to stdout immediately when Addf is called.

func Unlogged

Unlogged returns the original ResponseWriter, or w if it is not our inserted logger.

func WithTimeout

func WithTimeout(h httprouter.Handle, timeoutFunc timeoutFunc) httprouter.Handle

WithTimeout returns an http.Handler that runs h with a timeout determined by timeoutFunc. The new http.Handler calls h.ServeHTTP to handle each request, but if a call runs for longer than its time limit, the handler responds with a 504 Gateway Timeout error and the message provided. (If msg is empty, a suitable default message will be sent.) After the handler times out, writes by h to its http.ResponseWriter will return http.ErrHandlerTimeout. If timeoutFunc returns a nil timeout channel, no timeout will be enforced. recordFn is a function that will be invoked whenever a timeout happens.

Types

type CORSSpec

type CORSSpec struct {
	AllowedOriginPatterns []string `yaml:"allowedOriginPatterns"`
	AllowedMethods        []string `yaml:"allowedMethods"`
	AllowedHeaders        []string `yaml:"allowedHeaders"`
	ExposedHeaders        []string `yaml:"exposedHeaders"`
	AllowCredentials      bool     `yaml:"allowCredentials"`
}

type Middleware

type Middleware func(httprouter.Handle) httprouter.Handle

type Nelly

type Nelly struct {
	CORS                 CORSSpec
	RequiredHeaders      []string
	RequiredHeaderValues map[string]string
	RequestTimeout       time.Duration
}

func New

func New() *Nelly

func (*Nelly) Classic

func (n *Nelly) Classic() Stack

func (*Nelly) RegisterMetrics

func (n *Nelly) RegisterMetrics()

func (*Nelly) SetCORS

func (n *Nelly) SetCORS(cors CORSSpec)

func (*Nelly) SetRequestTimeout

func (n *Nelly) SetRequestTimeout(timeout time.Duration)

func (*Nelly) SetRequiredHeaderValues

func (n *Nelly) SetRequiredHeaderValues(requiredHeaderValues map[string]string)

func (*Nelly) SetRequiredHeaders

func (n *Nelly) SetRequiredHeaders(requiredHeaders []string)

func (*Nelly) WithCORS

func (n *Nelly) WithCORS(handler httprouter.Handle) httprouter.Handle

WithCORS is a simple CORS implementation that wraps an http Handler. Pass nil for allowedMethods and allowedHeaders to use the defaults. If allowedOriginPatterns is empty or nil, no CORS support is installed.

func (*Nelly) WithCacheControl

func (n *Nelly) WithCacheControl(handler httprouter.Handle) httprouter.Handle

WithCacheControl sets the Cache-Control header to "no-cache, private" because all servers are protected by authn/authz. see https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#defining_optimal_cache-control_policy

func (*Nelly) WithInstrument

func (n *Nelly) WithInstrument(handler httprouter.Handle) httprouter.Handle

func (*Nelly) WithLogging

func (n *Nelly) WithLogging(handler httprouter.Handle) httprouter.Handle

func (*Nelly) WithPanicRecovery

func (n *Nelly) WithPanicRecovery(handler httprouter.Handle) httprouter.Handle

func (*Nelly) WithRequiredHeaderValues

func (n *Nelly) WithRequiredHeaderValues(handler httprouter.Handle) httprouter.Handle

func (*Nelly) WithRequiredHeaders

func (n *Nelly) WithRequiredHeaders(handler httprouter.Handle) httprouter.Handle

func (*Nelly) WithTimeoutForNonLongRunningRequests

func (n *Nelly) WithTimeoutForNonLongRunningRequests(handler httprouter.Handle) httprouter.Handle

func (*Nelly) WithTrace

func (n *Nelly) WithTrace(handler httprouter.Handle) httprouter.Handle

type ResponseWriterDelegator

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

ResponseWriterDelegator interface wraps http.ResponseWriter to additionally record content-length, status-code, etc.

func (*ResponseWriterDelegator) ContentLength

func (r *ResponseWriterDelegator) ContentLength() int

func (*ResponseWriterDelegator) Status

func (r *ResponseWriterDelegator) Status() int

func (*ResponseWriterDelegator) Write

func (r *ResponseWriterDelegator) Write(b []byte) (int, error)

func (*ResponseWriterDelegator) WriteHeader

func (r *ResponseWriterDelegator) WriteHeader(code int)

type Stack

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

func NewStack

func NewStack(middlewares ...Middleware) Stack

func (Stack) Append

func (s Stack) Append(middlewares ...Middleware) Stack

func (Stack) Extend

func (s Stack) Extend(stack Stack) Stack

func (Stack) Then

type StacktracePred

type StacktracePred func(httpStatus int) (logStacktrace bool)

StacktracePred returns true if a stacktrace should be logged for this status.

func StatusIsNot

func StatusIsNot(statuses ...int) StacktracePred

StatusIsNot returns a StacktracePred which will cause stacktraces to be logged for any status *not* in the given list.

Jump to

Keyboard shortcuts

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