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

is it possible to get the registered path? #344

Open
yumxu opened this issue Mar 9, 2022 · 10 comments
Open

is it possible to get the registered path? #344

yumxu opened this issue Mar 9, 2022 · 10 comments

Comments

@yumxu
Copy link

yumxu commented Mar 9, 2022

I'm about to use httprouter in a http api gateway, and use httprouter to match path.
i do not need a handle function, so i register paths with an empty handler:

router := httprouter.New()
router.Handle("GET","/user/:user_id/basic", func(writer http.ResponseWriter,
			request *http.Request, params httprouter.Params) {})
router.Handle("GET","/user/:user_id/feature", func(writer http.ResponseWriter,
			request *http.Request, params httprouter.Params) {})

then use LookUp method to match path

router.LookUp("GET","/user/1234/basic")

the LookUp method only returns handler, params and tsr currently.
what i need is the matched path /user/:user_id/basic to get the upstream server which is related to the path.

@yumxu yumxu changed the title can LookUp returns the registered path? is it possible to get the registered path? Mar 9, 2022
@jairogloz
Copy link

I am also looking for this feature 😃 I'm using prometheus to instrument my code and I'd like to use each endpoint path as label for each of my metrics, but right now, for paths using url params I get a different path for every id that I use, which is wrong because I end up with n different metrics instead of one.

@jairogloz
Copy link

jairogloz commented May 30, 2022

I came up with a quick hack. Inside your handler, you can iterate over ps httprouter.Params (which is internally a []{key, value}) and then in your request.URL.Path, replace each value by it's key, preceded by a :. Like this:

registeredPath := request.URL.Path
for _, param := range ps {
	registeredPath = strings.Replace(registeredPath, param.Value, fmt.Sprintf(":%s", param.Key), 1)
}
fmt.Println(registeredPath)

And you can do the same with router.LookUp as it returns httprouter.Params as well.

Not the most elegant solution but it gets the job done. Hope it helps.

@jehiah
Copy link

jehiah commented Mar 2, 2023

This functionality was made available in master (but not yet released)

https://pkg.go.dev/github.com/julienschmidt/httprouter@master#Router.SaveMatchedRoutePath
https://pkg.go.dev/github.com/julienschmidt/httprouter@master#Params.MatchedRoutePath

router.SaveMatchedRoutePath=true
// in request
registeredPath := ps.MatchedRoutePath()

@MattKetmo
Copy link

@julienschmidt any chance to have a new tagged release for all commits since 2019?

@creativecreature
Copy link

@julienschmidt Sorry for the ping, but it would be amazing if you could include this in a versioned release 🙏

@sebogh
Copy link

sebogh commented Mar 9, 2024

This functionality was made available in master (but not yet released)

https://pkg.go.dev/github.com/julienschmidt/httprouter@master#Router.SaveMatchedRoutePath https://pkg.go.dev/github.com/julienschmidt/httprouter@master#Params.MatchedRoutePath

router.SaveMatchedRoutePath=true
// in request
registeredPath := ps.MatchedRoutePath()

I came up with a quick hack. Inside your handler, you can iterate over ps httprouter.Params (which is internally a []{key, value}) and then in your request.URL.Path, replace each value by it's key, preceded by a :. Like this:

registeredPath := request.URL.Path
for _, param := range ps {
	registeredPath = strings.Replace(registeredPath, param.Value, fmt.Sprintf(":%s", param.Key), 1)
}
fmt.Println(registeredPath)

And you can do the same with router.LookUp as it returns httprouter.Params as well.

Not the most elegant solution but it gets the job done. Hope it helps.

Wouldn't this fail, if a value matches a fixed path element (e.g. pattern: /foo/:id and request /foo/foo)?

@letmestudy
Copy link

letmestudy commented Mar 9, 2024 via email

@ajb-neodynamics-io
Copy link

@julienschmidt Any plans to incorporate this into a versioned release any time soon? This would be a very helpful feature.

@letmestudy
Copy link

letmestudy commented Jun 26, 2024 via email

@ajb-neodynamics-io
Copy link

@letmestudy I hope you're enjoying your (extended) vacation!

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

8 participants