fiber
fiber copied to clipboard
🚀 [Feature]: Expose Ctx.values
Feature Description
It would be good to expose values
in Context
.
I need to intercept the request and modify the parameter values in a middleware.
The end route handler uses c.Params(key)
. This method uses values
and c.route.Params
.
I can already modify c.route.Params
but I'm stuck due to values
.
https://github.com/gofiber/fiber/blob/v2.37.1/ctx.go#L65
Additional Context (optional)
No response
Code Snippet (optional)
No response
Checklist:
- [X] I agree to follow Fiber's Code of Conduct.
- [X] I have checked for existing issues that describe my suggestion prior to opening this one.
- [X] I understand that improperly formatted feature requests may be closed without explanation.
Alternatively, is there a way to provide to the Context
(in middleware, after the routing has already been done and arrived at the middleware) a brand new route path (i.e. /user/:name/books/:title
) and then have the route params + values regenerated?
Hello @pjebs I noticed that the populating logic of Ctx.values is done when the route is matched in https://github.com/gofiber/fiber/blob/master/path.go#L438 and because the param of the route have changed, is it possible to use Internal redirect here?
@li-jin-gou No because I need to do it from a middlware that has already been reached. After the middleware, another endpoint handler will handler the request.
I need to modify the values before it reaches the eventual endpoint handler.
If I do an internal redirect, the routing process will have to be performed again. That will be a performance hit but also defeat what I am trying to do cleanly.
Any feedback on this issue. Is there any harm in exporting Values
, given Params
is already exported?
This is my current work-around:
import "unsafe"
import "reflect"
routeParams := []string{} // route param names
values := [30]string{} // corresponding values
if len(routeParams) > 0 {
c.Route().Params = routeParams
val := reflect.Indirect(reflect.ValueOf(c))
ptrToValues := unsafe.Pointer(val.FieldByName("values").UnsafeAddr())
realPtrToValues := (*[30]string)(ptrToValues)
*realPtrToValues = values
}
@li-jin-gou No because I need to do it from a middlware that has already been reached. After the middleware, another endpoint handler will handler the request.
I need to modify the values before it reaches the eventual endpoint handler.
If I do an internal redirect, the routing process will have to be performed again. That will be a performance hit but also defeat what I am trying to do cleanly.
Can you give an example about necessarily of this feature?
I'm building a framework that tightly couples SvelteKit and Fiber. (It's absoluetely amazing and almost finished)
Sveltekit is designed for creating Front-End SPA. It's designed for JS in FE and Node for backend. You can't change the node requirement.
I'm so impressed by SK, that I created a workaround that allows you to use Go (i.e. Fiber) for the backend so you get the best of both worlds.
The way it works is I hijack SK and when it needs to make a call to Node, I instead make an AJAX call to Go.
SK has it's own router with it's own syntax and parameters.
I've designed my framework so that the Fiber handlers mirror the SK router. This feature request is about making my framework's Fiber Handler feel and operate like a normal Fiber despite it actually handling/serving a SK route. Therefore I need to load the route parameters using the workaround.