Skip to content

Commit

Permalink
feat(middlewares/skip): 添加 skip
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Mar 24, 2024
1 parent 9bcdcaa commit 3a7f3c5
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
- auth/session session 管理;
- acl/ratelimit x-rate-limit 的相关实现;
- acl/iplist 黑白名单;
- acl/rbac 简单的 RBAC 管理;
- skip 根据条件跳过路由的执行;

## 安装

Expand Down
11 changes: 7 additions & 4 deletions locales/und.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ messages:
- key: enable compression base on cpu used
message:
msg: enable compression base on cpu used
- key: gen session id
message:
msg: gen session id
- key: invalid ip %s
message:
msg: invalid ip %s
Expand All @@ -18,12 +21,12 @@ messages:
- key: not found resource %s
message:
msg: not found resource %s
- key: not found the context session key
- key: session id not exists in context
message:
msg: not found the context session key
- key: the client Authorization header %s is invalid JWT format
msg: session id not exists in context
- key: the client %s header %s is invalid format
message:
msg: the client Authorization header %s is invalid JWT format
msg: the client %s header %s is invalid format
- key: the role %s has children role, can not deleted
message:
msg: the role %s has children role, can not deleted
Expand Down
11 changes: 7 additions & 4 deletions locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ messages:
- key: enable compression base on cpu used
message:
msg: 基于 CPU 使用率决定是否启用压缩功能:w
- key: gen session id
message:
msg: 生成 session id
- key: invalid ip %s
message:
msg: 无效的 IP 地址 %s
Expand All @@ -18,12 +21,12 @@ messages:
- key: not found resource %s
message:
msg: 未定义的资源 %s
- key: not found the context session key
- key: session id not exists in context
message:
msg: 在当前对话中未找到相关的 session 数据
- key: the client Authorization header %s is invalid JWT format
msg: 当前对话中未找到 session id
- key: the client %s header %s is invalid format
message:
msg: 客户端提交到 Authorization 报头的 JWT 格式错误
msg: 客户端的请求报头 %s 提交的数据 %s 格式错误
- key: the role %s has children role, can not deleted
message:
msg: 不能删除拥有子角色的角色 %s
Expand Down
2 changes: 2 additions & 0 deletions middlewares/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

// Auth 登录凭证的验证接口
//
// T 表示每次验证后,附加在 [web.Context] 上的数据。
type Auth[T any] interface {
web.Middleware

Expand Down
22 changes: 22 additions & 0 deletions middlewares/skip/skip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-FileCopyrightText: 2024 caixw
//
// SPDX-License-Identifier: MIT

// Package skip 是否根据条件跳过路由的执行
package skip

import "github.com/issue9/web"

// New 只有在 cond 为 true 时才执行路由
//
// problemID 表示在条件为 false 时返回的错误码。
func New(cond func(*web.Context) bool, problemID string) web.Middleware {
return web.MiddlewareFunc(func(next web.HandlerFunc) web.HandlerFunc {
return func(ctx *web.Context) web.Responser {
if cond(ctx){
return next(ctx)
}
return ctx.Problem(problemID)
}
})
}
47 changes: 47 additions & 0 deletions middlewares/skip/skip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2024 caixw
//
// SPDX-License-Identifier: MIT

package skip

import (
"net/http"
"os"
"testing"

"github.com/issue9/assert/v4"
"github.com/issue9/web"
"github.com/issue9/web/server"
"github.com/issue9/web/server/servertest"
)

func TestSkip(t *testing.T) {
a := assert.New(t, false)

s, err := server.New("test", "1.0.0", &server.Options{
HTTPServer: &http.Server{Addr: ":8080"},
Mimetypes: server.JSONMimetypes(),
Logs: &server.Logs{Handler: server.NewTermHandler(os.Stderr, nil)},
})
a.NotError(err).NotNil(s)

next := func(ctx *web.Context) web.Responser {
return web.Created(nil, "")
}

router := s.Routers().New("def", nil)
router.Any("/test", New(func(ctx *web.Context) bool { return ctx.Request().Method != http.MethodHead }, web.ProblemBadRequest).Middleware(next))

defer servertest.Run(a, s)()
defer s.Close(0)

servertest.Get(a, "http://localhost:8080/test").
Header("X-Forwarded-For", "192.168.1.1").
Do(nil).
Status(http.StatusCreated)

servertest.NewRequest(a, http.MethodHead, "http://localhost:8080/test").
Header("X-Forwarded-For", "192.168.1.1").
Do(nil).
Status(http.StatusBadRequest)
}

0 comments on commit 3a7f3c5

Please sign in to comment.