xecho

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

CI

xecho

A thin wrapper around https://echo.labstack.com. Includes

  • New Relic integration
  • Logrus for structured json logging
  • Custom error type
  • Custom request context with tracing (correlation ID)

Requirements

  • Go 1.27 or later
  • Echo v4 (github.com/labstack/echo/v4)

Breaking changes in v0.2.0

Upgrading from v0.1.x requires changes in consuming services:

  • Echo v3 → v4. Exported functions (Echo, EchoHandler, ContextMiddleware and the other middleware constructors) now take and return github.com/labstack/echo/v4 types. Change all github.com/labstack/echo imports, including echo/middleware, to github.com/labstack/echo/v4, then run go mod tidy. See the Echo v4.0.0 release notes for changes to Echo's own API.
  • Go 1.27 minimum. The go directive moved from 1.12 to 1.27, so consumers must build with Go 1.27 or later.
  • New Relic transaction end errors are logged. A failure to end the New Relic transaction is now logged at error level on the request logger instead of being silently ignored.

Client IP behind a load balancer

The ip field in request logs and New Relic comes from Echo's RealIP(). By default xecho uses echo.ExtractIPDirect(), so the IP is the connection's remote address and X-Forwarded-For / X-Real-IP are ignored. Behind a load balancer this is the balancer's IP. Set Config.IPExtractor to trust forwarding headers only from your proxies:

conf := xecho.NewConfig()
conf.IPExtractor = echo.ExtractIPFromXFFHeader(echo.TrustIPRange(vpcCIDR))

ExtractIPFromXFFHeader() trusts loopback, link-local and private ranges by default. Never trust forwarding headers from addresses a client can reach directly, as the header can be spoofed.

Development

Tool versions are pinned in tools/go.mod and run with go tool, so nothing needs installing globally.

  • make test - gofmt, go vet and race-enabled tests
  • make tools - pre-build the pinned tools
  • make fmt - format with gofmt and goimports
  • make lint - golangci-lint
  • make vulncheck - govulncheck; fails on reachable vulnerabilities unless listed in the Makefile as VULNCHECK_ACCEPTED (long-term) or VULNCHECK_UNTIL_FIXED (fails once a fix is released). Also runs weekly on master via the "Scheduled vulncheck" workflow, which can be triggered manually from the Actions tab
  • make apidiff - gorelease against the latest tag (needs a clean worktree)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBadRequest = &Error{
	Status: http.StatusBadRequest,
	Code:   "BAD_REQUEST",
	Detail: "Bad request",
}
View Source
var ErrInternalServer = &Error{
	Status: http.StatusInternalServerError,
	Code:   "INTERNAL_SERVER_ERROR",
	Detail: "Internal server error",
}
View Source
var ErrMethodNotAllowed = &Error{
	Status: http.StatusMethodNotAllowed,
	Code:   "METHOD_NOT_ALLOWED",
	Detail: "Method not allowed",
}
View Source
var ErrNotFound = &Error{
	Status: http.StatusNotFound,
	Code:   "NOT_FOUND",
	Detail: "Not found",
}
View Source
var ErrUnauthorised = &Error{
	Status: http.StatusUnauthorized,
	Code:   "UNAUTHORISED",
	Detail: "Unauthorised",
}

Functions

func ContextMiddleware

func ContextMiddleware(
	buildVersion string,
	logger *logrus.Entry,
	isDebug bool,
	newRelicApp newrelic.Application,
) echo.MiddlewareFunc

func DebugLoggerMiddleware

func DebugLoggerMiddleware(isDebug bool) echo.MiddlewareFunc

func DefaultHeadersMiddleware

func DefaultHeadersMiddleware() echo.MiddlewareFunc

func Echo

func Echo(conf Config) *echo.Echo

func EchoHandler

func EchoHandler(handler Handler) echo.HandlerFunc

func ErrorHandlerMiddleware

func ErrorHandlerMiddleware(errorHandler ErrorHandlerFunc) echo.MiddlewareFunc

