3d481178 by astaxie

improve router

1 parent d0cb112f
Showing 1 changed file with 10 additions and 8 deletions
...@@ -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)
383 findrouter = true 383 findrouter = true
384 break 384 break
385 } 385 }
386 386 // pattern /admin url /admin 200 /admin/ 404
387 if (requestPath[n-1] != '/' && route.pattern == requestPath) || 387 // pattern /admin/ url /admin 301 /admin/ 200
388 (requestPath[n-1] == '/' && len(route.pattern) >= n-1 && requestPath[0:n-1] == route.pattern) || 388 if requestPath[n-1] != '/' && len(route.pattern) == n+1 &&
389 (len(route.pattern) == n+1 && requestPath == route.pattern[0:n]) { 389 route.pattern[n] == '/' && route.pattern[:n-1] == requestPath {
390 runrouter = route 390 http.Redirect(w, r, requestPath+"/", 301)
391 findrouter = true 391 return
392 break
393 } 392 }
394 } 393 }
395 394
...@@ -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)
545 544
546 if !findrouter { 545 if !findrouter {
547 for cName, methodmap := range p.autoRouter { 546 for cName, methodmap := range p.autoRouter {
548 if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName) { 547 if strings.ToLower(requestPath) == "/"+cName+"/" {
548 requestPath = requestPath + "index"
549 }
550 if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") {
549 for mName, controllerType := range methodmap { 551 for mName, controllerType := range methodmap {
550 if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) { 552 if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) {
551 //parse params 553 //parse params
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!