feat: 管理端网站监控,浏览量写入独立 monitor.db
请求日志按日 JSONL;page_views 不进主库,避免统计数据撑大 jiang13.db。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
33
middleware/accesslog.go
Normal file
33
middleware/accesslog.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.iioio.com/freefire/jiang13-forum/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AccessLogMiddleware 服务端访问日志采集(受 monitor_enabled 控制;热路径仅入队,后台写 jsonl)
|
||||
func AccessLogMiddleware(mon *service.MonitorService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if mon == nil || !mon.Enabled() {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
path := c.Request.URL.Path
|
||||
if mon.ShouldSkip(path) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
start := time.Now()
|
||||
c.Next()
|
||||
status := c.Writer.Status()
|
||||
bytes := int64(c.Writer.Size())
|
||||
if bytes < 0 {
|
||||
bytes = 0
|
||||
}
|
||||
dur := int(time.Since(start).Milliseconds())
|
||||
row := mon.BuildAccessLog(c.Request, c.Request.RemoteAddr, status, bytes, dur)
|
||||
mon.Enqueue(row)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user