Commit 3d481178 authored by astaxie's avatar astaxie

improve router

parent d0cb112f
...@@ -383,13 +383,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -383,13 +383,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
findrouter = true findrouter = true
break break
} }
// pattern /admin url /admin 200 /admin/ 404
if (requestPath[n-1] != '/' && route.pattern == requestPath) || // pattern /admin/ url /admin 301 /admin/ 200
(requestPath[n-1] == '/' && len(route.pattern) >= n-1 && requestPath[0:n-1] == route.pattern) || if requestPath[n-1] != '/' && len(route.pattern) == n+1 &&
(len(route.pattern) == n+1 && requestPath == route.pattern[0:n]) { route.pattern[n] == '/' && route.pattern[:n-1] == requestPath {
runrouter = route http.Redirect(w, r, requestPath+"/", 301)
findrouter = true return
break
} }
} }
...@@ -545,7 +544,10 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -545,7 +544,10 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
if !findrouter { if !findrouter {
for cName, methodmap := range p.autoRouter { for cName, methodmap := range p.autoRouter {
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName) { if strings.ToLower(requestPath) == "/"+cName+"/" {
requestPath = requestPath + "index"
}
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") {
for mName, controllerType := range methodmap { for mName, controllerType := range methodmap {
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) { if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) {
//parse params //parse params
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment