middleware

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 16 Imported by: 0

README

Echo Framework - Middlewares

Build Status Codecov branch Go Reference Go Report Card License

Description

echo-middleware is a Go package that provides multiple middleware for Echo Framework.

Requirements

Echo Middlewares requires Go 1.15 or later and Echo Framework v4.

Instalation

Use go get.

$ go get github.com/faabiosr/echo-middleware

Then import the package into your own code:

import "github.com/faabiosr/echo-middleware"

Development

Requirements
  • Install Go
Makefile
# Clean up
$ make clean

# Download project dependencies
$ make configure

# Run tests and generates html coverage file
$ make cover

# Format all go files
$ make fmt

# Run tests
$make test

License

This project is released under the MIT licence. See LICENSE for more details.

Documentation

Overview

Package middleware provides middlewares for Echo Framework.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// OpenCensusRequestCount counts the HTTP requests started.
	OpenCensusRequestCount = &view.View{
		Name:        "request_count",
		Description: "Count of HTTP request started",
		Measure:     ochttp.ServerRequestCount,
		Aggregation: view.Count(),
	}

	// OpenCensusRequestCountByMethod counts the HTTP requests by method.
	OpenCensusRequestCountByMethod = &view.View{
		Name:        "request_count_by_method",
		Description: "Server request count by HTTP method",
		TagKeys:     []tag.Key{ochttp.Method},
		Measure:     ochttp.ServerRequestCount,
		Aggregation: view.Count(),
	}

	// OpenCensusRequestCountByPath counts the HTTP requests by path.
	OpenCensusRequestCountByPath = &view.View{
		Name:        "request_count_by_path",
		Description: "Server request count by HTTP path",
		TagKeys:     []tag.Key{ochttp.Path},
		Measure:     ochttp.ServerRequestCount,
		Aggregation: view.Count(),
	}

	// OpenCensusResponseCountByStatusCode counts the HTTP requests by status code.
	OpenCensusResponseCountByStatusCode = &view.View{
		Name:        "response_count_by_status_code",
		Description: "Server response count by status code",
		TagKeys:     []tag.Key{ochttp.StatusCode},
		Measure:     ochttp.ServerLatency,
		Aggregation: view.Count(),
	}
)
View Source
var DefaultCharmLogConfig = CharmLogConfig{
	FieldMap: defaultFields,
	Logger:   charm.Default(),
	Skipper:  mw.DefaultSkipper,
}

DefaultCharmLogConfig is the default CharmBracelet Log middleware config.

View Source
var DefaultLogrusConfig = LogrusConfig{
	FieldMap: defaultFields,
	Logger:   logrus.StandardLogger(),
	Skipper:  mw.DefaultSkipper,
}

DefaultLogrusConfig is the default Logrus middleware config.

DefaultOpenCensusConfig is the default OpenCensus middleware config.

View Source
var DefaultRequestIDConfig = RequestIDConfig{
	Skipper:          emw.DefaultSkipper,
	Generator:        uuidGen,
	RequestIDHandler: requestIDHandler,
	TargetHeader:     echo.HeaderXRequestID,
}

DefaultRequestIDConfig is the default RequestID middleware config, based on the echo.RequestIDConfig but with uuid generator instead.

View Source
var DefaultZapLogConfig = ZapLogConfig{
	FieldMap: defaultFields,
	Logger: func() *zap.Logger {
		lg, _ := zap.NewProduction()
		return lg
	}(),
	Skipper: mw.DefaultSkipper,
}

DefaultZapLogConfig is the default Uber ZapLog middleware config.

View Source
var DefaultZeroLogConfig = ZeroLogConfig{
	FieldMap: defaultFields,
	Logger:   log.Logger,
	Skipper:  mw.DefaultSkipper,
}

DefaultZeroLogConfig is the default ZeroLog middleware config.

Functions

func CharmLog added in v0.8.0

func CharmLog() echo.MiddlewareFunc

CharmLog returns a middleware that logs HTTP requests.

Example

This example registers the CharmBracelet Log middleware with default configuration.

e := echo.New()

// Middleware
e.Use(middleware.CharmLog())
Output:

func CharmLogWithConfig added in v0.8.0

func CharmLogWithConfig(cfg CharmLogConfig) echo.MiddlewareFunc

CharmLogWithConfig returns a CharmBracelet Log middleware with config. See: `CharmLog()`.

Example

This example registers the CharmBracelet Log middleware with custom configuration.

e := echo.New()

