middleware

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2021 License: MIT Imports: 20 Imported by: 4

README

middlewares

Http Handler middlewares

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// LogEntryCtxKey is the context.Context key to store the request log entry.
	LogEntryCtxKey = &contextKey{"LogEntry"}
	// PanicEntryCtxKey is the context.Context key to store the request panic entry.
	PanicEntryCtxKey = &contextKey{"PanicEntry"}

	// DefaultLoggerExtensionsIgnore is the default request extensions to ignores
	DefaultLoggerExtensionsIgnore = StringsToExtensions("css", "js", "jpg", "png", "gif", "ico", "ttf", "woff2", "svg", "svgz")

	DefaultRequestLogFormatter = &DefaultLogAndPanicFormatter{
		Logger:           log.New(os.Stdout, "", log.LstdFlags),
		PanicLogger:      log.New(os.Stderr, "", log.LstdFlags),
		IgnoreExtensions: DefaultLoggerExtensionsIgnore,
	}

	// DefaultLogger is called by the Logger middleware handler to log each request.
	// Its made a package-level variable so that it can be reconfigured for custom
	// logging configurations.
	DefaultLogger = RequestLogger(DefaultRequestLogFormatter)
)
View Source
var DefaultPostLimitFailedHandler = PostLimitFailedHandler(func(w http.ResponseWriter, r *http.Request) {
	http.Error(w, "max post size exceeded", http.StatusBadRequest)
})
View Source
var ParseForm = post_limit.ParseForm

Functions

func Accept

func Accept(r *http.Request) bool

func CalcLengths

func CalcLengths(buckets []*stack.Bucket, fullPath bool) (int, int)

CalcLengths returns the maximum length of the source lines and package names.

func ColorWrite

func ColorWrite(w io.Writer, useColor bool, color []byte, s string, args ...interface{})

ColorWrite

func ColorWriteTtyCheck

func ColorWriteTtyCheck(w io.Writer, useColor bool, color []byte, s string, args ...interface{})

colorWriteTtyCheck

func GetRealIP

func GetRealIP(r *http.Request) string

func Logger

func Logger(next http.Handler) http.Handler

Logger is a middleware that logs the start and end of each request, along with some useful data about what was requested, what the response status was, and how long it took to return. When standard output is a TTY, Logger will print in color, otherwise it will print in black and white. Logger prints a request BID if one is provided.

Alternatively, look at https://github.com/pressly/lg and the `lg.RequestLogger` middleware pkg.

func LoggerPrintRequestMessage

func LoggerPrintRequestMessage(cW func(w io.Writer, useColor bool, color []byte, s string, args ...interface{}), useColor bool, maxUriLen int, w io.Writer, r *http.Request)

func LoggerPrintResponseMessage

func LoggerPrintResponseMessage(cW func(w io.Writer, useColor bool, color []byte, s string, args ...interface{}), useColor bool, w io.Writer, status, bytes int, elapsed time.Duration)

func NoCache

func NoCache(next http.Handler) http.Handler

NoCache is a simple piece of middleware that sets a number of HTTP headers to prevent a router (or subrouter) from being cached by an upstream proxy and/or client.

As per http://wiki.nginx.org/HttpProxyModule - NoCache sets:

Expires: Thu, 01 Jan 1970 00:00:00 UTC
Cache-Control: no-cache, private, max-age=0
X-Accel-Expires: 0
Pragma: no-cache (for HTTP/1.0 proxies/clients)

func PostLimit

func PostLimit(maxPostSize int64, failedHandler ...PostLimitFailedHandler) func(next http.Handler) http.Handler

func Recoverer

func Recoverer(f ...PanicFormatter) func(next http.Handler) http.Handler

Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recoverer prints a request BID if one is provided.

func RequestLogger

func RequestLogger(f LogFormatter) func(next http.Handler) http.Handler

RequestLogger returns a logger handler using a custom LogFormatter.

func StackWriteToConsole

func StackWriteToConsole(out io.Writer, p *StackPalette, buckets []*stack.Bucket, fullPath, needsEnv bool, filter, match *regexp.Regexp) error

func WithLogEntry

func WithLogEntry(r *http.Request, entry LogEntry) *http.Request

WithLogEntry sets the in-context LogEntry for a request.

Types

type ColorWriterFunc

