Back to godoc.org
github.com/kamva/hexa-echo

Package hecho

v0.0.0-...-f5abbe2
Latest Go to latest

The latest major version is .

Published: 4 days ago | License: MIT | Module: github.com/kamva/hexa-echo

Index

Constants

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 (
	AlgorithmHS256 = "HS256"
	AlgorithmHS384 = "HS384"
	AlgorithmHS512 = "HS512"

	AlgorithmRS256 = "RS256"
	AlgorithmRS384 = "RS384"
	AlgorithmRS512 = "RS512"

	AlgorithmES256 = "ES256"
	AlgorithmES384 = "ES384"
	AlgorithmES512 = "ES512"
)

Cryptography algorithms to sign our jwt token

const JwtContextKey = "jwt"

Variables

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 (
	// DefaultProxyConfig is the default Proxy middleware config.
	DefaultProxyConfig = ProxyConfig{
		Skipper:    middleware.DefaultSkipper,
		ContextKey: "target",
	}
)
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 AuthorizeRefreshToken

func AuthorizeRefreshToken(cfg AuthorizeRefreshTokenConfig) (user hexa.User, err error)

AuthorizeRefreshToken authorize provided jwt token. it first fetch the user from token, and then provide user and claim to the authorizer.

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 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 string, 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(logger hexa.Logger, level string) 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 JwtErrorHandler

func JwtErrorHandler(err error) error

JwtErrorHandler check errors type and return relative hexa error.

func NewRenderer

func NewRenderer(pattern string) echo.Renderer

NewTemplate creaets new template engine. This engine parses the template definitions from the files identified by the pattern.

func NewSingleHostReverseProxyRewriteHost

func NewSingleHostReverseProxyRewriteHost(target *url.URL) *httputil.ReverseProxy

NewSingleHostReverseProxyRewriteHost is just like httputil.NewSingleHostReverseProxyRewriteHost, but it overwrite host of the request also.

func Proxy

func Proxy(balancer ProxyBalancer) echo.MiddlewareFunc

Proxy returns a Proxy middleware.

Proxy middleware forwards the request to upstream server using a configured load balancing technique.

func ProxyWithConfig

func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc

ProxyWithConfig returns a Proxy middleware with config. See: `Proxy()`

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 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(level string) echo.MiddlewareFunc

SetContextLogger set the hexa logger on each context.

func SkipIfNotProvidedHeader

func SkipIfNotProvidedHeader(header string) middleware.Skipper

SkipIfNotProvidedHeader skip jwt middleware if jwt authorization header is not provided.

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 AuthorizeRefreshTokenConfig

type AuthorizeRefreshTokenConfig struct {
	SingingMethod jwt.SigningMethod
	Key           interface{} // for rsa this is the public key
	Token         string
	// Use Authorizer to verify that can get new token.
	Authorizer TokenAuthorizer
	UserFinder UserFinderBySub
}

AuthorizeRefreshTokenConfig use as config to refresh access token.

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 {
	SingingMethod    jwt.SigningMethod
	Key              interface{} // for rsa this is the private key
	SubGenerator     SubGenerator
	Claims           jwt.MapClaims
	ExpireTokenAfter time.Duration
}

GenerateTokenConfig use as config to generate new token.

type GetResource

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

type JwtClaimAuthorizerConfig

type JwtClaimAuthorizerConfig struct {
	Skipper       middleware.Skipper
	JWTContextKey string
	Authorizer    TokenAuthorizer
}

-------------------------------- JWT claim authorizer --------------------------------

type PatchResource

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

type ProxyBalancer

type ProxyBalancer interface {
	AddTarget(*ProxyTarget) bool
	RemoveTarget(string) bool
	Next(echo.Context) *ProxyTarget
}

ProxyBalancer defines an interface to implement a load balancing technique.

func NewRandomBalancer

func NewRandomBalancer(targets []*ProxyTarget) ProxyBalancer

NewRandomBalancer returns a random proxy balancer.

func NewRoundRobinBalancer

func NewRoundRobinBalancer(targets []*ProxyTarget) ProxyBalancer

NewRoundRobinBalancer returns a round-robin proxy balancer.

type ProxyConfig

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

	// Balancer defines a load balancing technique.
	// Required.
	Balancer ProxyBalancer

	// Rewrite defines URL path rewrite rules. The values captured in asterisk can be
	// retrieved by index e.g. $1, $2 and so on.
	// Examples:
	// "/old":              "/new",
	// "/api/*":            "/$1",
	// "/js/*":             "/public/javascripts/$1",
	// "/users/*/orders/*": "/user/$1/order/$2",
	Rewrite map[string]string

	// Context key to store selected ProxyTarget into context.
	// Optional. Default value "target".
	ContextKey string

	// To customize the transport to remote.
	// Examples: If custom TLS certificates are required.
	Transport http.RoundTripper

	ReverseProxyProvider func(target *url.URL) *httputil.ReverseProxy
	// contains filtered or unexported fields
}

ProxyConfig defines the config for Proxy middleware.

type ProxyTarget

type ProxyTarget struct {
	Name string
	URL  *url.URL
	Meta echo.Map
}

ProxyTarget defines the upstream target.

type QueryResource

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

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 Template

type Template struct {
	// contains filtered or unexported fields
}

Template is very simple template engine

func (*Template) Render

func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error

type TokenAuthorizer

type TokenAuthorizer func(claims jwt.MapClaims) error

TokenAuthorizer authorize the token.

func AuthorizeAudience

func AuthorizeAudience(aud string) TokenAuthorizer

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.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier