xecho

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: MIT Imports: 19 Imported by: 0

README

Build Status

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)

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(
	appName string,
	envName string,
	buildVersion string,
	logger *logrus.Logger,
	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
}

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