middleware

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2020 License: MIT Imports: 12 Imported by: 13

Documentation

Index

Constants

View Source
const RequestIDKey ctxKeyRequestID = 0

RequestIDKey is the key that holds the unique request ID in a request context.

Variables

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

	// 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(&DefaultLogFormatter{Logger: log.New(os.Stdout, "", log.LstdFlags), NoColor: false})
)

Functions

func GetReqID

func GetReqID(ctx context.Context) string

GetReqID returns a request ID from the given context if one is present. Returns the empty string if a request ID cannot be found.

func NextRequestID

func NextRequestID() uint64

NextRequestID generates the next request ID in the sequence.

func Recoverer

func Recoverer(next bokchoy.Handler) bokchoy.Handler

Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and adds the error to the request context

func RequestID

func RequestID(next bokchoy.Handler) bokchoy.Handler

func RequestLogger

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

RequestLogger returns a logger handler using a custom LogFormatter.

func Timeout

func Timeout(timeout time.Duration) func(next bokchoy.Handler) bokchoy.Handler

Timeout is a middleware that cancels ctx after a given timeout and return

It's required that you select the ctx.Done() channel to check for the signal if the context has reached its deadline and return, otherwise the timeout signal will be just ignored.

ie. a handler may look like:

 queue.HandlerFunc(func(r *bokchoy.Request) {
	 ctx := r.Context()
	 processTime := time.Duration(rand.Intn(4)+1) * time.Second

	 select {
	 case <-ctx.Done():
	 	return

	 case <-time.After(processTime):
	 	 // The above channel simulates some hard work.
	 }

	 return nil
 })

func WithLogEntry

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

WithLogEntry sets the in-context LogEntry for a request.

Types

type DefaultLogFormatter

type DefaultLogFormatter struct {
	Logger  LoggerInterface
	NoColor bool
}

DefaultLogFormatter is a simple logger that implements a LogFormatter.

func (*DefaultLogFormatter) NewLogEntry

func (l *DefaultLogFormatter) NewLogEntry(r *bokchoy.Request) LogEntry

NewLogEntry creates a new LogEntry for the request.

type LogEntry

type LogEntry interface {
	Write(r *bokchoy.Request, elapsed time.Duration)
	Panic(v interface{}, stack []byte)
}

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

func GetLogEntry

func GetLogEntry(r *bokchoy.Request) LogEntry

GetLogEntry returns the in-context LogEntry for a request.

type LogFormatter

type LogFormatter interface {
	NewLogEntry(r *bokchoy.Request) LogEntry
}

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

type LoggerInterface

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

LoggerInterface accepts printing to stdlib logger or compatible logger.

Jump to

Keyboard shortcuts

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