fix issue in group router feature for domain

This commit is contained in:
John 2019-10-24 19:44:30 +08:00
parent c0236d7dfa
commit 91c98bbb60
4 changed files with 93 additions and 13 deletions

View File

@ -1,10 +1,47 @@
package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
func main() {
fmt.Println('\f')
fmt.Println(0xA0)
func loadRouter(domain *ghttp.Domain) {
domain.Group("/", func(g *ghttp.RouterGroup) {
g.Group("/app", func(gApp *ghttp.RouterGroup) {
// 该路由规则仅会在GET请求下有效
gApp.GET("/{table}/list/{page}.html", func(r *ghttp.Request) {
r.Response.WriteJson(r.Router)
})
// 该路由规则仅会在GET请求及localhost域名下有效
gApp.GET("/order/info/{order_id}", func(r *ghttp.Request) {
r.Response.WriteJson(r.Router)
})
// 该路由规则仅会在DELETE请求下有效
gApp.DELETE("/comment/{id}", func(r *ghttp.Request) {
r.Response.WriteJson(r.Router)
})
})
// 该路由规则仅会在GET请求下有效
g.GET("/{table}/list/{page}.html", func(r *ghttp.Request) {
r.Response.WriteJson(r.Router)
})
// 该路由规则仅会在GET请求及localhost域名下有效
g.GET("/order/info/{order_id}", func(r *ghttp.Request) {
r.Response.WriteJson(r.Router)
})
// 该路由规则仅会在DELETE请求下有效
g.DELETE("/comment/{id}", func(r *ghttp.Request) {
r.Response.WriteJson(r.Router)
})
})
}
func main() {
s := g.Server()
domain := s.Domain("localhost")
loadRouter(domain)
s.SetPort(8199)
s.Run()
}

View File

@ -254,6 +254,10 @@ func (g *RouterGroup) doBind(bindType string, pattern string, object interface{}
if err != nil {
glog.Fatalf("invalid pattern: %s", pattern)
}
// If there'a already a domain, unset the domain field in the pattern.
if g.domain != nil {
domain = ""
}
if bindType == "REST" {
pattern = prefix + "/" + strings.TrimLeft(path, "/")
} else {

View File

@ -208,8 +208,11 @@ func (item *handlerParsedItem) MarshalJSON() ([]byte, error) {
// 生成回调方法查询的Key
func (s *Server) serveHandlerKey(method, path, domain string) string {
if method == "" {
return path + "@" + strings.ToLower(domain)
if len(domain) > 0 {
domain = "@" + domain
}
return strings.ToUpper(method) + ":" + path + "@" + strings.ToLower(domain)
if method == "" {
return path + strings.ToLower(domain)
}
return strings.ToUpper(method) + ":" + path + strings.ToLower(domain)
}

View File

@ -4,7 +4,6 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// 基本路由功能以及优先级测试
package ghttp_test
import (
@ -17,7 +16,6 @@ import (
"github.com/gogf/gf/test/gtest"
)
// 基本路由功能测试
func Test_Router_DomainBasic(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
@ -69,7 +67,6 @@ func Test_Router_DomainBasic(t *testing.T) {
})
}
// 测试HTTP Method注册.
func Test_Router_DomainMethod(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
@ -162,7 +159,6 @@ func Test_Router_DomainMethod(t *testing.T) {
})
}
// 测试状态返回.
func Test_Router_DomainStatus(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
@ -259,7 +255,6 @@ func Test_Router_DomainStatus(t *testing.T) {
})
}
// 自定义状态码处理.
func Test_Router_DomainCustomStatusHandler(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
@ -299,7 +294,6 @@ func Test_Router_DomainCustomStatusHandler(t *testing.T) {
})
}
// 测试不存在的路由.
func Test_Router_Domain404(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
@ -332,3 +326,45 @@ func Test_Router_Domain404(t *testing.T) {
gtest.Assert(client.GetContent("/"), "hello")
})
}
func Test_Router_DomainGroup(t *testing.T) {
p := ports.PopRand()
s := g.Server(p)
d := s.Domain("localhost, local")
d.Group("/", func(g *ghttp.RouterGroup) {
g.Group("/app", func(gApp *ghttp.RouterGroup) {
gApp.GET("/{table}/list/{page}.html", func(r *ghttp.Request) {
r.Response.Write(r.Get("table"), "&", r.Get("page"))
})
gApp.GET("/order/info/{order_id}", func(r *ghttp.Request) {
r.Response.Write(r.Get("order_id"))
})
gApp.DELETE("/comment/{id}", func(r *ghttp.Request) {
r.Response.Write(r.Get("id"))
})
})
})
s.SetPort(p)
s.SetDumpRouteMap(false)
s.Start()
defer s.Shutdown()
gtest.Case(t, func() {
client1 := ghttp.NewClient()
client1.SetPrefix(fmt.Sprintf("http://local:%d", p))
client2 := ghttp.NewClient()
client2.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
gtest.Assert(client1.GetContent("/app/t/list/2.html"), "t&2")
gtest.Assert(client2.GetContent("/app/t/list/2.html"), "Not Found")
gtest.Assert(client1.GetContent("/app/order/info/2"), "2")
gtest.Assert(client2.GetContent("/app/order/info/2"), "Not Found")
gtest.Assert(client1.GetContent("/app/comment/20"), "Not Found")
gtest.Assert(client2.GetContent("/app/comment/20"), "Not Found")
gtest.Assert(client1.DeleteContent("/app/comment/20"), "20")
gtest.Assert(client2.DeleteContent("/app/comment/20"), "Not Found")
})
}