Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #109 from gobuffalo/pathinfo
Browse files Browse the repository at this point in the history
Return the RouteInfo when mapping an endpoint. Also make it available in the request context
  • Loading branch information
markbates committed Jan 10, 2017
2 parents 1951856 + 34ca4b9 commit 9ba92dd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
5 changes: 3 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
*/
type Handler func(Context) error

func (a *App) handlerToHandler(h Handler) http.Handler {
func (a *App) handlerToHandler(info RouteInfo, h Handler) http.Handler {
hf := func(res http.ResponseWriter, req *http.Request) {
ws := res.(*buffaloResponse)
params := req.URL.Query()
Expand All @@ -44,7 +44,8 @@ func (a *App) handlerToHandler(h Handler) http.Handler {
session: a.getSession(req, ws),
notFound: a.notFound(),
data: map[string]interface{}{
"routes": a.Routes(),
"routes": a.Routes(),
"current_route": info,
},
}

Expand Down
16 changes: 11 additions & 5 deletions route.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package buffalo

import "github.com/gorilla/mux"

// Routes returns a list of all of the routes defined
// in this application.
func (a *App) Routes() RouteList {
Expand All @@ -9,16 +11,20 @@ func (a *App) Routes() RouteList {
return a.routes
}

type route struct {
Method string `json:"method"`
Path string `json:"path"`
HandlerName string `json:"handler"`
// RouteInfo provides information about the underlying route that
// was built.
type RouteInfo struct {
Method string `json:"method"`
Path string `json:"path"`
HandlerName string `json:"handler"`
MuxRoute *mux.Route `json:"-"`
Handler Handler `json:"-"`
}

// RouteList contains a mapping of the routes defined
// in the application. This listing contains, Method, Path,
// and the name of the Handler defined to process that route.
type RouteList []route
type RouteList []RouteInfo

func (a RouteList) Len() int { return len(a) }
func (a RouteList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
Expand Down
42 changes: 23 additions & 19 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@ import (
)

// GET maps an HTTP "GET" request to the path and the specified handler.
func (a *App) GET(p string, h Handler) {
a.addRoute("GET", p, h)
func (a *App) GET(p string, h Handler) RouteInfo {
return a.addRoute("GET", p, h)
}

// POST maps an HTTP "POST" request to the path and the specified handler.
func (a *App) POST(p string, h Handler) {
a.addRoute("POST", p, h)
func (a *App) POST(p string, h Handler) RouteInfo {
return a.addRoute("POST", p, h)
}

// PUT maps an HTTP "PUT" request to the path and the specified handler.
func (a *App) PUT(p string, h Handler) {
a.addRoute("PUT", p, h)
func (a *App) PUT(p string, h Handler) RouteInfo {
return a.addRoute("PUT", p, h)
}

// DELETE maps an HTTP "DELETE" request to the path and the specified handler.
func (a *App) DELETE(p string, h Handler) {
a.addRoute("DELETE", p, h)
func (a *App) DELETE(p string, h Handler) RouteInfo {
return a.addRoute("DELETE", p, h)
}

// HEAD maps an HTTP "HEAD" request to the path and the specified handler.
func (a *App) HEAD(p string, h Handler) {
a.addRoute("HEAD", p, h)
func (a *App) HEAD(p string, h Handler) RouteInfo {
return a.addRoute("HEAD", p, h)
}

// OPTIONS maps an HTTP "OPTIONS" request to the path and the specified handler.
func (a *App) OPTIONS(p string, h Handler) {
a.addRoute("OPTIONS", p, h)
func (a *App) OPTIONS(p string, h Handler) RouteInfo {
return a.addRoute("OPTIONS", p, h)
}

// PATCH maps an HTTP "PATCH" request to the path and the specified handler.
func (a *App) PATCH(p string, h Handler) {
a.addRoute("PATCH", p, h)
func (a *App) PATCH(p string, h Handler) RouteInfo {
return a.addRoute("PATCH", p, h)
}

// ServeFiles maps an path to a directory on disk to serve static files.
Expand Down Expand Up @@ -122,25 +122,29 @@ func (a *App) Group(path string) *App {
return g
}

func (a *App) addRoute(method string, url string, h Handler) {
func (a *App) addRoute(method string, url string, h Handler) RouteInfo {
a.moot.Lock()
defer a.moot.Unlock()

url = path.Join(a.prefix, url)
hs := funcKey(h)
routes := a.Routes()
routes = append(routes, route{
r := RouteInfo{
Method: method,
Path: url,
HandlerName: hs,
})
Handler: h,
}

r.MuxRoute = a.router.Handle(url, a.handlerToHandler(r, h)).Methods(method)

routes := a.Routes()
routes = append(routes, r)
sort.Sort(routes)
if a.root != nil {
a.root.routes = routes
} else {
a.routes = routes
}

a.router.Handle(url, a.handlerToHandler(h)).Methods(method)
return r
}

0 comments on commit 9ba92dd

Please sign in to comment.