router

package
v4.3.4 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2023 License: MIT Imports: 29 Imported by: 1

Documentation

Index

Constants

View Source
const (
	//ContextKeyUser is used as key to add the user data in the request context
	ContextKeyUser keyContext = iota
	//ContextKeyLoggerR is used as key to add the value of the http.Request at the CustomLogger middlewere execution
	ContextKeyLoggerR
	//UserLogin is used as key to add the userloging into the request context
	UserLogin
)

Variables

View Source
var (
	// RequestLogger 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.
	RequestLogger = CustomRequestLogger(&CustomLogFormatter{Logger: log.New(os.Stdout, "", log.LstdFlags), NoColor: false})
)
View Source
var (
	// ZapRequestLogger 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.
	ZapRequestLogger = CustomZapRequestLogger(&CustomZapLogFormatter{Logger: log.New(os.Stdout, "", log.LstdFlags), NoColor: false})
)

Functions

func CustomLogger

func CustomLogger(next http.Handler) http.Handler

CustomLogger 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 ID if one is provided.

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

func CustomRequestLogger

func CustomRequestLogger(f chimiddleware.LogFormatter) func(next http.Handler) http.Handler

CustomRequestLogger returns a logger handler using a custom LogFormatter.

func CustomZapLogger

func CustomZapLogger(next http.Handler) http.Handler

CustomZapLogger 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 ID if one is provided.

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

func CustomZapRequestLogger

func CustomZapRequestLogger(f chimiddleware.LogFormatter) func(next http.Handler) http.Handler

CustomZapRequestLogger returns a logger handler using a custom LogFormatter.

func NewChiRouterSimple added in v4.1.11

func NewChiRouterSimple(config ConfigSimple) *chi.Mux

NewChiRouter initialize a chi.Mux router with all required default middleware (logger, security, recovery, etc.)

func NewMetricMiddleware added in v4.1.11

func NewMetricMiddleware(namespace string, prometheusLabels stdprometheus.Labels, name string, buckets ...float64) func(next http.Handler) http.Handler

NewMetricMiddleware returns a new prometheus MetricMiddleware handler.

func UnverifiedAuthenticator added in v4.1.11

func UnverifiedAuthenticator(next http.Handler) http.Handler

UnverifiedAuthenticator doc WARNING: Don't use this method unless you know what you're doing This method parses the token but doesn't validate the signature. It's only ever useful in cases where you know the signature is valid (because it has been checked previously in the stack) and you want to extract values from it.

Types

type ConfigSimple added in v4.1.11

type ConfigSimple struct {
	Production              bool
	Security                bool
	CORS                    bool
	GatewayMode             bool
	VerboseError            bool
	AuthenticationMode      string
	LogLevel                zap.AtomicLevel
	MetricsNamespace        string
	MetricsPrometheusLabels stdprometheus.Labels
	MetricsServiceName      string
	PublicRoutes            map[string]http.Handler
	ProtectedRoutes         map[string]http.Handler
}

ConfigSimple wraps common configuration parameters

func (*ConfigSimple) Check added in v4.1.11

func (config *ConfigSimple) Check()

Check clean up the configuration and logs comments if required

type CustomLogFormatter

type CustomLogFormatter struct {
	Logger  chimiddleware.LoggerInterface
	NoColor bool
}

CustomLogFormatter is a simple logger that implements a LogFormatter.

func (*CustomLogFormatter) NewLogEntry

NewLogEntry creates a new LogEntry for the request.

type CustomZapLogFormatter

type CustomZapLogFormatter struct {
	Logger  chimiddleware.LoggerInterface
	NoColor bool
}

CustomZapLogFormatter is a simple logger that implements a LogFormatter.

func (*CustomZapLogFormatter) NewLogEntry

NewLogEntry creates a new LogEntry for the request.

type MetricMiddleware added in v4.1.11

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

MetricMiddleware is a handler that exposes prometheus metrics for the number of requests, the latency and the response size, partitioned by status code, method and HTTP path.

Jump to

Keyboard shortcuts

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