Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 [Feature]: Expose Ctx.values #2118

Open
3 tasks done
pjebs opened this issue Sep 25, 2022 · 7 comments
Open
3 tasks done

🚀 [Feature]: Expose Ctx.values #2118

pjebs opened this issue Sep 25, 2022 · 7 comments

Comments

@pjebs
Copy link
Contributor

pjebs commented Sep 25, 2022

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:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests may be closed without explanation.
@pjebs
Copy link
Contributor Author

pjebs commented Sep 25, 2022

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?

@li-jin-gou
Copy link
Contributor

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?

@pjebs
Copy link
Contributor Author

pjebs commented Sep 25, 2022

@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
Copy link
Contributor Author

pjebs commented Oct 5, 2022

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

@pjebs
Copy link
Contributor Author

pjebs commented Oct 6, 2022

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
}

@efectn
Copy link
Member

efectn commented Dec 14, 2022

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

@pjebs
Copy link
Contributor Author

pjebs commented Dec 14, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants