middleware

package module
Version: v0.3.10 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2022 License: MIT Imports: 12 Imported by: 0

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.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 DefaultLogrusConfig = LogrusConfig{
	FieldMap: map[string]string{
		"remote_ip": logRemoteIP,
		"uri":       logURI,
		"host":      logHost,
		"method":    logMethod,
		"status":    logStatus,
		"latency":   logLatency,
		"error":     logError,
	},
	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": logRemoteIP,
		"uri":       logURI,
		"host":      logHost,
		"method":    logMethod,
		"status":    logStatus,
		"latency":   logLatency,
		"error":     logError,
	},
	Logger:  log.Logger,
	Skipper: mw.DefaultSkipper,
}

DefaultZeroLogConfig is the default ZeroLog middleware config.

Functions

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