Documentation ¶
Index ¶
- Constants
- Variables
- func AsErrorMapper(errMapper ErrorMapper) any
- func ProvideAsMiddleware(provider any, anns ...fx.Annotation) fx.Option
- func RegisterDefaultErrorMapper(builder ErrorResponseBuilder) fx.Option
- func RegisterErrorAsMapper(errAs error, builder ErrorResponseBuilder) fx.Option
- func RegisterErrorCodeMapper(errCode string, builder ErrorResponseBuilder) fx.Option
- func RegisterErrorIsMapper(errIs error, builder ErrorResponseBuilder) fx.Option
- func SetPrincipal(ctx *gin.Context, principal Principal)
- type AuthNMiddleware
- func (m *AuthNMiddleware) ExcludePrefix(pathPrefix string)
- func (m *AuthNMiddleware) ExcludePrefixAndMethods(pathPrefix string, methods ...string)
- func (m *AuthNMiddleware) Priority() MiddlewarePriority
- func (m *AuthNMiddleware) Require(ctx *gin.Context)
- func (m *AuthNMiddleware) Setup(httpHandler rest.HTTPHandler)
- type AuthNProvider
- type AuthZMiddleware
- type AuthZProvider
- type BaseMiddleware
- type DatabaseTrxMiddleware
- type ErrorHandlerMiddleware
- type ErrorHandlerMiddlewareParams
- type ErrorMapper
- type ErrorResponseBuilder
- type HealthCheckMiddleware
- type HealthCheckMiddlewareParams
- type HealthCheckProvider
- type Middleware
- type MiddlewarePriority
- type Middlewares
- type PanicRecoveryMiddleware
- type Principal
- type PrincipalType
- type ReadyCheckMiddleware
- type ReadyCheckMiddlewareParams
- type ReadyCheckProvider
- type RequestLoggerMiddleware
Constants ¶
const AuthNExcludedCtxKey = "authn_excluded"
const ErrCodeHealthCheckFailed = "HEALTH_CHECK_FAILED"
const ErrCodeReadyCheckFailed = "READY_CHECK_FAILED"
const PrincipalCtxKey = "authn_principal"
Variables ¶
var AuthNModule = ProvideAsMiddleware(NewAuthN)
var AuthZModule = ProvideAsMiddleware(NewAuthZ)
var DatabaseTrxModule = ProvideAsMiddleware(NewDatabaseTrx)
var ErrInvalidToken = errors.Newf("AUTHN_TOKEN_NOT_VALID", "token is not valid")
var Module = fx.Options( fx.Invoke(NewMiddlewares), ProvideAsMiddleware(NewHealthCheckFx), ProvideAsMiddleware(NewReadyCheckFx), ProvideAsMiddleware(NewPanicRecovery), ProvideAsMiddleware(NewErrorHandlerFx), )
var RequestLoggerModule = ProvideAsMiddleware(NewRequestLogger)
Functions ¶
func AsErrorMapper ¶
func AsErrorMapper(errMapper ErrorMapper) any
func ProvideAsMiddleware ¶
func ProvideAsMiddleware(provider any, anns ...fx.Annotation) fx.Option
func RegisterDefaultErrorMapper ¶
func RegisterDefaultErrorMapper(builder ErrorResponseBuilder) fx.Option
func RegisterErrorAsMapper ¶
func RegisterErrorAsMapper(errAs error, builder ErrorResponseBuilder) fx.Option
func RegisterErrorCodeMapper ¶
func RegisterErrorCodeMapper(errCode string, builder ErrorResponseBuilder) fx.Option
func RegisterErrorIsMapper ¶
func RegisterErrorIsMapper(errIs error, builder ErrorResponseBuilder) fx.Option
func SetPrincipal ¶
Types ¶
type AuthNMiddleware ¶
type AuthNMiddleware struct { BaseMiddleware // contains filtered or unexported fields }
AuthNMiddleware The exclusions are implemented as a map from path prefix to list of methods. If the list of methods is empty, then all methods are excluded for the path prefix acting as map key
func NewAuthN ¶
func NewAuthN( conf config.Config, lf *log.LoggerFactory, provider AuthNProvider, ) *AuthNMiddleware
func (*AuthNMiddleware) ExcludePrefix ¶
func (m *AuthNMiddleware) ExcludePrefix(pathPrefix string)
ExcludePrefix excludes the given path prefix from authentication for all methods. Internally, this method attempts to add the empty list to the exclusion map. If the path prefix was already excluded for specific methods, then the function will make the list nil to exclude all methods.
func (*AuthNMiddleware) ExcludePrefixAndMethods ¶
func (m *AuthNMiddleware) ExcludePrefixAndMethods(pathPrefix string, methods ...string)
ExcludePrefixAndMethods excludes the given path prefix from authentication for the given methods. There must be at least one given method. Internally, this method attempts to append the given methods to the methods list already present in the exclusion map for the given path prefix. If the already present method list is of length zero, it means the path prefix was already excluded for all methods, and the function doesn't do anything further.
func (*AuthNMiddleware) Priority ¶
func (m *AuthNMiddleware) Priority() MiddlewarePriority
func (*AuthNMiddleware) Require ¶
func (m *AuthNMiddleware) Require(ctx *gin.Context)
func (*AuthNMiddleware) Setup ¶
func (m *AuthNMiddleware) Setup(httpHandler rest.HTTPHandler)
type AuthNProvider ¶
type AuthZMiddleware ¶
type AuthZMiddleware struct { BaseMiddleware // contains filtered or unexported fields }
func NewAuthZ ¶
func NewAuthZ( conf config.Config, lf *log.LoggerFactory, provider AuthZProvider, ) *AuthZMiddleware
func (*AuthZMiddleware) Priority ¶
func (m *AuthZMiddleware) Priority() MiddlewarePriority
func (*AuthZMiddleware) Require ¶
func (m *AuthZMiddleware) Require(args ...any) gin.HandlerFunc
func (*AuthZMiddleware) RequireCustom ¶
func (m *AuthZMiddleware) RequireCustom(provider AuthZProvider, args ...any) gin.HandlerFunc
func (*AuthZMiddleware) Setup ¶
func (m *AuthZMiddleware) Setup(rest.HTTPHandler)
type AuthZProvider ¶
type BaseMiddleware ¶
func (*BaseMiddleware) GetLogger ¶
func (m *BaseMiddleware) GetLogger() log.Logger
type DatabaseTrxMiddleware ¶
type DatabaseTrxMiddleware struct { BaseMiddleware // contains filtered or unexported fields }
func NewDatabaseTrx ¶
func NewDatabaseTrx( conf config.Config, lf *log.LoggerFactory, db database.DB, ) *DatabaseTrxMiddleware
func (*DatabaseTrxMiddleware) Priority ¶
func (m *DatabaseTrxMiddleware) Priority() MiddlewarePriority
func (*DatabaseTrxMiddleware) Run ¶
func (m *DatabaseTrxMiddleware) Run(ctx *gin.Context)
func (*DatabaseTrxMiddleware) Setup ¶
func (m *DatabaseTrxMiddleware) Setup(httpHandler rest.HTTPHandler)
type ErrorHandlerMiddleware ¶
type ErrorHandlerMiddleware struct { BaseMiddleware // contains filtered or unexported fields }
func NewErrorHandler ¶
func NewErrorHandler( conf config.Config, lf *log.LoggerFactory, errMappings []ErrorMapper, defaultErrorMapper ErrorResponseBuilder, ) *ErrorHandlerMiddleware
func NewErrorHandlerFx ¶
func NewErrorHandlerFx(params ErrorHandlerMiddlewareParams) *ErrorHandlerMiddleware
func (*ErrorHandlerMiddleware) Priority ¶
func (m *ErrorHandlerMiddleware) Priority() MiddlewarePriority
func (*ErrorHandlerMiddleware) Run ¶
func (m *ErrorHandlerMiddleware) Run(ctx *gin.Context)
func (*ErrorHandlerMiddleware) Setup ¶
func (m *ErrorHandlerMiddleware) Setup(httpHandler rest.HTTPHandler)
type ErrorHandlerMiddlewareParams ¶
type ErrorHandlerMiddlewareParams struct { di.BaseParams ErrorMappers []ErrorMapper `group:"error_mappers"` DefaultErrorMapper ErrorResponseBuilder `name:"default_error_mapper" optional:"true"` }
type ErrorMapper ¶
type ErrorMapper struct {
// contains filtered or unexported fields
}
func NewErrorAsMapper ¶
func NewErrorAsMapper[E error](mapErr E, builder ErrorResponseBuilder) ErrorMapper
func NewErrorCodeMapper ¶
func NewErrorCodeMapper(errCode string, builder ErrorResponseBuilder) ErrorMapper
func NewErrorIsMapper ¶
func NewErrorIsMapper(mapErr error, builder ErrorResponseBuilder) ErrorMapper
type ErrorResponseBuilder ¶
type HealthCheckMiddleware ¶
type HealthCheckMiddleware struct { BaseMiddleware // contains filtered or unexported fields }
func NewHealthCheck ¶
func NewHealthCheck(conf config.Config, lf *log.LoggerFactory, healthChecks []HealthCheckProvider) *HealthCheckMiddleware
func NewHealthCheckFx ¶
func NewHealthCheckFx(params HealthCheckMiddlewareParams) *HealthCheckMiddleware
func (*HealthCheckMiddleware) HealthCheck ¶
func (m *HealthCheckMiddleware) HealthCheck(ctx *gin.Context)
func (*HealthCheckMiddleware) Priority ¶
func (m *HealthCheckMiddleware) Priority() MiddlewarePriority
func (*HealthCheckMiddleware) Setup ¶
func (m *HealthCheckMiddleware) Setup(httpHandler rest.HTTPHandler)
type HealthCheckMiddlewareParams ¶
type HealthCheckMiddlewareParams struct { di.BaseParams HealthChecks []HealthCheckProvider `group:"health_checks"` }
type HealthCheckProvider ¶
type Middleware ¶
type Middleware interface { Setup(httpHandler rest.HTTPHandler) Priority() MiddlewarePriority GetLogger() log.Logger }
type MiddlewarePriority ¶
type MiddlewarePriority int
const ( MiddlewarePriorityHighest MiddlewarePriority = iota * 1_000 MiddlewarePriorityProbes MiddlewarePriorityAuthN MiddlewarePriorityAuthZ MiddlewarePriorityHeader MiddlewarePriorityBody MiddlewarePriorityDefault MiddlewarePriorityLowest )
type Middlewares ¶
type Middlewares []Middleware
func NewMiddlewares ¶
func NewMiddlewares(deps struct { fx.In LF *log.LoggerFactory Middlewares []Middleware `group:"middlewares"` HTTPHandler rest.HTTPHandler }) Middlewares
func (Middlewares) Setup ¶
func (ms Middlewares) Setup(httpHandler rest.HTTPHandler)
type PanicRecoveryMiddleware ¶
type PanicRecoveryMiddleware struct {
BaseMiddleware
}
func NewPanicRecovery ¶
func NewPanicRecovery( conf config.Config, lf *log.LoggerFactory, ) *PanicRecoveryMiddleware
func (*PanicRecoveryMiddleware) Priority ¶
func (m *PanicRecoveryMiddleware) Priority() MiddlewarePriority
func (*PanicRecoveryMiddleware) Run ¶
func (m *PanicRecoveryMiddleware) Run(ctx *gin.Context)
func (*PanicRecoveryMiddleware) Setup ¶
func (m *PanicRecoveryMiddleware) Setup(httpHandler rest.HTTPHandler)
type Principal ¶
type Principal interface { GetID() any GetName() string GetEmail() string GetType() PrincipalType }
func MustGetPrincipal ¶
type PrincipalType ¶
type PrincipalType string
type ReadyCheckMiddleware ¶
type ReadyCheckMiddleware struct { BaseMiddleware // contains filtered or unexported fields }
func NewReadyCheck ¶
func NewReadyCheck( conf config.Config, lf *log.LoggerFactory, readyChecks []ReadyCheckProvider, ) *ReadyCheckMiddleware
func NewReadyCheckFx ¶
func NewReadyCheckFx(params ReadyCheckMiddlewareParams) *ReadyCheckMiddleware
func (*ReadyCheckMiddleware) Priority ¶
func (m *ReadyCheckMiddleware) Priority() MiddlewarePriority
func (*ReadyCheckMiddleware) ReadyCheck ¶
func (m *ReadyCheckMiddleware) ReadyCheck(ctx *gin.Context)
func (*ReadyCheckMiddleware) Setup ¶
func (m *ReadyCheckMiddleware) Setup(httpHandler rest.HTTPHandler)
type ReadyCheckMiddlewareParams ¶
type ReadyCheckMiddlewareParams struct { di.BaseParams LF *log.LoggerFactory ReadyChecks []ReadyCheckProvider `group:"ready_checks"` }
type ReadyCheckProvider ¶
type RequestLoggerMiddleware ¶
type RequestLoggerMiddleware struct { BaseMiddleware // contains filtered or unexported fields }
func NewRequestLogger ¶
func NewRequestLogger(conf config.Config, lf *log.LoggerFactory) *RequestLoggerMiddleware
func (*RequestLoggerMiddleware) Priority ¶
func (m *RequestLoggerMiddleware) Priority() MiddlewarePriority
func (*RequestLoggerMiddleware) Run ¶
func (m *RequestLoggerMiddleware) Run(ctx *gin.Context)
func (*RequestLoggerMiddleware) Setup ¶
func (m *RequestLoggerMiddleware) Setup(httpHandler rest.HTTPHandler)