🎨 优化项目结构 && 完善ai配置
This commit is contained in:
@@ -3,111 +3,87 @@ package middleware
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.echol.cn/loser/ai_proxy/server/global"
|
||||
"git.echol.cn/loser/ai_proxy/server/model/system"
|
||||
"git.echol.cn/loser/ai_proxy/server/service"
|
||||
"git.echol.cn/loser/ai_proxy/server/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var operationRecordService = service.ServiceGroupApp.SystemServiceGroup.OperationRecordService
|
||||
// LogLayout 日志layout
|
||||
type LogLayout struct {
|
||||
Time time.Time
|
||||
Metadata map[string]interface{} // 存储自定义原数据
|
||||
Path string // 访问路径
|
||||
Query string // 携带query
|
||||
Body string // 携带body数据
|
||||
IP string // ip地址
|
||||
UserAgent string // 代理
|
||||
Error string // 错误
|
||||
Cost time.Duration // 花费时间
|
||||
Source string // 来源
|
||||
}
|
||||
|
||||
// OperationRecord 操作记录中间件
|
||||
func OperationRecord() gin.HandlerFunc {
|
||||
type Logger struct {
|
||||
// Filter 用户自定义过滤
|
||||
Filter func(c *gin.Context) bool
|
||||
// FilterKeyword 关键字过滤(key)
|
||||
FilterKeyword func(layout *LogLayout) bool
|
||||
// AuthProcess 鉴权处理
|
||||
AuthProcess func(c *gin.Context, layout *LogLayout)
|
||||
// 日志处理
|
||||
Print func(LogLayout)
|
||||
// Source 服务唯一标识
|
||||
Source string
|
||||
}
|
||||
|
||||
func (l Logger) SetLoggerMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
path := c.Request.URL.Path
|
||||
query := c.Request.URL.RawQuery
|
||||
var body []byte
|
||||
var userId int
|
||||
if c.Request.Method != http.MethodGet {
|
||||
var err error
|
||||
body, err = io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("read body from request error:", zap.Error(err))
|
||||
} else {
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
||||
}
|
||||
if l.Filter != nil && !l.Filter(c) {
|
||||
body, _ = c.GetRawData()
|
||||
// 将原body塞回去
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
||||
}
|
||||
|
||||
userId = int(utils.GetUserID(c))
|
||||
|
||||
writer := responseBodyWriter{
|
||||
ResponseWriter: c.Writer,
|
||||
body: &bytes.Buffer{},
|
||||
}
|
||||
c.Writer = writer
|
||||
now := time.Now()
|
||||
|
||||
c.Next()
|
||||
|
||||
latency := time.Since(now)
|
||||
|
||||
if c.Request.Method != http.MethodGet {
|
||||
record := system.SysOperationRecord{
|
||||
Ip: c.ClientIP(),
|
||||
Method: c.Request.Method,
|
||||
Path: c.Request.URL.Path,
|
||||
Agent: c.Request.UserAgent(),
|
||||
Body: string(body),
|
||||
UserID: userId,
|
||||
Status: c.Writer.Status(),
|
||||
Latency: latency,
|
||||
Resp: writer.body.String(),
|
||||
}
|
||||
|
||||
values, _ := url.ParseQuery(c.Request.URL.RawQuery)
|
||||
record.Query = values.Encode()
|
||||
|
||||
if err := operationRecordService.CreateSysOperationRecord(record); err != nil {
|
||||
global.GVA_LOG.Error("create operation record error:", zap.Error(err))
|
||||
}
|
||||
cost := time.Since(start)
|
||||
layout := LogLayout{
|
||||
Time: time.Now(),
|
||||
Path: path,
|
||||
Query: query,
|
||||
IP: c.ClientIP(),
|
||||
UserAgent: c.Request.UserAgent(),
|
||||
Error: strings.TrimRight(c.Errors.ByType(gin.ErrorTypePrivate).String(), "\n"),
|
||||
Cost: cost,
|
||||
Source: l.Source,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type responseBodyWriter struct {
|
||||
gin.ResponseWriter
|
||||
body *bytes.Buffer
|
||||
}
|
||||
|
||||
func (r responseBodyWriter) Write(b []byte) (int, error) {
|
||||
r.body.Write(b)
|
||||
return r.ResponseWriter.Write(b)
|
||||
}
|
||||
|
||||
func (r responseBodyWriter) WriteString(s string) (int, error) {
|
||||
r.body.WriteString(s)
|
||||
return r.ResponseWriter.WriteString(s)
|
||||
}
|
||||
|
||||
func (r responseBodyWriter) WriteJSON(obj interface{}) error {
|
||||
data, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.body.Write(data)
|
||||
return r.ResponseWriter.WriteJSON(obj)
|
||||
}
|
||||
|
||||
// NeedRecordPath 判断是否需要记录操作日志
|
||||
func NeedRecordPath(path string) bool {
|
||||
// 排除不需要记录的路径
|
||||
excludePaths := []string{
|
||||
"/health",
|
||||
"/swagger",
|
||||
"/api/captcha",
|
||||
}
|
||||
|
||||
for _, excludePath := range excludePaths {
|
||||
if strings.HasPrefix(path, excludePath) {
|
||||
return false
|
||||
if l.Filter != nil && !l.Filter(c) {
|
||||
layout.Body = string(body)
|
||||
}
|
||||
if l.AuthProcess != nil {
|
||||
// 处理鉴权需要的信息
|
||||
l.AuthProcess(c, &layout)
|
||||
}
|
||||
if l.FilterKeyword != nil {
|
||||
// 自行判断key/value 脱敏等
|
||||
l.FilterKeyword(&layout)
|
||||
}
|
||||
// 自行处理日志
|
||||
l.Print(layout)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func DefaultLogger() gin.HandlerFunc {
|
||||
return Logger{
|
||||
Print: func(layout LogLayout) {
|
||||
// 标准输出,k8s做收集
|
||||
v, _ := json.Marshal(layout)
|
||||
fmt.Println(string(v))
|
||||
},
|
||||
Source: "GVA",
|
||||
}.SetLoggerMiddleware()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user