hecho (hexa echo) contain middlewares,handlers,... for the echo.


go get


  • log: set new log handler as context log that contains:

    • request id in eac log record.
    • users data in each log record.
  • transltion: Set new translator in context that localized with users accept-languages and then fallback and default languages.


  • error handler: handle hexa errors.

Middleware dependencies:

  • hecho.CurrentUser middleware requires
    • hecho.JWT middleware (load JWT middleware before CurrentUser).
  • hecho.HexaContext middleware requires
    • echo middleware.RequestID
    • hexa hecho.CorrelationID
    • hexa hecho.CurrentUser middleware.
  • hecho.SetContextLogger middleware requires
    • hexa hexa.HexaContext


  • Map echo errors (see errors list in echo.go:263) to hexa error with translation.
  • Tests
  • Add badges to readme.
  • CI
Expand ▾ Collapse ▴




const (

	// ContextKeyHexaRequestID uses as key in context to store request id to use in context middleware
	ContextKeyHexaRequestID = "__hexa_ctx.rid__"

	// ContextKeyHexaCorrelationID uses as key in context to store correlation id to use in context middleware
	ContextKeyHexaCorrelationID = "__hexa_ctx.cid__"

	// ContextKeyHexaCtx is the identifier to set the hexa context as a field in the context of a request.
	ContextKeyHexaCtx = "__hexa_ctx.ctx__"

	// ContextKeyHexaUser is the identifier to set the hexa user as a field in the context of a request.
	ContextKeyHexaUser = "__hexa_ctx.user__"

const JwtContextKey = "jwt"


var (
	// HeaderCorrelationID is the http X-Correlation-ID header name.
	HeaderCorrelationID = "X-Correlation-ID"

	// DefaultCorrelationIDConfig is the default CorrelationID middleware config.
	DefaultCorrelationIDConfig = CorrelationIDConfig{
		Skipper:   middleware.DefaultSkipper,
		Generator: uuidGenerator,

var (
	// CurrentUserContextKey is the context key to set
	// the current user in the request context.
	CurrentUserContextKey = "user"

var (
	// DefaultRequestIDConfig is the default RequestID middleware config.
	DefaultRequestIDConfig = RequestIDConfig{
		Skipper:   middleware.DefaultSkipper,
		Generator: uuidGenerator,


func AuthMiddleware

func AuthMiddleware() echo.MiddlewareFunc

AuthMiddleware is a middleware that force user to authenticate to access to specific API. AuthMiddleware should be after the HexaContext middleware.

func CorrelationID

func CorrelationID() echo.MiddlewareFunc

CorrelationID returns a X-Request-ID middleware.

func CorrelationIDWithConfig

func CorrelationIDWithConfig(config CorrelationIDConfig) echo.MiddlewareFunc

CorrelationIDWithConfig returns a X-Correlation-ID middleware with config.

func CorsConfig

func CorsConfig(config hexa.Config) middleware.CORSConfig

CORSConfig returns te CORS config.

func CurrentUser

func CurrentUser(uf UserFinderBySub, userSDK hexa.UserSDK) echo.MiddlewareFunc

CurrentUser is a middleware to set the user in the context. If provided jwt, so this function find user and set it as user otherwise set guest user.

func CurrentUserWithConfig

func CurrentUserWithConfig(cfg CurrentUserConfig) echo.MiddlewareFunc

CurrentUser is a middleware to set the user in the context. If provided jwt, so this function find user and set it as user otherwise set guest user.

func CurrentUserWithoutFetch

func CurrentUserWithoutFetch(userSDK hexa.UserSDK) echo.MiddlewareFunc

CurrentUserWithoutFetch is for when you have a gateway that find the user and include it in the jwt. so you will dont need to any user finder.

func DebugMiddleware

func DebugMiddleware(e *echo.Echo) echo.MiddlewareFunc

DebugMiddleware make a route available just in debug mode.

func DefaultCtxCreator

func DefaultCtxCreator(ctx hexa.Context) hexa.Context

This is default implementation of context creator.

func GenerateToken

func GenerateToken(u hexa.User, cfg GenerateTokenConfig) (token, rToken hexa.Secret, err error)

GenerateToken generate new token for the user.

func GuestMiddleware

func GuestMiddleware() echo.MiddlewareFunc

GuestMiddleware is a middleware that force user to be guest to access to specific API. GuestMiddleware should be after the hexaContext middleware.

func HTTPErrorHandler

func HTTPErrorHandler(l hexa.Logger, t hexa.Translator, debug bool) echo.HTTPErrorHandler

HTTPErrorHandler is the echo error handler. this function need to the HexaContext middleware.

func HexaContext

func HexaContext(ctxCreator CtxCreator, logger hexa.Logger, translator hexa.Translator) echo.MiddlewareFunc

HexaContext set hexa context on each request.

func HexaToEchoLogger

func HexaToEchoLogger(cfg hexa.Config, logger hexa.Logger) echo.Logger

HexaToEchoLogger convert hexa logger to echo logger.

func IDAsSubjectGenerator

func IDAsSubjectGenerator(user hexa.User) (string, error)

IDAsSubjectGenerator return user's id as jwt subject (sub).

func JWT

func JWT(key hexa.Secret) echo.MiddlewareFunc

JWT middleware

func Recover

func Recover() echo.MiddlewareFunc

Recover returns a middleware which recovers from panics anywhere in the chain and handles the control to the centralized HTTPErrorHandler.

func RecoverWithConfig

func RecoverWithConfig(skipper middleware.Skipper) echo.MiddlewareFunc

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

func RefreshToken

func RefreshToken(cfg RefreshTokenConfig) (token, rToken hexa.Secret, err error)

RefreshToken refresh the jwt token by provided config. In provided config to this function set user as just simple hexa guest user. we set it by your authorizer later.

func RequestID

func RequestID() echo.MiddlewareFunc

RequestID returns a X-Request-ID middleware.

func RequestIDWithConfig

func RequestIDWithConfig(config RequestIDConfig) echo.MiddlewareFunc

RequestIDWithConfig returns a X-Request-ID middleware with config.

func ResourceAPI

func ResourceAPI(group *echo.Group, resource interface{}, prefix string, m ...echo.MiddlewareFunc)

Resource define each method that exists in provided resource.

func SetContextLogger

func SetContextLogger(cfg hexa.Config) echo.MiddlewareFunc

SetContextLogger set the hexa logger on each context.

func UserGateMiddleware

func UserGateMiddleware(cfg GateMiddlewareConfig) echo.MiddlewareFunc

UserGateMiddleware is a middleware to specify user should be authenticated or be guest to access to specific API.


type CorrelationIDConfig

type CorrelationIDConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper

	// Generator defines a function to generate an ID.
	// Optional. Default value unique uuid.
	Generator func() string

CorrelationIDConfig defines the config for CorrelationID middleware.

type CreateResource

type CreateResource interface {
	Create(c echo.Context) error

type CtxCreator

type CtxCreator func(ctx hexa.Context) hexa.Context

NewCtx get hexa Context and return itself context.

type CurrentUserConfig

type CurrentUserConfig struct {
	ExtendJWT      bool
	UserContextKey string
	JWTContextKey  string
	// contains filtered or unexported fields

CurrentUserConfig is the config to use in CurrentUser middleware.

type DeleteResource

type DeleteResource interface {
	Delete(c echo.Context) error

type GateMiddlewareConfig

type GateMiddlewareConfig struct {
	MustBeGuest bool

type GenerateTokenConfig

type GenerateTokenConfig struct {
	Secret                  hexa.Secret
	ExpireTokenAfter        time.Duration
	ExpireRefreshTokenAfter time.Duration
	SubGenerator            SubGenerator

GenerateTokenConfig use as config to generate new token.

type GetResource

type GetResource interface {
	Get(c echo.Context) error

type PatchResource

type PatchResource interface {
	Patch(c echo.Context) error

type QueryResource

type QueryResource interface {
	Query(c echo.Context) error

type RefreshTokenAuthorizer

type RefreshTokenAuthorizer func(sub string) (hexa.User, error)

RefreshTokenAuthorizer is a type check that user can get new token.

type RefreshTokenConfig

type RefreshTokenConfig struct {
	RefreshToken hexa.Secret
	// Use Authorizer to verify that can get new token.
	Authorizer RefreshTokenAuthorizer

RefreshTokenConfig use as config to refresh access token.

type RequestIDConfig

type RequestIDConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper

	// Generator defines a function to generate an ID.
	// Optional. Default value random.String(32).
	Generator func() string

RequestIDConfig defines the config for RequestID middleware.

type Resource

type Resource struct {

func (Resource) Ctx

func (r Resource) Ctx(c echo.Context) hexa.Context

Ctx method extract the hexa context from the echo context.

type SubGenerator

type SubGenerator func(user hexa.User) (string, error)

type UpdateResource

type UpdateResource interface {
	Update(c echo.Context) error

type UserFinderBySub

type UserFinderBySub func(sub string) (hexa.User, error)

UserFinderBySub find the user by provided sub.