It's possible to create a group of middlewares and use it per routes, or apply middlewares on demand.
Middleware grous allow you to create a group of meiddlewares and reuse it into many routes that you like. There is a working example of this here with its tests.
import "github.com/roger-russel/fasthttp-router-middleware/pkg/middleware"
func exampleAuthFunc(ctx *fasthttp.RequestCtx) bool { ... }
func exampleRuleFunc(ctx *fasthttp.RequestCtx) bool { ... }
func exampleRequestHandler(ctx *fasthttp.RequestCtx) { ... }
func main() {
...
midGroupAuth = middleware.New([]middleware.Middleware{exampleAuthFunc, exampleRuleFunc})
router := router.New()
router.GET("/", exampleRequestHandler)
router.GET("/protected", midGroupAuth(exampleRequestHandler))
...
}
Ondemand you just put yours middlewares per route like the example bellow. There is a working example of this here with its tests.
import "github.com/roger-russel/fasthttp-router-middleware/pkg/middleware"
func exampleAuthFunc(ctx *fasthttp.RequestCtx) bool { ... }
func exampleRuleFunc(ctx *fasthttp.RequestCtx) bool { ... }
func exampleRequestHandler(ctx *fasthttp.RequestCtx) { ... }
func main() {
...
router := router.New()
router.GET("/", exampleRequestHandler)
router.GET("/protected", middleware.Apply([]middleware.Middleware{
exampleAuthFunc,
exampleRuleFunc,
}, exampleRequestHandler))
...
}
Please take a look at Contribute Guide.
- @Hanjm, I learn a lot on your middleware project for fasthttp.