mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-12-26 09:22:50 +08:00
feat: 增加logger中间件
This commit is contained in:
parent
d47c03c3d6
commit
3ba1283b88
3 changed files with 37 additions and 1 deletions
15
backend/app/entity/log.go
Normal file
15
backend/app/entity/log.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package entity
|
||||
|
||||
import "github.com/1Panel-dev/1Panel/app/entity/common"
|
||||
|
||||
type OperateLog struct {
|
||||
common.BaseModel
|
||||
Name string `gorm:"type:varchar(64)"`
|
||||
Type string `gorm:"type:varchar(64)"`
|
||||
User string `gorm:"type:varchar(64)"`
|
||||
Path string `gorm:"type:varchar(64)"`
|
||||
IP string `gorm:"type:varchar(64)"`
|
||||
UserAgent string `gorm:"type:varchar(64)"`
|
||||
Source string `gorm:"type:varchar(64)"`
|
||||
Detail string `gorm:"type:longText"`
|
||||
}
|
||||
|
|
@ -54,7 +54,6 @@ func CorsByRules() gin.HandlerFunc {
|
|||
|
||||
func checkCors(currentOrigin string) *configs.CORSWhiteList {
|
||||
for _, whitelist := range global.Config.CORS.WhiteList {
|
||||
// 遍历配置中的跨域头,寻找匹配项
|
||||
if currentOrigin == whitelist.AllowOrigin {
|
||||
return &whitelist
|
||||
}
|
||||
|
|
|
|||
22
backend/middlerware/logger.go
Normal file
22
backend/middlerware/logger.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package middlerware
|
||||
|
||||
import (
|
||||
"github.com/1Panel-dev/1Panel/app/entity"
|
||||
"github.com/1Panel-dev/1Panel/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Logger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
operateLog := entity.OperateLog{
|
||||
Path: path,
|
||||
IP: c.ClientIP(),
|
||||
UserAgent: c.Request.UserAgent(),
|
||||
}
|
||||
global.DB.Model(entity.OperateLog{}).Save(&operateLog)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
//TODO 根据URL写操作日志
|
||||
Loading…
Add table
Reference in a new issue