// Middleware
logConfig := middleware.CharmLogConfig{
	Logger: charm.Default(),
	FieldMap: map[string]string{
		"uri":    "@uri",
		"host":   "@host",
		"method": "@method",
		"status": "@status",
	},
}

e.Use(middleware.CharmLogWithConfig(logConfig))
Output:

func Logrus added in v0.2.0

func Logrus() echo.MiddlewareFunc

Logrus returns a middleware that logs HTTP requests.

Example

This example registers the Logrus middleware with default configuration.

e := echo.New()

// Middleware
e.Use(middleware.Logrus())
Output:

func LogrusWithConfig added in v0.2.0

func LogrusWithConfig(cfg LogrusConfig) echo.MiddlewareFunc

LogrusWithConfig returns a Logrus middleware with config. See: `Logrus()`.

Example

This example registers the Logrus middleware with custom configuration.

e := echo.New()

// Custom logrus logger instance
logger := logrus.New()

// Middleware
logConfig := middleware.LogrusConfig{
	Logger: logger,
	FieldMap: map[string]string{
		"uri":    "@uri",
		"host":   "@host",
		"method": "@method",
		"status": "@status",
	},
}

e.Use(middleware.LogrusWithConfig(logConfig))
Output:

func OpenCensus added in v0.3.0

func OpenCensus() echo.MiddlewareFunc

OpenCensus returns a middleware that collect HTTP requests and response metrics.

Example

This example registers the OpenCensus middleware with default configuration.

e := echo.New()

// Middleware
e.Use(middleware.OpenCensus())
Output:

func OpenCensusWithConfig added in v0.3.0

func OpenCensusWithConfig(cfg OpenCensusConfig) echo.MiddlewareFunc

OpenCensusWithConfig returns a OpenCensus middleware with config. See: `OpenCensus()`.

Example

This example registers the OpenCensus middleware with custom configuration.

e := echo.New()

// Middleware
cfg := middleware.OpenCensusConfig{
	Views: []*view.View{
		middleware.OpenCensusRequestCount,
	},
}

e.Use(middleware.OpenCensusWithConfig(cfg))
Output:

func RequestID added in v0.5.0

func RequestID() echo.MiddlewareFunc

RequestID returns a middleware that reads or generates a new request id and returns to response, also stores in context.

Example

This example registers the RequestID middleware with default configuration.

e := echo.New()

// Middleware
e.Use(middleware.RequestID())
Output:

func RequestIDValue added in v0.5.0

func RequestIDValue(ctx context.Context) string

RequestIDValue returns the value stored in the context, otherwise returns an empty string

func RequestIDWithConfig added in v0.5.0

func RequestIDWithConfig(cfg RequestIDConfig) echo.MiddlewareFunc

RequestIDWithConfig uses the echo.RequestIDWithConfig under the hood with custom generator and sets the request id in context.

Example

This example registers the RequestID middleware with custom configuration.

e := echo.New()

// Middleware
config := middleware.RequestIDConfig{
	TargetHeader: echo.HeaderXRequestID,
}

e.Use(middleware.RequestIDWithConfig(config))
Output:

func ZapLog added in v0.4.0

func ZapLog() echo.MiddlewareFunc

ZapLog returns a middleware that logs HTTP requests.

Example

This example registers the ZapLog middleware with default configuration.

e := echo.New()

// Middleware
e.Use(middleware.ZapLog())
Output:

func ZapLogWithConfig added in v0.4.0

func ZapLogWithConfig(cfg ZapLogConfig) echo.MiddlewareFunc

ZapLogWithConfig returns a Uber ZapLog middleware with config. See: `ZapLog()`.

Example

This example registers the ZapLog middleware with custom configuration.

e := echo.New()

// Custom ZapLog logger instance
logger, _ := zap.NewProduction()

// Middleware
logConfig := middleware.ZapLogConfig{
	Logger: logger,
	FieldMap: map[string]string{
		"uri":    "@uri",
		"host":   "@host",
		"method": "@method",
		"status": "@status",
	},
}

e.Use(middleware.ZapLogWithConfig(logConfig))
Output:

func ZeroLog

func ZeroLog() echo.MiddlewareFunc

ZeroLog returns a middleware that logs HTTP requests.

Example

This example registers the ZeroLog middleware with default configuration.

e := echo.New()

// Middleware
e.Use(middleware.ZeroLog())
Output:

func ZeroLogWithConfig

func ZeroLogWithConfig(cfg ZeroLogConfig) echo.MiddlewareFunc

ZeroLogWithConfig returns a ZeroLog middleware with config. See: `ZeroLog()`.

Example

This example registers the ZeroLog middleware with custom configuration.

