mirror of
https://github.com/gin-gonic/gin.git
synced 2026-01-11 00:56:57 +08:00
Merge bb8194b526c66fd73ec4f3ccf8903e0d84fd1ea0 into b57163a0e4339d7feb393ff430a454f4e448cf9c
This commit is contained in:
commit
e8fd2876eb
63
gin.go
63
gin.go
@ -210,6 +210,7 @@ func New() *Engine {
|
|||||||
|
|
||||||
// Default returns an Engine instance with the Logger and Recovery middleware already attached.
|
// Default returns an Engine instance with the Logger and Recovery middleware already attached.
|
||||||
func Default() *Engine {
|
func Default() *Engine {
|
||||||
|
println("hello hsy")
|
||||||
debugPrintWARNINGDefault()
|
debugPrintWARNINGDefault()
|
||||||
engine := New()
|
engine := New()
|
||||||
engine.Use(Logger(), Recovery())
|
engine.Use(Logger(), Recovery())
|
||||||
@ -340,6 +341,68 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) delRoute(method, path string) {
|
||||||
|
assert1(path[0] == '/', "path must begin with '/'")
|
||||||
|
assert1(method != "", "HTTP method can not be empty")
|
||||||
|
|
||||||
|
root := engine.trees.get(method)
|
||||||
|
if root == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
target ,parent ,ok ,myself:= root.findNode(path)
|
||||||
|
if !ok{
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !myself {
|
||||||
|
parent.delChildNode(target)
|
||||||
|
}else {
|
||||||
|
root.delChildNode(target)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) findNode(path string) (target *node,parent *node,found bool,myself bool){
|
||||||
|
if n.fullPath == path {
|
||||||
|
return n,nil,true,true
|
||||||
|
}
|
||||||
|
for _,child := range n.children {
|
||||||
|
if t,p,ok,m := child.findNode(path); ok {
|
||||||
|
if m {
|
||||||
|
return t,n,ok,false
|
||||||
|
}
|
||||||
|
return t,p,ok,false
|
||||||
|
}else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil,nil,false,false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *node) delChildNode(target *node){
|
||||||
|
switch len(target.children) {
|
||||||
|
case 0:
|
||||||
|
for i, max := 0, len(n.indices); i < max; i++ {
|
||||||
|
if target.path[0] == n.indices[i] {
|
||||||
|
n.indices = n.indices[:i] + n.indices[i+1:]
|
||||||
|
n.children = append(n.children[:i], n.children[i+1:]...)
|
||||||
|
n.priority --
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
for i, max := 0, len(n.indices); i < max; i++ {
|
||||||
|
if target.path[0] == n.indices[i] {
|
||||||
|
n.indices = n.indices[:i] + string(target.children[0].path[0]) + n.indices[i+1:]
|
||||||
|
n.children[i] = target.children[0]
|
||||||
|
n.priority --
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
target.handlers = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Routes returns a slice of registered routes, including some useful information, such as:
|
// Routes returns a slice of registered routes, including some useful information, such as:
|
||||||
// the http method, path and the handler name.
|
// the http method, path and the handler name.
|
||||||
func (engine *Engine) Routes() (routes RoutesInfo) {
|
func (engine *Engine) Routes() (routes RoutesInfo) {
|
||||||
|
|||||||
@ -89,6 +89,20 @@ func (group *RouterGroup) handle(httpMethod, relativePath string, handlers Handl
|
|||||||
return group.returnObj()
|
return group.returnObj()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (group *RouterGroup) unhandle(httpMethod, relativePath string) IRoutes {
|
||||||
|
absolutePath := group.calculateAbsolutePath(relativePath)
|
||||||
|
group.engine.delRoute(httpMethod, absolutePath)
|
||||||
|
return group.returnObj()
|
||||||
|
}
|
||||||
|
|
||||||
|
// REMOVE is the way to delete route
|
||||||
|
func (group *RouterGroup) REMOVE(httpMethod,relativePath string) IRoutes {
|
||||||
|
if matched := regEnLetter.MatchString(httpMethod); !matched {
|
||||||
|
panic("http method " + httpMethod + " is not valid")
|
||||||
|
}
|
||||||
|
return group.unhandle(httpMethod, relativePath)
|
||||||
|
}
|
||||||
|
|
||||||
// Handle registers a new request handle and middleware with the given path and method.
|
// Handle registers a new request handle and middleware with the given path and method.
|
||||||
// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
|
// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
|
||||||
// See the example code in GitHub.
|
// See the example code in GitHub.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user