Skip to content

Commit

Permalink
refactor: 更新至 web 0.87.3
Browse files Browse the repository at this point in the history
同时对以下几个包从 Middleware 转换为 Plugin:access 和 health。
  • Loading branch information
caixw committed Mar 8, 2024
1 parent 5c5ed49 commit bb53254
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 29 deletions.
15 changes: 6 additions & 9 deletions access/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ import "github.com/issue9/web"
// l 表示记录输出的通道;
// format 表示记录的格式,接受三个参数,分别为状态码、请求方法和请求地址,
// 默认使用 "[%d] %s\t%s\n",可以使用 [fmt.Sprintf] 的顺序标记作调整;
func New(l *web.Logger, format string) web.Middleware {
func New(l *web.Logger, format string) web.Plugin {
const defaultFormat = "[%d] %s\t%s\n"
if format == "" {
format = defaultFormat
}

return web.MiddlewareFunc(func(next web.HandlerFunc) web.HandlerFunc {
return func(ctx *web.Context) web.Responser {
ctx.OnExit(func(ctx *web.Context, status int) {
r := ctx.Request()
l.Printf(format, status, r.Method, r.URL.String())
})
return next(ctx)
}
return web.PluginFunc(func(s web.Server) {
s.OnExitContext(func(ctx *web.Context, status int) {
r := ctx.Request()
l.Printf(format, status, r.Method, r.URL.String())
})
})
}
2 changes: 1 addition & 1 deletion access/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAccess(t *testing.T) {
r := srv.Routers().New("def", nil)
m := New(srv.Logs().ERROR(), "")
a.NotNil(m)
r.Use(m)
srv.Use(m)

wait := make(chan bool, 1)
r.Get("/test", func(ctx *web.Context) web.Responser {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/issue9/assert/v4 v4.1.1
github.com/issue9/cache v0.9.2
github.com/issue9/sliceutil v0.15.1
github.com/issue9/web v0.87.2
github.com/issue9/web v0.87.3
)

require (
Expand All @@ -17,7 +17,7 @@ require (
github.com/issue9/conv v1.3.5 // indirect
github.com/issue9/errwrap v0.3.2 // indirect
github.com/issue9/localeutil v0.26.4 // indirect
github.com/issue9/logs/v7 v7.4.3 // indirect
github.com/issue9/logs/v7 v7.5.0 // indirect
github.com/issue9/mux/v7 v7.4.1 // indirect
github.com/issue9/query/v3 v3.1.3 // indirect
github.com/issue9/rands/v2 v2.0.1 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/issue9/errwrap v0.3.2 h1:7KEme9Pfe75M+sIMcPCn/DV90wjnOcRbO4DXVAHj3Fw=
github.com/issue9/errwrap v0.3.2/go.mod h1:KcCLuUGiffjooLCUjL89r1cyO8/HT/VRcQrneO53N3A=
github.com/issue9/localeutil v0.26.4 h1:rfBC3YAt9PLHXHc5unreZpYjlEt+xCnVOUr7QDAveiw=
github.com/issue9/localeutil v0.26.4/go.mod h1:BJXJwcAT9CyyVZOlqfmq+B5FcPbqGxGjYnTYbVuiMM8=
github.com/issue9/logs/v7 v7.4.3 h1:ibEkWHRRz2mbughMfZvr4NcR3R9tv3THmge0tssQMqU=
github.com/issue9/logs/v7 v7.4.3/go.mod h1:uEIWEiDOgd92CWHkXpDX3DMyCicz4FuKmJ/whgC8OSU=
github.com/issue9/logs/v7 v7.5.0 h1:IAzqaRXuhNzTvoZVwFJWnWOxCiqhsiqxOt3GuAvJaWQ=
github.com/issue9/logs/v7 v7.5.0/go.mod h1:zPhcpznbDmfApbtrZHeHDvngfNAcuNhgo9pJfKD8nXY=
github.com/issue9/mux/v7 v7.4.1 h1:FG5iu44zd4GVIdzj7/oZgDYBaXnsRnrXWMZANcNhVIk=
github.com/issue9/mux/v7 v7.4.1/go.mod h1:rd0Oy/gYlxVRSNt3HYFtGWqXTfSMQWmP7+DEyEzvUUQ=
github.com/issue9/query/v3 v3.1.3 h1:Y6ETEYXxaKqhpM4lXPKCffhJ72VuKQbrAwgwHlacu0Y=
Expand All @@ -42,8 +42,8 @@ github.com/issue9/term/v3 v3.2.7 h1:esfhoinbQ65P3oFscXhticrDFOgZJQqUwL/IC70HiWc=
github.com/issue9/term/v3 v3.2.7/go.mod h1:DvA/fPiKzX11P/ZoVWJG5QMVpI0ia+uqiU31iZV2jHE=
github.com/issue9/unique/v2 v2.0.1 h1:Tdbq7hWZd7rvnf3ckUqzvEftOBWl1Z3S60Jk/zbPvyM=
github.com/issue9/unique/v2 v2.0.1/go.mod h1:oYIXt0BXX4tekc9+77oBu/ROsm7tr5kmD78XEqFoWwk=
github.com/issue9/web v0.87.2 h1:b6YWj65ZwLO1jicHy6qClVGuIJFMjhdwnVCXi2WTIxI=
github.com/issue9/web v0.87.2/go.mod h1:TjLPrNl/lxmhmn7I3pcRxk0I9hMmlDMRlHaWzPySujg=
github.com/issue9/web v0.87.3 h1:zuUzTpJCvEczOaX0AnXgNWFBimut7kjhUgaWBVKVJ+s=
github.com/issue9/web v0.87.3/go.mod h1:n9sIOcvdTbw1F5ME3ArYPbugH6mf7sqjXk+g0bT2nP4=
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
Expand Down
20 changes: 9 additions & 11 deletions health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ func newState(route, method, path string) *State {
// New 声明 [Health] 实例
func New(store Store) *Health { return &Health{Enabled: true, store: store} }

func (h *Health) Plugin(s web.Server) {
s.OnExitContext(func(ctx *web.Context, status int) {
if h.Enabled {
h.save(ctx, status)
}
})
}

// Register 注册一条路由项
//
// 这不是一个必须的操作。
// [web.Server] 的路由是可以动态加载的,无法预加载所有的路由项,
// 当路由项被第一次访问时,才会将该路由项的信息进行保存。
// 此操作可以让指定的路由项出现在 States() 中。
// 此操作可以让指定的路由项出现在 [Health.States] 中。
//
// NOTE: 只有在路由项还不存在于 [Health] 时才会填一个零值对象。
func (h *Health) Register(route, method, pattern string) {
Expand Down Expand Up @@ -92,16 +100,6 @@ func (h *Health) Fill(s web.Server) {
// States 返回所有的状态列表
func (h *Health) States() []*State { return h.store.All() }

// Middleware 将当前中间件应用于 next
func (h *Health) Middleware(next web.HandlerFunc) web.HandlerFunc {
return func(ctx *web.Context) web.Responser {
if h.Enabled {
ctx.OnExit(func(c *web.Context, status int) { h.save(c, status) })
}
return next(ctx)
}
}

func (h *Health) save(ctx *web.Context, status int) {
route := ctx.Route()
state := h.store.Get(route.RouterName(), ctx.Request().Method, route.Node().Pattern())
Expand Down
4 changes: 2 additions & 2 deletions health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/issue9/web/server/servertest"
)

var _ web.Middleware = &Health{}
var _ web.Plugin = &Health{}

func TestHealth(t *testing.T) {
a := assert.New(t, false)
Expand All @@ -39,8 +39,8 @@ func TestHealth(t *testing.T) {
a.NotError(err).NotNil(s)

h := New(NewCacheStore(s, "health_"))
s.Use(h)
r := s.Routers().New("def", nil)
r.Use(h)
r.Get("/", func(ctx *web.Context) web.Responser {
status, err := strconv.Atoi(ctx.Request().FormValue("status"))
if err != nil {
Expand Down

0 comments on commit bb53254

Please sign in to comment.