Fiber: 馃敟 Consider adding support for custom Context

Created on 8 Feb 2020  路  6Comments  路  Source: gofiber/fiber

Is your feature request related to a problem?
I find myself in need to instantiate a validator and pass it to all the routes. I don't want to have any singletons in the application and so I am looking for a way to extend fiber.Context.

Describe the solution you'd like
I would like to be able to extend the Context with my own custom Context which contains a 'Validator' property which I can set at server startup. The custom Context would be passed to all the routes and I could access the custom properties when needed in my routes.

Describe alternatives you've considered
The alternative is to have a singleton that I can access from my routes. But we all know why Singletons are not a good idea.

Another alternative would be to have a middleware to do this, but I don't want to instantiate a validator on every request.

Most helpful comment

@Fenny tnx and sorry for the late reply, I saw the Locals method, but wasn't sure what the intended use was. I will try to see how it works out with that.

By the way great work with the framework... I usually use Echo, but looking at Fiber it feels more like what I like. Again tnx for the great work!

All 6 comments

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template!

Hey @cgiacomi, many web frameworks use the Context feature WithValue & Value to extend the Ctx. Fiber is really flexible and has a method called Locals (pass data between middlewares) which basically does the same. Let me show you an example:

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()
  app.Post(Valiware())
  app.Post("/register", func(c *fiber.Ctx) {
    body := c.Locals("validator")
  })
  app.Listen(3000)
}

func Valiware() func(*fiber.Ctx) {
  // initiate your validator once
  vali := validator.New()
  return func(c *fiber.Ctx) {
    parsed, err := vali(c.Body())
    if err == nil {
      c.Locals("validator", parsed)
    }
    c.Next()
  }
}

This is just a basic example, you can implement your validator logic in a 1000 ways.

Here is an example how echo framework allow to customize context and validator
https://echo.labstack.com/guide/context
https://echo.labstack.com/guide/request

@Fenny tnx and sorry for the late reply, I saw the Locals method, but wasn't sure what the intended use was. I will try to see how it works out with that.

By the way great work with the framework... I usually use Echo, but looking at Fiber it feels more like what I like. Again tnx for the great work!

I'm closing this issue, if you got further questions feel free to re-open!

Hello, @Fenny why there is an error while make that function you give?, sorry i am very new in go language. Go fiber v2
image
image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GrigoriyMikhalkin picture GrigoriyMikhalkin  路  4Comments

mayowa picture mayowa  路  3Comments

MohamedGouaouri picture MohamedGouaouri  路  3Comments

peterbourgon picture peterbourgon  路  4Comments

bashery picture bashery  路  4Comments