type ColorWriterFunc func(w io.Writer, useColor bool, color []byte, s string, args ...interface{})

type DefaultLogAndPanicFormatter

type DefaultLogAndPanicFormatter struct {
	Logger, PanicLogger LoggerInterface
	NoColor             bool
	IgnoreExtensions    Extensions
	TruncateUri         int
	NoColorTtyCheck     bool
}

DefaultLogAndPanicFormatter is a simple logger that implements a LogFormatter.

func (*DefaultLogAndPanicFormatter) Accept

func (*DefaultLogAndPanicFormatter) NewLogEntry

func (l *DefaultLogAndPanicFormatter) NewLogEntry(r *http.Request) LogEntry

NewLogEntry creates a new LogEntry for the request.

func (*DefaultLogAndPanicFormatter) NewPanicEntry

func (l *DefaultLogAndPanicFormatter) NewPanicEntry(r *http.Request) PanicEntry

NewPanicEntry creates a new LogEntry for the request panic.

type Extensions

type Extensions map[string]bool

func StringsToExtensions

func StringsToExtensions(values ...string) (m Extensions)

func (Extensions) Copy

func (this Extensions) Copy() (m Extensions)

func (*Extensions) PtrUpdate

func (this *Extensions) PtrUpdate(news ...Extensions) *Extensions

func (*Extensions) PtrUpdateStrings

func (this *Extensions) PtrUpdateStrings(values ...string) *Extensions

func (Extensions) Update

func (this Extensions) Update(news ...Extensions) Extensions

func (Extensions) UpdateStrings

func (this Extensions) UpdateStrings(values ...string) Extensions

type LogAndPanicFormatter

type LogAndPanicFormatter interface {
	LogFormatter
	PanicFormatter
}

type LogEntry

type LogEntry interface {
	Write(status, bytes int, elapsed time.Duration)
	WithLogger(logger LoggerInterface) LogEntry
}

LogEntry records the final log when a request completes. See defaultLogEntry for an example implementation.

func GetLogEntry

func GetLogEntry(r *http.Request) LogEntry

GetLogEntry returns the in-context LogEntry for a request.

func NewLogEntry

func NewLogEntry(r *http.Request) LogEntry

type LogFormatter

type LogFormatter interface {
	NewLogEntry(r *http.Request) LogEntry
	Accept(r *http.Request) bool
}

LogFormatter initiates the beginning of a new LogEntry per request. See DefaultLogAndPanicFormatter for an example implementation.

type LoggerInterface

type LoggerInterface interface {
	Print(v ...interface{})
}

LoggerInterface accepts printing to stdlib logger or compatible logger.

type PanicEntry

type PanicEntry interface {
	Write(v interface{}, stack []byte)
	WithLogger(logger LoggerInterface) PanicEntry
}

PanicEntry records the final log when a request failed. See defaultPanicEntry for an example implementation.

func GetPanicEntry

func GetPanicEntry(r *http.Request) PanicEntry

GetLogEntry returns the in-context LogEntry for a request.

func NewPanicEntry

func NewPanicEntry(r *http.Request) PanicEntry

type PanicFormatter

type PanicFormatter interface {
	NewPanicEntry(r *http.Request) PanicEntry
}

PanicFormatter initiates the beginning of a new PanicEntry per request. See DefaultLogAndPanicFormatter for an example implementation.

type PostLimitFailedHandler

type PostLimitFailedHandler = http.HandlerFunc

type StackPalette

type StackPalette struct {
	EOLReset string

	// Routine header.
	RoutineFirst string // The first routine printed.
	Routine      string // Following routines.
	CreatedBy    string

	// Call line.
	Package            string
	SrcFile            string
	FuncStdLib         string
	FuncStdLibExported string
	FuncMain           string
	FuncOther          string
	FuncOtherExported  string
	Arguments          string
}

StackPalette defines the color used.

An empty object StackPalette{} can be used to disable coloring.

func (*StackPalette) BucketHeader

func (p *StackPalette) BucketHeader(bucket *stack.Bucket, fullPath, multipleBuckets bool) string

BucketHeader prints the header of a goroutine signature.

func (*StackPalette) StackLines

func (p *StackPalette) StackLines(signature *stack.Signature, srcLen, pkgLen int, fullPath bool) string

StackLines prints one complete stack trace, without the header.

Jump to

Keyboard shortcuts

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