e := echo.New()

// Custom zerolog logger instance
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()

// Middleware
logConfig := middleware.ZeroLogConfig{
	Logger: logger,
	FieldMap: map[string]string{
		"uri":    "@uri",
		"host":   "@host",
		"method": "@method",
		"status": "@status",
	},
}

e.Use(middleware.ZeroLogWithConfig(logConfig))
Output:

Types

type CharmLogConfig added in v0.8.0

type CharmLogConfig struct {
	// FieldMap set a list of fields with tags
	//
	// Tags to constructed the logger fields.
	//
	// - @id (Request ID)
	// - @remote_ip
	// - @uri
	// - @host
	// - @method
	// - @path
	// - @protocol
	// - @referer
	// - @user_agent
	// - @status
	// - @error
	// - @latency (In nanoseconds)
	// - @latency_human (Human readable)
	// - @bytes_in (Bytes received)
	// - @bytes_out (Bytes sent)
	// - @header:<NAME>
	// - @query:<NAME>
	// - @form:<NAME>
	// - @cookie:<NAME>
	FieldMap map[string]string

	// Logger it is a charm logger
	Logger charm.Logger

	// Skipper defines a function to skip middleware.
	Skipper mw.Skipper
}

CharmLogConfig defines the config for CharmBracelet Log middleware.

type LogrusConfig added in v0.2.0

type LogrusConfig struct {
	// FieldMap set a list of fields with tags
	//
	// Tags to constructed the logger fields.
	//
	// - @id (Request ID)
	// - @remote_ip
	// - @uri
	// - @host
	// - @method
	// - @path
	// - @protocol
	// - @referer
	// - @user_agent
	// - @status
	// - @error
	// - @latency (In nanoseconds)
	// - @latency_human (Human readable)
	// - @bytes_in (Bytes received)
	// - @bytes_out (Bytes sent)
	// - @header:<NAME>
	// - @query:<NAME>
	// - @form:<NAME>
	// - @cookie:<NAME>
	FieldMap map[string]string

	// Logger it is a logrus logger
	Logger logrus.FieldLogger

	// Skipper defines a function to skip middleware.
	Skipper mw.Skipper
}

LogrusConfig defines the config for Logrus middleware.

type OpenCensusConfig added in v0.3.0

type OpenCensusConfig struct {
	// View it is a OpenCensus Views list.
	Views []*view.View

	// Skipper defines a function to skip middleware.
	Skipper mw.Skipper
}

OpenCensusConfig defines the config for OpenCensus middleware.

type RequestIDConfig added in v0.5.1

type RequestIDConfig = emw.RequestIDConfig

RequestIDConfig alias for emw.RequestIDConfig

type ZapLogConfig added in v0.4.0

type ZapLogConfig struct {
	// FieldMap set a list of fields with tags
	//
	// Tags to constructed the logger fields.
	//
	// - @id (Request ID)
	// - @remote_ip
	// - @uri
	// - @host
	// - @method
	// - @path
	// - @protocol
	// - @referer
	// - @user_agent
	// - @status
	// - @error
	// - @latency (In nanoseconds)
	// - @latency_human (Human readable)
	// - @bytes_in (Bytes received)
	// - @bytes_out (Bytes sent)
	// - @header:<NAME>
	// - @query:<NAME>
	// - @form:<NAME>
	// - @cookie:<NAME>
	FieldMap map[string]string

	// Logger it is a zap logger
	Logger *zap.Logger

	// Skipper defines a function to skip middleware.
	Skipper mw.Skipper
}

ZapLogConfig defines the config for Uber ZapLog middleware.

type ZeroLogConfig

type ZeroLogConfig struct {
	// FieldMap set a list of fields with tags
	//
	// Tags to constructed the logger fields.
	//
	// - @id (Request ID)
	// - @remote_ip
	// - @uri
	// - @host
	// - @method
	// - @path
	// - @protocol
	// - @referer
	// - @user_agent
	// - @status
	// - @error
	// - @latency (In nanoseconds)
	// - @latency_human (Human readable)
	// - @bytes_in (Bytes received)
	// - @bytes_out (Bytes sent)
	// - @header:<NAME>
	// - @query:<NAME>
	// - @form:<NAME>
	// - @cookie:<NAME>
	FieldMap map[string]string

	// Logger it is a zerolog logger
	Logger zerolog.Logger

	// Skipper defines a function to skip middleware.
	Skipper mw.Skipper
}

ZeroLogConfig defines the config for ZeroLog middleware.

Jump to

Keyboard shortcuts

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