mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-09-19 01:36:40 +08:00
优化请求hook处理
This commit is contained in:
parent
1efbf698e2
commit
f7307e4fd4
@ -7,11 +7,13 @@
|
|||||||
package hook
|
package hook
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
"hotgo/internal/library/contexts"
|
"hotgo/internal/library/contexts"
|
||||||
"hotgo/internal/service"
|
"hotgo/internal/service"
|
||||||
|
"hotgo/utility/simple"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AccessLog 访问日志
|
// AccessLog 访问日志
|
||||||
@ -29,9 +31,9 @@ func (s *sHook) AccessLog(r *ghttp.Request) {
|
|||||||
// 计算运行耗时
|
// 计算运行耗时
|
||||||
contexts.SetTakeUpTime(ctx, gtime.TimestampMilli()-r.EnterTime)
|
contexts.SetTakeUpTime(ctx, gtime.TimestampMilli()-r.EnterTime)
|
||||||
|
|
||||||
go func() {
|
simple.SafeGo(ctx, func(ctx context.Context) {
|
||||||
if err := service.SysLog().AutoLog(ctx); err != nil {
|
if err := service.SysLog().AutoLog(ctx); err != nil {
|
||||||
g.Log().Info(ctx, "hook AccessLog err:", err)
|
g.Log().Warningf(ctx, "hook AccessLog err:%+v", err)
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
"github.com/gogf/gf/v2/os/gtimer"
|
"github.com/gogf/gf/v2/os/gtimer"
|
||||||
"hotgo/internal/library/contexts"
|
"hotgo/internal/library/contexts"
|
||||||
|
"hotgo/utility/simple"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -69,12 +70,15 @@ func (s *sHook) LastActive(r *ghttp.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if allow(memberId) {
|
if allow(memberId) {
|
||||||
|
simple.SafeGo(ctx, func(ctx context.Context) {
|
||||||
_, err := g.Model("admin_member").Ctx(ctx).
|
_, err := g.Model("admin_member").Ctx(ctx).
|
||||||
Where("id", memberId).
|
Where("id", memberId).
|
||||||
|
WhereLT("last_active_at", gtime.Now()).
|
||||||
Data(g.Map{"last_active_at": gtime.Now()}).
|
Data(g.Map{"last_active_at": gtime.Now()}).
|
||||||
Update()
|
Update()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log().Warningf(ctx, "hook LastActive err:%+v, memberId:%v", err, memberId)
|
g.Log().Warningf(ctx, "hook LastActive err:%+v, memberId:%v", err, memberId)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,29 +104,40 @@ func (s *sSysLog) RealWrite(ctx context.Context, commonLog entity.SysLog) (err e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AutoLog 根据配置自动记录请求日志
|
// AutoLog 根据配置自动记录请求日志
|
||||||
func (s *sSysLog) AutoLog(ctx context.Context) (err error) {
|
func (s *sSysLog) AutoLog(ctx context.Context) error {
|
||||||
|
return g.Try(ctx, func(ctx context.Context) {
|
||||||
|
var err error
|
||||||
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
config, err := service.SysConfig().GetLoadLog(ctx)
|
config, err := service.SysConfig().GetLoadLog(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !config.Switch {
|
if !config.Switch {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := s.AnalysisLog(ctx)
|
data := s.AnalysisLog(ctx)
|
||||||
if ok := validate.InSliceExistStr(config.Module, data.Module); !ok {
|
if ok := validate.InSliceExistStr(config.Module, data.Module); !ok {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ok := validate.InSliceExistStr(config.SkipCode, gconv.String(data.ErrorCode)); ok {
|
if ok := validate.InSliceExistStr(config.SkipCode, gconv.String(data.ErrorCode)); ok {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.Queue {
|
if config.Queue {
|
||||||
return queue.Push(consts.QueueLogTopic, data)
|
err = queue.Push(consts.QueueLogTopic, data)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return s.RealWrite(ctx, data)
|
err = s.RealWrite(ctx, data)
|
||||||
|
return
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnalysisLog 解析日志数据
|
// AnalysisLog 解析日志数据
|
||||||
|
@ -82,7 +82,7 @@
|
|||||||
<n-gi>
|
<n-gi>
|
||||||
<n-form-item label="绑定角色" path="roleId">
|
<n-form-item label="绑定角色" path="roleId">
|
||||||
<n-tree-select
|
<n-tree-select
|
||||||
:default-value="formParams.roleId"
|
:default-value="formParams.roleId.toString()"
|
||||||
:options="roleList"
|
:options="roleList"
|
||||||
@update:value="handleUpdateRoleValue"
|
@update:value="handleUpdateRoleValue"
|
||||||
:default-expand-all="true"
|
:default-expand-all="true"
|
||||||
|
Loading…
Reference in New Issue
Block a user