fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🚀 [Feature]: Expose Ctx.values

Open pjebs opened this issue 2 years ago • 7 comments

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.

pjebs avatar Sep 25 '22 00:09 pjebs

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?

pjebs avatar Sep 25 '22 01:09 pjebs

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 avatar Sep 25 '22 01:09 li-jin-gou

@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.

pjebs avatar Sep 25 '22 01:09 pjebs

Any feedback on this issue. Is there any harm in exporting Values, given Params is already exported?

pjebs avatar Oct 05 '22 22:10 pjebs

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
}

pjebs avatar Oct 06 '22 11:10 pjebs

@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?

efectn avatar Dec 14 '22 17:12 efectn

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.

pjebs avatar Dec 14 '22 21:12 pjebs