Documentation
¶
Index ¶
- type MiddlewareProvider
- func (mp *MiddlewareProvider) NewAuthMiddleware(authStrategy string, ...) gin.HandlerFunc
- func (mp *MiddlewareProvider) NewCorsMiddleware(corsConfig *cors.Config) gin.HandlerFunc
- func (mp *MiddlewareProvider) NewErrorMiddleware() gin.HandlerFunc
- func (mp *MiddlewareProvider) NewLoggingMiddleware() gin.HandlerFunc
- func (mp *MiddlewareProvider) NewPermissionMiddleware(roleContextKey string, requiredPermission string, ...) gin.HandlerFunc
- func (mp *MiddlewareProvider) NewRateLimitMiddleware(limit rate.Limit, burst int) gin.HandlerFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MiddlewareProvider ¶
type MiddlewareProvider struct {
// contains filtered or unexported fields
}
func NewMiddlewareProvider ¶
func NewMiddlewareProvider(logger ezutil.Logger) *MiddlewareProvider
func (*MiddlewareProvider) NewAuthMiddleware ¶
func (mp *MiddlewareProvider) NewAuthMiddleware( authStrategy string, tokenCheckFunc func(ctx *gin.Context, token string) (bool, map[string]any, error), ) gin.HandlerFunc
NewAuthMiddleware creates an authentication middleware for Gin. It extracts a token using the given strategy (e.g., "Bearer") via extractToken, calls tokenCheckFunc to validate the token and retrieve user data, stores user data in the Gin context, and aborts the request on errors. Returns a Gin HandlerFunc for authentication handling.
func (*MiddlewareProvider) NewCorsMiddleware ¶
func (mp *MiddlewareProvider) NewCorsMiddleware(corsConfig *cors.Config) gin.HandlerFunc
NewCorsMiddleware creates a CORS middleware for Gin with the provided configuration. If corsConfig is nil, default settings are used (via cors.Default()). The middleware validates the configuration and logs a fatal error if invalid. Returns a Gin HandlerFunc to handle CORS according to the specified config.
func (*MiddlewareProvider) NewErrorMiddleware ¶
func (mp *MiddlewareProvider) NewErrorMiddleware() gin.HandlerFunc
func (*MiddlewareProvider) NewLoggingMiddleware ¶
func (mp *MiddlewareProvider) NewLoggingMiddleware() gin.HandlerFunc
func (*MiddlewareProvider) NewPermissionMiddleware ¶
func (mp *MiddlewareProvider) NewPermissionMiddleware( roleContextKey string, requiredPermission string, permissionMap map[string][]string, ) gin.HandlerFunc
NewPermissionMiddleware creates a permission-checking middleware for Gin. It retrieves the user role from context using the provided roleContextKey, checks if the role exists in permissionMap and includes the requiredPermission, and aborts the request with a ForbiddenError if permission is missing. Returns a Gin HandlerFunc for permission enforcement.
func (*MiddlewareProvider) NewRateLimitMiddleware ¶
func (mp *MiddlewareProvider) NewRateLimitMiddleware(limit rate.Limit, burst int) gin.HandlerFunc
NewRateLimitMiddleware creates a rate limiter middleware for Gin. It limits requests based on the client's IP address using a token bucket algorithm. limit: The number of requests per second derived from time.Duration (e.g., 1 request per second). burst: The maximum number of requests allowed to exceed the limit.