feat: add Go backend

This commit is contained in:
richjohnrambo
2026-10-10 20:56:17 +08:00
parent 50fcfd2d3d
commit f6024f0964
1040 changed files with 107169 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
// Package hook
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package hook
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/text/gstr"
"hotgo/internal/library/contexts"
"hotgo/internal/service"
"hotgo/utility/simple"
"strings"
)
// 忽略的请求方式
var ignoredRequestMethods = []string{"HEAD", "PRI"}
// accessLog 访问日志
func (s *sHook) accessLog(r *ghttp.Request) {
if s.isIgnoredRequest(r) {
return
}
var ctx = r.Context()
if contexts.Get(ctx) == nil {
return
}
contexts.SetDataMap(ctx, g.Map{
"request.takeUpTime": gtime.Now().Sub(gtime.New(r.EnterTime)).Milliseconds(),
// ...
})
simple.SafeGo(ctx, func(ctx context.Context) {
if err := service.SysLog().AutoLog(ctx); err != nil {
g.Log().Infof(ctx, "hook accessLog err:%+v", err)
}
})
}
// isIgnoredRequest 是否忽略请求
func (s *sHook) isIgnoredRequest(r *ghttp.Request) bool {
if r.IsFileRequest() {
return true
}
if gstr.InArray(ignoredRequestMethods, strings.ToUpper(r.Method)) {
return true
}
return false
}
+31
View File
@@ -0,0 +1,31 @@
// Package hook
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package hook
import (
"github.com/gogf/gf/v2/net/ghttp"
"hotgo/internal/service"
)
type sHook struct {
}
func init() {
service.RegisterHook(New())
}
func New() *sHook {
return &sHook{}
}
func (s *sHook) BeforeServe(r *ghttp.Request) {
}
func (s *sHook) AfterOutput(r *ghttp.Request) {
s.accessLog(r)
s.lastAdminActive(r)
}
+94
View File
@@ -0,0 +1,94 @@
// Package hook
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package hook
import (
"context"
"hotgo/internal/consts"
"hotgo/internal/dao"
"hotgo/internal/library/contexts"
"hotgo/utility/simple"
"sync"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/os/gtimer"
)
type visitor struct {
lastSeen *gtime.Time
}
var (
visitors = make(map[int64]*visitor)
mtx sync.Mutex
)
// Run a background goroutine to remove old entries from the visitors map.
func init() {
go cleanupVisitors()
}
// Every minute check the map for visitors that haven't been seen for
// more than 3 minutes and delete the entries.
func cleanupVisitors() {
gtimer.AddSingleton(gctx.New(), time.Minute, func(ctx context.Context) {
mtx.Lock()
for memberId, v := range visitors {
if gtime.Now().Sub(v.lastSeen) > 3*time.Minute {
delete(visitors, memberId)
}
}
mtx.Unlock()
})
}
func allow(memberId int64) bool {
mtx.Lock()
defer mtx.Unlock()
v, exists := visitors[memberId]
if !exists {
visitors[memberId] = &visitor{gtime.Now()}
return true
}
if gtime.Now().Sub(v.lastSeen) > time.Second*3 {
v.lastSeen = gtime.Now()
return true
}
return false
}
// lastAdminActive 更新后台用户最后活跃
func (s *sHook) lastAdminActive(r *ghttp.Request) {
if r.IsFileRequest() {
return
}
var (
ctx = r.Context()
member = contexts.GetUser(ctx)
)
if member == nil || member.App != consts.AppAdmin {
return
}
if allow(member.Id) {
simple.SafeGo(ctx, func(ctx context.Context) {
_, err := dao.AdminMember.Ctx(ctx).
Where(dao.AdminMember.Columns().Id, member.Id).
Data(g.Map{dao.AdminMember.Columns().LastActiveAt: gtime.Now()}).
Update()
if err != nil {
g.Log().Warningf(ctx, "hook lastActive err:%+v, memberId:%v", err, member.Id)
}
})
}
}