-
Middleware signature changed:
Middlewaretype changed fromfunc(next HandlerFunc) HandlerFunctofunc(*Context) error(type alias forHandlerFunc). Middleware now callsc.Next()to pass control to the next handler instead of receivingnextas a parameter.Before:
func RequestID() Middleware { return func(next HandlerFunc) HandlerFunc { return func(c *Context) error { id := c.Header(requestIDHeader) if id == "" { id = uuid.New().String() } c.Set("request_id", id) c.Response().Header().Set(requestIDHeader, id) return next(c) } } }
After:
func RequestID() Middleware { return func(c *Context) error { id := c.Header(requestIDHeader) if id == "" { id = uuid.New().String() } c.Set("request_id", id) c.Response().Header().Set(requestIDHeader, id) return c.Next() } }
-
MiddlewareFuncis now also a type alias forHandlerFunc.
Context.Next(): New method onContextthat executes the next handler in the middleware chain. This replaces thenext(c)call pattern and simplifies middleware authoring.
To migrate existing middleware from v0.4.x to v0.5.0:
- Remove the outer
func(next HandlerFunc) HandlerFuncwrapper - Remove the inner
return func(c *Context) errorwrapper (flatten to a single function) - Replace all
next(c)calls withc.Next() - Replace all
return next(c)withreturn c.Next()
Custom middleware before:
func myMiddleware(next okapi.HandlerFunc) okapi.HandlerFunc {
return func(c *okapi.Context) error {
// pre-processing
err := next(c)
// post-processing
return err
}
}Custom middleware after:
func myMiddleware(c *okapi.Context) error {
// pre-processing
err := c.Next()
// post-processing
return err
}- Middleware chain execution replaced from function wrapping to slice-based handler chain (
buildHandlers()) - Updated all built-in middleware:
LoggerMiddleware,BasicAuth,BodyLimit,JWTAuth,RequestID,CORSHandler,handleAccessLog - Updated
UseMiddlewareadapters (standardhttp.Handlermiddleware compatibility) in bothOkapiandGroup - Updated group middleware application in
add(),HandleStd(),HandleHTTP()