From 91c98bbb60ac41f1a985f5cb84b2de97bc77b008 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 24 Oct 2019 19:44:30 +0800 Subject: [PATCH] fix issue in group router feature for domain --- .example/other/test.go | 45 +++++++++++++++-- net/ghttp/ghttp_server_router_group.go | 4 ++ net/ghttp/ghttp_server_router_serve.go | 9 ++-- .../ghttp_unit_router_domain_basic_test.go | 48 ++++++++++++++++--- 4 files changed, 93 insertions(+), 13 deletions(-) diff --git a/.example/other/test.go b/.example/other/test.go index 43affb7d8..8e1037ace 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -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() } diff --git a/net/ghttp/ghttp_server_router_group.go b/net/ghttp/ghttp_server_router_group.go index 1486401d2..194d0aa0c 100644 --- a/net/ghttp/ghttp_server_router_group.go +++ b/net/ghttp/ghttp_server_router_group.go @@ -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 { diff --git a/net/ghttp/ghttp_server_router_serve.go b/net/ghttp/ghttp_server_router_serve.go index 2b9eb318a..139fe0fbc 100644 --- a/net/ghttp/ghttp_server_router_serve.go +++ b/net/ghttp/ghttp_server_router_serve.go @@ -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) } diff --git a/net/ghttp/ghttp_unit_router_domain_basic_test.go b/net/ghttp/ghttp_unit_router_domain_basic_test.go index 9647278c8..df2751c82 100644 --- a/net/ghttp/ghttp_unit_router_domain_basic_test.go +++ b/net/ghttp/ghttp_unit_router_domain_basic_test.go @@ -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") + }) +}