Skip to content

roger-russel/fasthttp-router-middleware

Repository files navigation

Fasthttp Router Middleware

CircleCI codecov Software License Maintainability

Usage

It's possible to create a group of middlewares and use it per routes, or apply middlewares on demand.

Middleware Groups

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))

  ...

}

On demand

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))

  ...

}

Contribute Guide

Please take a look at Contribute Guide.

Thanks