Skip to content

Added a Print function on router that prints routing paths #77

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

Closed
wants to merge 1 commit into from
Closed

Added a Print function on router that prints routing paths #77

wants to merge 1 commit into from

Conversation

markbates
Copy link
Contributor

Very similar to using rake routes in Ruby on Rails, an application can use this print out a listing of the routes in their application.

Example application:

package main

import (
    "net/http"
    "os"

    "github.com/labstack/echo"
)

func main() {
    e := echo.New()

    e.Get("/foo", func(res http.ResponseWriter, req *http.Request) {})
    e.Put("/foo", func(res http.ResponseWriter, req *http.Request) {})
    e.Get("/foo/:id", func(res http.ResponseWriter, req *http.Request) {})
    e.Get("/bar/baz/:id", func(res http.ResponseWriter, req *http.Request) {})
    e.Get("/:controller/:action/:id", func(res http.ResponseWriter, req *http.Request) {})

    g := e.Group("/api/v1")
    g.Get("/users/:id", func(res http.ResponseWriter, req *http.Request) {})
    g.Get("/users/:id/edit", func(res http.ResponseWriter, req *http.Request) {})
    g.Post("/users/:id", func(res http.ResponseWriter, req *http.Request) {})

    e.Router.Print(os.Stdout)
}

Example output*:

GET /foo
    /foo/:id
    /bar/baz/:id
    /:controller/:action/:id
    /api/v1/users/:id
    /api/v1/users/:id/edit
POST    /api/v1/users/:id
PUT /foo

*please excuse the formatting on GitHub, it apparently doesn't care much for tabs.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.43%) to 89.82% when pulling d9c47b6 on markbates:print into 1e11762 on labstack:master.

@vishr
Copy link
Member

vishr commented May 29, 2015

@markbates We already have a data structure which holds all the routes. It is used by Echo.URI API. I will leverage from your code and expose an API. Let me know your thoughts.

@markbates
Copy link
Contributor Author

Ultimately it would be great to have access to a simple version of the route information, like I have above. An API to gain access to that would be great so people can build middleware, and other extensions around it.

For example, had there an API for routes I would've written a handler that would generate this type of print out.

Another example would be to build route helpers, like you'd find in frameworks such as Rails e.g. generate the final href string given the name of the route and parameter values.

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

Successfully merging this pull request may close these issues.

3 participants