README

Echo Framework - Middlewares

Build Status Codecov branch GoDoc Go Report Card License

Description

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

Requirements

Echo Middlewares requires Go 1.12 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.

Expand ▾ Collapse ▴

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 DefaultLogrusConfig = LogrusConfig{
	FieldMap: map[string]string{
		"remote_ip": "@remote_ip",
		"uri":       "@uri",
		"host":      "@host",
		"method":    "@method",
		"status":    "@status",
		"latency":   "@latency",
		"error":     "@error",
	},
	Logger:  logrus.StandardLogger(),
	Skipper: mw.DefaultSkipper,
}

DefaultLogrusConfig is the default Logrus middleware config.

DefaultOpenCensusConfig is the default OpenCensus middleware config.

View Source
var DefaultZeroLogConfig = ZeroLogConfig{
	FieldMap: map[string]string{
		"remote_ip": "@remote_ip",
		"uri":       "@uri",
		"host":      "@host",
		"method":    "@method",
		"status":    "@status",
		"latency":   "@latency",
		"error":     "@error",
	},
	Logger:  log.Logger,
	Skipper: mw.DefaultSkipper,
}

DefaultZeroLogConfig is the default ZeroLog middleware config.

Functions

func Logrus

func Logrus() echo.MiddlewareFunc

Logrus returns a middleware that logs HTTP requests.

Example

This example registers the Logrus middleware with default configuration.

Code:

e := echo.New()

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

func LogrusWithConfig

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.

Code:

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))

func OpenCensus

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.

Code:

e := echo.New()

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

func OpenCensusWithConfig

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.

Code:

e := echo.New()

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

e.Use(middleware.OpenCensusWithConfig(cfg))

func ZeroLog

func ZeroLog() echo.MiddlewareFunc

ZeroLog returns a middleware that logs HTTP requests.

Example

This example registers the ZeroLog middleware with default configuration.

Code:

e := echo.New()

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

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.

Code:

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))

Types

type LogrusConfig

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

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 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.