EchoMiddleware

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2022 License: MIT Imports: 11 Imported by: 3

README

EchoMiddleware

middleware for echo server

usage
import (
	"github.com/universe-30/EchoMiddleware"  // logger and panic handle
	"github.com/universe-30/EchoMiddleware/tool"  // use Jsoniter as json parser
)
example
package main

import (
	"log"
	"net/http"

	"github.com/labstack/echo/v4"
	"github.com/universe-30/EchoMiddleware"
	"github.com/universe-30/EchoMiddleware/tool"
	"github.com/universe-30/LogrusULog"
	"github.com/universe-30/ULog"
)

func main() {
	// need a logger which implements ULog.Logger interface
	// here use LogrusULog as example
	logger, _ := LogrusULog.New("./logs", 2, 20, 30)
	logger.SetLevel(ULog.DebugLevel)

	// new echo instance
	hs := echo.New()

	//use Jsoniter as json parser
	hs.JSONSerializer = tool.NewJsoniter()

	//use logger middleware
	hs.Use(EchoMiddleware.LoggerWithConfig(EchoMiddleware.LoggerConfig{
		Logger: logger, //Logger interface
	}))

	//use recover and panic handler
	hs.Use(EchoMiddleware.RecoverWithConfig(EchoMiddleware.RecoverConfig{
		// callback to handler panic
		OnPanic: func(panic_err interface{}) {
			log.Println(panic_err)
		},
	}))

	///////////////////  JSONP //////////////////////
	hs.GET("/test1", func(c echo.Context) error {
		return c.String(http.StatusOK, "success test1")
	})

	hs.GET("/test2", func(c echo.Context) error {
		//example panic happen
		a := 1
		_ = 1 / (a - 1)

		return c.String(http.StatusOK, "success test2")
	})

	// start server
	hs.Start(":8080")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLoggerConfig is the default Logger middleware config.
	DefaultLoggerConfig = LoggerConfig{

		Format:           "=> ${method} | ${remote_ip} | ${host} | ${uri} | ${bytes_in} B in | ${status} | ${bytes_out} B out | ${latency_human} | ${error}",
		CustomTimeFormat: "2006-01-02 15:04:05.00000",

		Logger:            nil,
		RecordFailRequest: false,
	}
)

Functions

func LoggerWithConfig

func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc

LoggerWithConfig returns a Logger middleware with config. See: `Logger()`.

func RecoverWithConfig

func RecoverWithConfig(config RecoverConfig) echo.MiddlewareFunc

RecoverWithConfig returns a Recover middleware with config. See: `Recover()`.

Types

type LoggerConfig

type LoggerConfig struct {

	// Tags to construct the logger format.
	//
	// - time_unix
	// - time_unix_nano
	// - time_rfc3339
	// - time_rfc3339_nano
	// - time_custom
	// - 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>
	//
	// Example "${remote_ip} ${status}"
	//
	// Optional. Default value DefaultLoggerConfig.Format.
	Format string `yaml:"format"`

	// Optional. Default value DefaultLoggerConfig.CustomTimeFormat.
	CustomTimeFormat string `yaml:"custom_time_format"`

	Logger ULog.Logger

	RecordFailRequest bool
	// contains filtered or unexported fields
}

LoggerConfig defines the config for Logger middleware.

type RecoverConfig

type RecoverConfig struct {
	OnPanic func(panic_err interface{})
}

RecoverConfig defines the config for Recover middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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