提交 3d44dbd8 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

Merge branch 'operation-record' of...

Merge branch 'operation-record' of https://github.com/flipped-aurora/gin-vue-admin into operation-record

 Conflicts:
	server/router/sys_auto_code.go
......@@ -58,12 +58,4 @@ log:
prefix: '[GIN-VUE-ADMIN]'
log-file: true
stdout: 'DEBUG'
file: 'DEBUG'
# operation record configuration
operation:
skip-paths:
- '/base/login'
- '/base/register'
- '/sysOperationRecord/getSysOperationRecordList'
- '/sysOperationRecord/deleteSysOperationRecord'
\ No newline at end of file
file: 'DEBUG'
\ No newline at end of file
......@@ -10,7 +10,6 @@ type Server struct {
JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`
Log Log `mapstructure:"log" json:"log" yaml:"log"`
Operation Operation `mapstructure:"operation" json:"operation" yaml:"operation"`
}
type System struct {
......@@ -71,7 +70,3 @@ type Sqlite struct {
Config string `mapstructure:"config" json:"config" yaml:"config"`
LogMode bool `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"`
}
type Operation struct {
SkipPaths []string `mapstructure:"skip-paths" json:"skipPaths" yaml:"skip-paths"`
}
......@@ -654,33 +654,13 @@ CREATE TABLE `sys_operation_records` (
`latency` bigint(20) NULL DEFAULT NULL,
`agent` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
`error_message` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
`body` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '请求Body',
`body` varchar(4096) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '请求Body',
`resp` varchar(4096) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '响应Body',
`user_id` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `idx_sys_operation_records_deleted_at`(`deleted_at`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
-- ----------------------------
-- Records of sys_operation_records
-- ----------------------------
INSERT INTO `sys_operation_records` VALUES (1, '2020-07-02 14:26:18', '2020-07-02 14:26:18', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 15621200, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (2, '2020-07-02 14:26:18', '2020-07-02 14:26:18', NULL, '127.0.0.1', 'POST', '/authority/getAuthorityList', 200, 13668800, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '{\"page\":1,\"pageSize\":999}', 0);
INSERT INTO `sys_operation_records` VALUES (3, '2020-07-02 14:30:20', '2020-07-02 14:30:20', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 19526100, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (4, '2020-07-02 14:31:18', '2020-07-02 14:31:18', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 21476700, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (5, '2020-07-02 14:31:20', '2020-07-02 14:31:20', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 15621000, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (6, '2020-07-02 14:31:23', '2020-07-02 14:31:23', NULL, '127.0.0.1', 'POST', '/menu/getMenuList', 200, 12692200, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '{\"page\":1,\"pageSize\":999}', 0);
INSERT INTO `sys_operation_records` VALUES (7, '2020-07-02 14:31:29', '2020-07-02 14:31:29', NULL, '127.0.0.1', 'POST', '/menu/getBaseMenuById', 200, 15621600, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '{\"id\":52}', 0);
INSERT INTO `sys_operation_records` VALUES (8, '2020-07-02 14:31:40', '2020-07-02 14:31:40', NULL, '127.0.0.1', 'POST', '/menu/getMenuList', 200, 10743400, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '{\"page\":1,\"pageSize\":999}', 0);
INSERT INTO `sys_operation_records` VALUES (9, '2020-07-02 14:31:43', '2020-07-02 14:31:43', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 14646100, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (10, '2020-07-02 14:31:43', '2020-07-02 14:31:43', NULL, '127.0.0.1', 'POST', '/menu/getMenuList', 200, 14645000, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '{\"page\":1,\"pageSize\":999}', 0);
INSERT INTO `sys_operation_records` VALUES (11, '2020-07-02 14:33:41', '2020-07-02 14:33:41', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 19525200, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (12, '2020-07-02 14:33:41', '2020-07-02 14:33:41', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 28313500, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (13, '2020-07-02 14:34:40', '2020-07-02 14:34:40', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 14646300, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (14, '2020-07-02 14:34:41', '2020-07-02 14:34:41', NULL, '127.0.0.1', 'GET', '/sysDictionary/getSysDictionaryList?page=1&pageSize=10', 200, 11715200, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (15, '2020-07-02 14:51:13', '2020-07-02 14:51:13', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 26360200, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (16, '2020-07-02 14:51:57', '2020-07-02 14:51:57', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 18549000, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '', 0);
INSERT INTO `sys_operation_records` VALUES (17, '2020-07-02 14:52:31', '2020-07-02 14:52:31', NULL, '127.0.0.1', 'POST', '/menu/getMenu', 200, 14646500, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36', '', '', 0);
-- ----------------------------
-- Table structure for sys_users
-- ----------------------------
......
......@@ -16,7 +16,6 @@ func Routers() *gin.Engine {
var Router = gin.Default()
// Router.Use(middleware.LoadTls()) // 打开就能玩https了
global.GVA_LOG.Debug("use middleware logger")
Router.Use(middleware.RecordRequestBody(), middleware.OperationRecord())
// 跨域
Router.Use(middleware.Cors())
global.GVA_LOG.Debug("use middleware cors")
......
......@@ -9,56 +9,60 @@ import (
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
var body []byte
func RecordRequestBody() gin.HandlerFunc {
func OperationRecord() gin.HandlerFunc {
return func(c *gin.Context) {
var body []byte
if c.Request.Method != http.MethodGet {
var err error
body, err = ioutil.ReadAll(c.Request.Body)
if err != nil {
global.GVA_LOG.Error(err)
global.GVA_LOG.Error("read body from request error:", err)
} else {
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
}
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
} else {
body = nil
}
userId, err := strconv.Atoi(c.Request.Header.Get("x-user-id"))
if err != nil {
userId = 0
}
record := model.SysOperationRecord{
Ip: c.ClientIP(),
Method: c.Request.Method,
Path: c.Request.URL.Path,
Agent: c.Request.UserAgent(),
Body: string(body),
UserId: userId,
}
writer := responseBodyWriter{
ResponseWriter: c.Writer,
body: &bytes.Buffer{},
}
c.Writer = writer
now := time.Now()
c.Next()
latency := time.Now().Sub(now)
record.ErrorMessage = c.Errors.ByType(gin.ErrorTypePrivate).String()
record.Status = c.Writer.Status()
record.Latency = latency
record.Resp = writer.body.String()
if err := service.CreateSysOperationRecord(record); err != nil {
global.GVA_LOG.Error("create operation record error:", err)
}
}
}
func OperationRecord() gin.HandlerFunc {
return gin.LoggerWithConfig(gin.LoggerConfig{
Formatter: func(param gin.LogFormatterParams) string {
// 防止加载查询参数,再次过滤
for _, v := range global.GVA_CONFIG.Operation.SkipPaths {
if strings.Contains(param.Path, v) {
return ""
}
}
userId, err := strconv.Atoi(param.Request.Header.Get("x-user-id"))
if err != nil {
userId = 0
}
err = service.CreateSysOperationRecord(model.SysOperationRecord{
Ip: param.ClientIP,
Method: param.Method,
Path: param.Path,
Status: param.StatusCode,
Latency: param.Latency,
Agent: param.Request.UserAgent(),
ErrorMessage: param.ErrorMessage,
Body: string(body),
UserId: userId,
})
if err != nil {
global.GVA_LOG.Error(err)
}
return ""
},
Output: nil,
SkipPaths: global.GVA_CONFIG.Operation.SkipPaths,
})
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)
}
......@@ -17,5 +17,7 @@ type SysOperationRecord struct {
Agent string `json:"agent" form:"agent" gorm:"column:agent;comment:''"`
ErrorMessage string `json:"error_message" form:"error_message" gorm:"column:error_message;comment:''"`
Body string `json:"body" form:"body" gorm:"column:body;comment:'请求Body'"`
Resp string `json:"resp" form:"resp" gorm:"column:resp;comment:'响应Body'"`
UserId int `json:"user_id" form:"user_id" gorm:"column:user_id;comment:''"`
User SysUser `json:"user"`
}
......@@ -7,7 +7,10 @@ import (
)
func InitCustomerRouter(Router *gin.RouterGroup) {
ApiRouter := Router.Group("customer").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
ApiRouter := Router.Group("customer").
Use(middleware.JWTAuth()).
Use(middleware.CasbinHandler()).
Use(middleware.OperationRecord())
{
ApiRouter.POST("customer", v1.CreateExaCustomer) // 创建客户
ApiRouter.PUT("customer", v1.UpdateExaCustomer) // 更新客户
......
......@@ -7,7 +7,10 @@ import (
)
func InitAuthorityRouter(Router *gin.RouterGroup) {
AuthorityRouter := Router.Group("authority").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
AuthorityRouter := Router.Group("authority").
Use(middleware.JWTAuth()).
Use(middleware.CasbinHandler()).
Use(middleware.OperationRecord())
{
AuthorityRouter.POST("createAuthority", v1.CreateAuthority) // 创建角色
AuthorityRouter.POST("deleteAuthority", v1.DeleteAuthority) // 删除角色
......
......@@ -2,16 +2,19 @@ package router
import (
"gin-vue-admin/api/v1"
"gin-vue-admin/middleware"
"github.com/gin-gonic/gin"
)
func InitAutoCodeRouter(Router *gin.RouterGroup) {
AutoCodeRouter := Router.Group("autoCode")
AutoCodeRouter := Router.Group("autoCode").
Use(middleware.JWTAuth()).
Use(middleware.CasbinHandler()).
Use(middleware.OperationRecord())
{
AutoCodeRouter.POST("createTemp", v1.CreateTemp) // 创建自动化代码
AutoCodeRouter.GET("getTables", v1.GetTables) // 获取对应数据库的表
AutoCodeRouter.GET("getDB", v1.GetDB) // 获取数据库
AutoCodeRouter.GET("getColume", v1.GetColume) // 获取指定表所有字段信息
}
}
......@@ -7,7 +7,10 @@ import (
)
func InitCasbinRouter(Router *gin.RouterGroup) {
CasbinRouter := Router.Group("casbin").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
CasbinRouter := Router.Group("casbin").
Use(middleware.JWTAuth()).
Use(middleware.CasbinHandler()).
Use(middleware.OperationRecord())
{
CasbinRouter.POST("updateCasbin", v1.UpdateCasbin)
CasbinRouter.POST("getPolicyPathByAuthorityId", v1.GetPolicyPathByAuthorityId)
......
......@@ -7,7 +7,10 @@ import (
)
func InitSysDictionaryRouter(Router *gin.RouterGroup) {
SysDictionaryRouter := Router.Group("sysDictionary").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
SysDictionaryRouter := Router.Group("sysDictionary").
Use(middleware.JWTAuth()).
Use(middleware.CasbinHandler()).
Use(middleware.OperationRecord())
{
SysDictionaryRouter.POST("createSysDictionary", v1.CreateSysDictionary) // 新建SysDictionary
SysDictionaryRouter.DELETE("deleteSysDictionary", v1.DeleteSysDictionary) // 删除SysDictionary
......
......@@ -7,7 +7,10 @@ import (
)
func InitSysDictionaryDetailRouter(Router *gin.RouterGroup) {
SysDictionaryDetailRouter := Router.Group("sysDictionaryDetail").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
SysDictionaryDetailRouter := Router.Group("sysDictionaryDetail").
Use(middleware.JWTAuth()).
Use(middleware.CasbinHandler()).
Use(middleware.OperationRecord())
{
SysDictionaryDetailRouter.POST("createSysDictionaryDetail", v1.CreateSysDictionaryDetail) // 新建SysDictionaryDetail
SysDictionaryDetailRouter.DELETE("deleteSysDictionaryDetail", v1.DeleteSysDictionaryDetail) // 删除SysDictionaryDetail
......
......@@ -7,7 +7,9 @@ import (
)
func InitSysOperationRecordRouter(Router *gin.RouterGroup) {
SysOperationRecordRouter := Router.Group("sysOperationRecord").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
SysOperationRecordRouter := Router.Group("sysOperationRecord").
Use(middleware.JWTAuth()).
Use(middleware.CasbinHandler())
{
SysOperationRecordRouter.POST("createSysOperationRecord", v1.CreateSysOperationRecord) // 新建SysOperationRecord
SysOperationRecordRouter.DELETE("deleteSysOperationRecord", v1.DeleteSysOperationRecord) // 删除SysOperationRecord
......
......@@ -7,7 +7,10 @@ import (
)
func InitUserRouter(Router *gin.RouterGroup) {
UserRouter := Router.Group("user").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
UserRouter := Router.Group("user").
Use(middleware.JWTAuth()).
Use(middleware.CasbinHandler()).
Use(middleware.OperationRecord())
{
UserRouter.POST("changePassword", v1.ChangePassword) // 修改密码
UserRouter.POST("uploadHeaderImg", v1.UploadHeaderImg) // 上传头像
......
......@@ -74,6 +74,6 @@ func GetSysOperationRecordInfoList(info request.SysOperationRecordSearch) (err e
db = db.Where("status = ?", info.Status)
}
err = db.Count(&total).Error
err = db.Limit(limit).Offset(offset).Find(&sysOperationRecords).Error
err = db.Order("id desc").Limit(limit).Offset(offset).Preload("User").Find(&sysOperationRecords).Error
return err, sysOperationRecords, total
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册