I am trying to define middleware for Gin's router. The middleware is authboss's. I'm pretty sure my issue is me not understanding fully how golang's interface implementation works. I know that if struct S implements all of the methods of interface I, then we say S implements I (and also has its type).
However in this specific issue, it seems go requires my function to implement an interface, rather than struct as usual. I am using authboss package for authentication.
"github.com/volatiletech/authboss"
router.Use(middleware ...HandlerFunc)
(router is of a type which has an embedded field *gin.Engine
)gin.WrapH (h http.Handler) HandlerFunc
func (a *Authboss) LoadClientStateMiddleware(h http.Handler) http.Handler
ab:= authboss.New()
all together:
router.Use(gin.WrapH(ab.LoadClientStateMiddleware))
The error I am getting is:
cannot use ab.LoadClientStateMiddleware (type func(http.Handler) http.Handler) as type http.Handler in argument to gin.WrapH: func(http.Handler) http.Handler does not implement http.Handler (missing ServeHTTP method)
The way I read the error is, 'please make a function with ab.middleware
as receiver and implement ServeHttp', but can a function be a receiver?
func (ab *authboss.LoadClientStateMiddleware) ServeHttp (){
}
EDIT:
As Suggested bellow by @mkopriva, there is a package called a "gin-adapter" that does exactly the wrapping I needed.