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

[Question] Naming the route #540

Closed
bentcoder opened this issue Sep 21, 2020 · 2 comments
Closed

[Question] Naming the route #540

bentcoder opened this issue Sep 21, 2020 · 2 comments

Comments

@bentcoder
Copy link

bentcoder commented Sep 21, 2020

Hi,

Maybe I might have missed but couldn't see anything so just asking. Are we able to name the routes - similar to this? See example below for better explanation.

Thanks

// Set
r.Post("/users", createUser).SetName("create_user")

// Get in the controller
...(w http.ResponseWriter, r *http.Request) {
   name := chi.RouteName(r)
}
@bentcoder
Copy link
Author

Is it possible to treat this as a "feature request" please?

@pkieltyka
Copy link
Member

pkieltyka commented Sep 26, 2020

Thanks for the request, but chi's goals are to remain a minimal router -- however, you can already do this today:

You can pull it off with your own custom middleware and use r.With(SetName("create_user")).Post("/users", createUser) .. this would work out of the box now. https://github.com/go-chi/chi/blob/master/middleware/value.go#L9-L17 here is a simple middleware you could adapt.. but if you wanted to use middleware.Value directly for the above you could do ..

r.With(middleware.Value("routeName", "create_user")).Post("/users", createUser)

//..

then in your handler,
name, _ := r.Context().Value("routeName").(string)

to make the above shorter, you'd write your own helpers to set the value and get the value

feature delivered :)

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

No branches or pull requests

2 participants