func NewHttpClient

func NewHttpClient(
	context *Context,
	isDebug bool,
) *http.Client

func PanicHandlerMiddleware

func PanicHandlerMiddleware(errorHandler ErrorHandlerFunc) echo.MiddlewareFunc

func RequestLogger added in v0.1.7

func RequestLogger(c *Context, next echo.HandlerFunc, time TimeProvider) error

func RequestLoggerMiddleware

func RequestLoggerMiddleware(timeFn TimeProvider) echo.MiddlewareFunc

Types

type Config

type Config struct {
	ProjectName       string
	AppName           string
	EnvName           string
	BuildVersion      string
	LogLevel          logrus.Level
	LogFormatter      logrus.Formatter
	IsDebug           bool
	NewRelicLicense   string
	NewRelicEnabled   bool
	ErrorHandler      ErrorHandlerFunc
	UseDefaultHeaders bool
	RoutePrefix       string
	// IPExtractor sets how the client IP is derived for logs and New Relic attributes.
	// Nil uses echo.ExtractIPDirect, which uses the connection's remote address and ignores
	// X-Forwarded-For/X-Real-IP. Behind a load balancer, set echo.ExtractIPFromXFFHeader
	// with the proxy's ranges trusted to record the real client IP.
	IPExtractor echo.IPExtractor
}

func NewConfig

func NewConfig() Config

type Context

type Context struct {
	echo.Context
	CorrelationID string
	HttpClient    *http.Client
	NewRelicApp   newrelic.Application
	NewRelicTx    newrelic.Transaction
	// contains filtered or unexported fields
}

func NewContext added in v0.1.2

func NewContext(
	echoCtx echo.Context,
	newRelicApp newrelic.Application,
	logger *Logger,
	correlationID string,
	isDebug bool,
	buildVersion string,
) *Context

func (*Context) AddNewRelicAttribute

func (c *Context) AddNewRelicAttribute(key string, val interface{})

func (*Context) Logger

func (c *Context) Logger() echo.Logger

type Error

type Error struct {
	Status int               `json:"-" example:"400"`
	Code   string            `json:"code" example:"BAD_REQUEST"`
	Detail string            `json:"detail" example:"Bad request"`
	Params map[string]string `json:"-"`
}

func (*Error) Error

func (err *Error) Error() string

type ErrorHandlerFunc

type ErrorHandlerFunc func(c *Context, err *Error)

func DefaultErrorHandler

func DefaultErrorHandler() ErrorHandlerFunc

type Handler

type Handler func(c *Context) error

type Logger

type Logger struct {
	*logrus.Entry
}

Wrap logrus entry and implement additional methods required to satisfy echo logger interface

func (*Logger) Debugj

func (l *Logger) Debugj(j log.JSON)

func (*Logger) Errorj

func (l *Logger) Errorj(j log.JSON)

func (*Logger) Fatalj

func (l *Logger) Fatalj(j log.JSON)

func (*Logger) Infoj

func (l *Logger) Infoj(j log.JSON)

func (*Logger) Level

func (l *Logger) Level() log.Lvl

func (*Logger) Output

func (l *Logger) Output() io.Writer

func (*Logger) Panicj

func (l *Logger) Panicj(j log.JSON)

func (*Logger) Prefix

func (l *Logger) Prefix() string

func (*Logger) Printj

func (l *Logger) Printj(j log.JSON)

func (*Logger) SetHeader

func (l *Logger) SetHeader(_ string)

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl log.Lvl)

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(_ string)

func (*Logger) Warnj

func (l *Logger) Warnj(j log.JSON)

type TimeProvider added in v0.1.7

type TimeProvider func() time.Time

type Xecho added in v0.1.9

type Xecho struct {
	Echo        *echo.Echo
	NewRelicApp newrelic.Application
}

func New added in v0.1.9

func New(conf Config) *Xecho

Jump to

Keyboard shortcuts

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