Router lookup feature


#1

Hello,

I am building an application with Echo and I am using some routing name like:

 app.GET("/", handler).Name = "index"

But I want to check if an url match an existing route to retrieve its name, like reverse but which returns the *Route:

 route, err := app.Lookup("/")
 if err != nil {
      return err
 }
 fmt.Println(route.Name) // this echo "index" 

Are you open for a PR for this kind of feature?


Get Route or Route Name from URL
#2

@maxime.vidori Have you looked at Echo#Routes() - it returns a list of all registered routes. You can iterate and lookup for your path or build a custom lookup function / map.


#3

This is what I wanted to do :D. I wanted to ask you if you are open to integrate this feature in Echo?


#4

You mean a lookup function? I don’t think it is generic enough to be a part of an API.


#5

Dude, the feature is already in echo.
Just make a simple func like this:

func lookup(name string) (string, error) {

	for _, v := range c.Echo().Routes() {

		if v.Name == name {
			return v.Path, nil
		}

	}

	err = errors.New("Route not found")

	return "", err

}