I would like to know if it is possible to pass the value obtained in middleware to the route so that it can be rendered in response.
For example, in middleware we calculate how long it took to process the request and we want to render this number in the rendered HTML response using yours template engine
func Timer() fiber.Handler {
return func(c *fiber.Ctx) {
start := time.Now()
c.Next()
stop := time.Now()
// and we want to render this value in response with other values
c.Append("Server-Timing", fmt.Sprintf("Main;dur=%v", stop.Sub(start).String()))
}
}
@hugmouse Yes you can do this with Locals.
Something like this:
app.Use(func(c *fiber.Ctx) {
c.Locals("user", "admin")
c.Next()
})
app.Get("/admin", func(c *fiber.Ctx) {
if c.Locals("user") == "admin" {
c.Status(200).Send("Welcome, admin!")
} else {
c.SendStatus(403) // => 403 Forbidden
}
})
Thank you!
Most helpful comment
@hugmouse Yes you can do this with
Locals.Something like this:
Ref: https://docs.gofiber.io/ctx#locals