You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler func(context.Context, any) (any, error)
|
|
|
|
|
|
|
|
type Middleware func(Handler) Handler
|
|
|
|
|
|
|
|
func Chain(m ...Middleware) Middleware {
|
|
|
|
return func(handler Handler) Handler {
|
|
|
|
for i := len(m) - 1; i >= 0; i-- {
|
|
|
|
handler = m[i](handler)
|
|
|
|
}
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HttpMiddleware(c *gin.Context, handler Handler, m ...Middleware) Handler {
|
|
|
|
opts := []Middleware{Recover(), AddHeaderMdMiddle(c), func(handler Handler) Handler {
|
|
|
|
return func(ctx context.Context, a any) (any, error) {
|
|
|
|
return handler(ctx, a)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
opts = append(opts, m...)
|
|
|
|
return Chain(opts...)(handler)
|
|
|
|
}
|