ape

package module
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2022 License: Apache-2.0 Imports: 17 Imported by: 5

README

APE

ape package is a collection of http middlewares for errors, logging, context, caching and other helpers.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CtxMiddleware = CtxMiddleWare

Functions

func CacheMiddleware

func CacheMiddleware(ttl time.Duration) func(http.Handler) http.Handler

CacheMiddleware returns middleware with directive for a browser to cache the response for some TTL (time to live).

func ContentType

func ContentType(contentType string) func(http.Handler) http.Handler

func CtxMiddleWare

func CtxMiddleWare(extenders ...func(context.Context) context.Context) func(http.Handler) http.Handler

DEPRECATED: use typo-free CtxMiddleware instead

func DefaultMiddlewares

func DefaultMiddlewares(r chi.Router, args ...interface{})

DefaultMiddlewares provide sane defaults that should just work

func GetRequestID

func GetRequestID(ctx context.Context) string

func Log

func Log(r *http.Request) *logan.Entry

Log allows to retrieve request fielded logan.Entry, useful only with DefaultMiddlewares.

func LoganMiddleware

func LoganMiddleware(entry *logan.Entry, args ...interface{}) func(http.Handler) http.Handler

LoganMiddleware by default will just log the begging and end of the request Also it might take use of number of optionally injected arguments like: - time.Duration will set threshold for logging a warning of slow request - LoggerSetter - will be called with *logan.Entry which will contain auxiliary fields to identify the request - RequestIDProvider - will set result into fields of *logan.Entry if LoggerSetter is provided

func RecoverMiddleware

func RecoverMiddleware(args ...interface{}) func(http.Handler) http.Handler

RecoverMiddleware by default will just catch handler panic. Also it might take use of number of optionally injected arguments like: - LoggerGetter request context log getter - Logger implementation to log stacktrace with Error level - *jsonapi.ErrorObject to render error body - *raven.Client to report exception to Sentry

func Render

func Render(w http.ResponseWriter, res interface{})

func RenderErr

func RenderErr(w http.ResponseWriter, errs ...*jsonapi.ErrorObject)

func Serve

func Serve(ctx context.Context, handler http.Handler, cfg ServeConfig, opts ServeOpts)

Serve - accepts incoming connections on the Listener. Similar to http.Server, but with default sane timeouts suitable for generic REST API and graceful shutdown.

func SetContextLog

func SetContextLog(ctx context.Context, log *logan.Entry) context.Context

Types

type Logger

type Logger interface {
	Log(level uint32, fields map[string]interface{}, err error, withStack bool, args ...interface{})
}

Logger abstract logger interface providing all possible bells and whistles, mainly for decoupling `distributed_lab/logan` package

type LoggerGetter

type LoggerGetter func(ctx context.Context) *logan.Entry

LoggerGetter get *logan.Entry from request context

type LoggerSetter

type LoggerSetter func(ctx context.Context, entry *logan.Entry) context.Context

LoggerSetter - sets specified entry into ctx

type RequestIDProvider

type RequestIDProvider func(context.Context) string

RequestIDProvider - returns ID of the request from ctx if one present

type ServeConfig

type ServeConfig interface {
	Log() *logan.Entry
	Listener() net.Listener
}

ServeConfig - defines external dependencies of serve

type ServeOpts

type ServeOpts struct {
	// ReadTimeout is the maximum duration for reading the entire
	// request, including the body. Default is 5s.
	ReadTimeout time.Duration
	// WriteTimeout, unless connection is HTTPS (in our case never as we are always behind nginx),
	// covers the time from the end of the request header read to the end of the response write
	// (a.k.a. the lifetime of the ServeHTTP). Default 10s
	WriteTimeout time.Duration
	// ShutdownTimeout is maximum duration for waiting for server to shutdown. Default 15s.
	ShutdownTimeout time.Duration
}

ServeOpts - defines serve configuration options that must be specified by developer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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