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

Add URLPathTemplate to Route … #104

Merged
merged 5 commits into from
Mar 5, 2016

Conversation

djgilcrease
Copy link
Contributor

to make it easier to generate a API page that lists all routes by template

This works well with #83 so you can do something like

func Index(router *mux.Router) *index {
    return &index{router}
}

type index struct {
    router *mux.Router
}

func (h *index) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    h.router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
        path, _ := route.URLPathTemplate()
        fmt.Fprintf(rw, "%s\n", path)
        return nil
    })
}```

@elithrar
Copy link
Contributor

I'm going to close this one out as I don't think it's a useful addition to the public API, but I'm happy to be convinced otherwise if others have a need for it.

@elithrar elithrar closed this Feb 28, 2016
@djgilcrease
Copy link
Contributor Author

Obviously I find it useful. I use it in every rest service I have written with gorilla to serve up a simple api doc. I also use it heavily for instrumentation with new relic to ensure it is reporting consistently.

This also allows @tleyden to do what he is requesting in issue #129

@@ -508,6 508,19 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
}, nil
}

// URLPathTemplate returns the template used to match against for the route
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand on this? What is this useful for?

@elithrar
Copy link
Contributor

@djgilcrease

If you can address my comment and add tests I'll re-consider. Really big on having tests for this since mux sees a lot of PRs.

@djgilcrease
Copy link
Contributor Author

Updated comments, and added a new testTemplate method that is called everywhere testRoute is called.

@@ -532,6 532,30 @@ func (r *Route) URLPath(pairs ...string) (*url.URL, error) {
}, nil
}

// GetPathTemplate and GetHostTemplate returns the template used to match against for the route
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// GetPathTemplate returns the template used to build the
// route match.

@djgilcrease
Copy link
Contributor Author

All updated based on latest feedback

elithrar added a commit that referenced this pull request Mar 5, 2016
[feature] GetHostTemplate and GetPathTemplate return the template used to build the route.
@elithrar elithrar merged commit 147a95f into gorilla:master Mar 5, 2016
@elithrar
Copy link
Contributor

elithrar commented Mar 5, 2016

Merged. Thanks!

On Sat, Mar 5, 2016 at 3:28 AM Dj Gilcrease [email protected]
wrote:

All updated based on latest feedback


Reply to this email directly or view it on GitHub
#104 (comment).

@djgilcrease djgilcrease deleted the method-to-get-url-template branch March 5, 2016 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants