[gogf/gf]建议 路由组prefix前缀可以做为可选字段

2024-06-25 506 views
0

QIKP10.png

如图所示,中间件Auth和Limit只想在/user和/team下使用,又不想把Auth和Limit多写几遍(在每个模块下) 但如果按图里面的写法,/auth模块下也会受到中间件auth和limit的作用

回答

5

@mingzaily 你好,Group的第一个参数可以使用/,参考以下单元测试代码:

func MiddlewareScope1(r *ghttp.Request) {
    r.Response.Write("a")
    r.Middleware.Next()
    r.Response.Write("b")
}

func MiddlewareScope2(r *ghttp.Request) {
    r.Response.Write("c")
    r.Middleware.Next()
    r.Response.Write("d")
}

func MiddlewareScope3(r *ghttp.Request) {
    r.Response.Write("e")
    r.Middleware.Next()
    r.Response.Write("f")
}

func Test_Middleware_Scope(t *testing.T) {
    p := ports.PopRand()
    s := g.Server(p)
    s.Group("/", func(group *ghttp.RouterGroup) {
        group.Middleware(MiddlewareScope1)
        group.ALL("/scope1", func(r *ghttp.Request) {
            r.Response.Write("1")
        })
        group.Group("/", func(group *ghttp.RouterGroup) {
            group.Middleware(MiddlewareScope2)
            group.ALL("/scope2", func(r *ghttp.Request) {
                r.Response.Write("2")
            })
        })
        group.Group("/", func(group *ghttp.RouterGroup) {
            group.Middleware(MiddlewareScope3)
            group.ALL("/scope3", func(r *ghttp.Request) {
                r.Response.Write("3")
            })
        })
    })
    s.SetPort(p)
    //s.SetDumpRouterMap(false)
    s.Start()
    defer s.Shutdown()
    time.Sleep(100 * time.Millisecond)
    gtest.Case(t, func() {
        client := ghttp.NewClient()
        client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))

        gtest.Assert(client.GetContent("/"), "Not Found")
        gtest.Assert(client.GetContent("/scope1"), "a1b")
        gtest.Assert(client.GetContent("/scope2"), "ac2db")
        gtest.Assert(client.GetContent("/scope3"), "ae3fb")
    })
}

路由信息如下:

  SERVER | ADDRESS | DOMAIN  | METHOD | P |  ROUTE  |                              HANDLER                              |                       MIDDLEWARE                         
|--------|---------|---------|--------|---|---------|-------------------------------------------------------------------|---------------------------------------------------------|
   8929  |  :8929  | default |  ALL   | 1 | /scope1 | github.com/gogf/gf/net/ghttp_test.Test_Middleware_Scope.func1.1   | ghttp_test.MiddlewareScope1                              
|--------|---------|---------|--------|---|---------|-------------------------------------------------------------------|---------------------------------------------------------|
   8929  |  :8929  | default |  ALL   | 1 | /scope2 | github.com/gogf/gf/net/ghttp_test.Test_Middleware_Scope.func1.2.1 | ghttp_test.MiddlewareScope1,ghttp_test.MiddlewareScope2  
|--------|---------|---------|--------|---|---------|-------------------------------------------------------------------|---------------------------------------------------------|
   8929  |  :8929  | default |  ALL   | 1 | /scope3 | github.com/gogf/gf/net/ghttp_test.Test_Middleware_Scope.func1.3.1 | ghttp_test.MiddlewareScope1,ghttp_test.MiddlewareScope3  
|--------|---------|---------|--------|---|---------|-------------------------------------------------------------------|---------------------------------------------------------|
3

QTkTNq.png

 SERVER  | ADDRESS | DOMAIN  | METHOD | P |         ROUTE          |                           HANDLER                           | MIDDLEWARE  
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | GET    | 1 | /                      | fras-go/router.init.0.func1.1                               |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | ALL    | 5 | /*                     | fras-go/router.MiddlewareCORS                               | MIDDLEWARE  
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | ALL    | 4 | /*                     | fras-go/router.MiddlewareError                              | MIDDLEWARE  
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | ALL    | 3 | /*                     | fras-go/router.MiddlewareAjax                               | MIDDLEWARE  
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | ALL    | 2 | /*                     | fras-go/router.MiddlewareAuth                               | MIDDLEWARE  
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | ALL    | 1 | /*                     | fras-go/router.MiddlewareLimit                              | MIDDLEWARE  
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | POST   | 2 | /auth/login            | github.com/gogf/gf-jwt.(*GfJWTMiddleware).LoginHandler-fm   |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | POST   | 2 | /auth/refresh          | github.com/gogf/gf-jwt.(*GfJWTMiddleware).RefreshHandler-fm |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | POST   | 2 | /auth/register         | fras-go/app/controller/user.Register                        |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | POST   | 2 | /auth/verificationCode | fras-go/app/controller/verification.SmsCode                 |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | POST   | 2 | /user/avatar           | fras-go/app/controller/user.UpdateAvatar                    |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | POST   | 2 | /user/face             | fras-go/app/controller/user.UpdateFace                      |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | GET    | 2 | /user/info             | fras-go/app/controller/user.GetInfo                         |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | PUT    | 2 | /user/nickname         | fras-go/app/controller/user.UpdateNickName                  |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | PUT    | 2 | /user/passport         | fras-go/app/controller/user.UpdatePassport                  |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|
  default |  :8199  | default | PUT    | 2 | /user/password         | fras-go/app/controller/user.UpdatePassword                  |             
|---------|---------|---------|--------|---|------------------------|-------------------------------------------------------------|------------|

放在user模块的中间件被加载到了根路由,要怎么改呢

0

@mingzaily 你用的gf版本多少?

0

@mingzaily 你用的gf版本多少?

github.com/gogf/gf v1.9.11-0.20191201134129-10451864e6ce
2

@mingzaily 更新到最新的v1.10.0再试试

8

可以了,就是每个路由要是中间件多了,排版有点难看😂

3

@mingzaily 你可以把路由注册代码贴出来我看看,有无排版优化空间

9

也不算排版问题吧,主要就是屏幕不够大,导致中间件会到第二行

8

@mingzaily 你可以换行啊,也可以使用多个Middleware方法来分开注册。

2

嗯嗯

5

ports.PopRand 在哪里?