Merge branch 'routes-list'
This commit is contained in:
36
gin.go
36
gin.go
@ -61,8 +61,22 @@ type (
|
||||
HandleMethodNotAllowed bool
|
||||
ForwardedByClientIP bool
|
||||
}
|
||||
|
||||
RouteInfo struct {
|
||||
Method string
|
||||
Path string
|
||||
Handler string
|
||||
}
|
||||
)
|
||||
|
||||
func (c HandlersChain) Last() HandlerFunc {
|
||||
length := len(c)
|
||||
if length > 0 {
|
||||
return c[length-1]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns a new blank Engine instance without any middleware attached.
|
||||
// The most basic configuration
|
||||
func New() *Engine {
|
||||
@ -181,6 +195,28 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
||||
root.addRoute(path, handlers)
|
||||
}
|
||||
|
||||
func (engine *Engine) Routes() (routes []RouteInfo) {
|
||||
for _, tree := range engine.trees {
|
||||
routes = iterate("", tree.method, routes, tree.root)
|
||||
}
|
||||
return routes
|
||||
}
|
||||
|
||||
func iterate(path, method string, routes []RouteInfo, root *node) []RouteInfo {
|
||||
path += root.path
|
||||
if len(root.handlers) > 0 {
|
||||
routes = append(routes, RouteInfo{
|
||||
Method: method,
|
||||
Path: path,
|
||||
Handler: nameOfFunction(root.handlers.Last()),
|
||||
})
|
||||
}
|
||||
for _, node := range root.children {
|
||||
routes = iterate(path, method, routes, node)
|
||||
}
|
||||
return routes
|
||||
}
|
||||
|
||||
// The router is attached to a http.Server and starts listening and serving HTTP requests.
|
||||
// It is a shortcut for http.ListenAndServe(addr, router)
|
||||
// Note: this method will block the calling goroutine undefinitelly unless an error happens.
|
||||
|
Reference in New Issue
Block a user