210 lines
4.3 KiB
Go
210 lines
4.3 KiB
Go
|
package webserver
|
|||
|
|
|||
|
import (
|
|||
|
"github.com/gin-gonic/gin"
|
|||
|
"net/http"
|
|||
|
"proxymysql/app/conf"
|
|||
|
"proxymysql/app/db"
|
|||
|
"proxymysql/app/zlog"
|
|||
|
"strings"
|
|||
|
"time"
|
|||
|
)
|
|||
|
|
|||
|
type ApiRes struct {
|
|||
|
Code int32
|
|||
|
Message string
|
|||
|
Data interface{}
|
|||
|
}
|
|||
|
|
|||
|
func Start() {
|
|||
|
g := gin.Default()
|
|||
|
|
|||
|
g.LoadHTMLGlob("resource/view/*.tmpl")
|
|||
|
|
|||
|
g.Static("/resource", "resource")
|
|||
|
|
|||
|
g.GET("/", func(ctx *gin.Context) {
|
|||
|
ctx.HTML(http.StatusOK, "index.tmpl", nil)
|
|||
|
})
|
|||
|
|
|||
|
g.POST("/ListSqlQueryLog", func(ctx *gin.Context) {
|
|||
|
//time.Sleep(time.Millisecond * 200)
|
|||
|
type Req struct {
|
|||
|
StartDate string `json:"start_date"`
|
|||
|
EndDate string `json:"end_date"`
|
|||
|
QueryInfo string `json:"query_info"`
|
|||
|
Page int `json:"page"`
|
|||
|
Limit int `json:"limit"`
|
|||
|
AdminName string `json:"admin_name"`
|
|||
|
}
|
|||
|
|
|||
|
req := &Req{}
|
|||
|
err := ctx.ShouldBindJSON(req)
|
|||
|
if err != nil {
|
|||
|
zlog.Warn("params err: %s", err)
|
|||
|
}
|
|||
|
|
|||
|
query := db.GetDB().Table("sql_query_log a")
|
|||
|
query.Select("a.*")
|
|||
|
query.Where("a.admin_name != ''")
|
|||
|
|
|||
|
//if req.GameId >= 0 {
|
|||
|
// query.Where("header_game_id", req.GameId)
|
|||
|
//}
|
|||
|
|
|||
|
if req.QueryInfo != "" {
|
|||
|
query.Where("a.query like ?", "%"+req.QueryInfo+"%")
|
|||
|
}
|
|||
|
|
|||
|
if req.AdminName != "" {
|
|||
|
query.Where("a.admin_name = ?", req.AdminName)
|
|||
|
}
|
|||
|
|
|||
|
if req.StartDate != "" && req.EndDate != "" {
|
|||
|
query.Where("a.create_time between ? and ?", req.StartDate, req.EndDate+" 23:59:59.999")
|
|||
|
}
|
|||
|
|
|||
|
if req.Page <= 0 {
|
|||
|
req.Page = 1
|
|||
|
}
|
|||
|
|
|||
|
if req.Limit <= 0 {
|
|||
|
req.Limit = 20
|
|||
|
}
|
|||
|
|
|||
|
query.Order("a.id desc")
|
|||
|
|
|||
|
offset := (req.Page - 1) * req.Limit
|
|||
|
|
|||
|
query.Limit(req.Limit).Offset(offset)
|
|||
|
|
|||
|
type SqlQueryLog struct {
|
|||
|
Id int64
|
|||
|
AdminId int32
|
|||
|
AdminName string
|
|||
|
AdminRealName string
|
|||
|
QueryGameId int32
|
|||
|
HeaderGameId int32
|
|||
|
Ip string
|
|||
|
RequestPath string
|
|||
|
RequestInfo string
|
|||
|
UnixMilli int64
|
|||
|
Query string
|
|||
|
CreateTime time.Time
|
|||
|
QueryTime string
|
|||
|
Project string
|
|||
|
CallInfo string
|
|||
|
MenuName string
|
|||
|
}
|
|||
|
|
|||
|
list := make([]*SqlQueryLog, 0)
|
|||
|
|
|||
|
err = query.Find(&list).Error
|
|||
|
if err != nil {
|
|||
|
ctx.JSON(http.StatusOK, &ApiRes{
|
|||
|
Code: -1,
|
|||
|
Message: err.Error(),
|
|||
|
})
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
pathList := make([]string, 0, len(list))
|
|||
|
|
|||
|
for _, v := range list {
|
|||
|
pathList = append(pathList, v.RequestPath)
|
|||
|
//if v.Query != "" {
|
|||
|
//decQuery, _ := base64.StdEncoding.DecodeString(v.Query)
|
|||
|
//v.Query = string(decQuery)
|
|||
|
//}
|
|||
|
|
|||
|
v.QueryTime = v.CreateTime.Format("2006-01-02 15:04:05.000")
|
|||
|
//if v.UnixMilli > 0 {
|
|||
|
// v.QueryTime = time.UnixMilli(v.UnixMilli).Format("2006-01-02 15:04:05.000")
|
|||
|
//} else {
|
|||
|
// v.QueryTime = v.CreateTime.Format("2006-01-02 15:04:05.000")
|
|||
|
//}
|
|||
|
|
|||
|
if v.RequestPath != "" {
|
|||
|
v.Project = strings.Split(v.RequestPath, "/")[2]
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
queryMenu := db.GetDB().Table("new_admin.power_menu").
|
|||
|
Select("name", "path").
|
|||
|
Where("path in ?", pathList)
|
|||
|
|
|||
|
type MenuInfo struct {
|
|||
|
Name string
|
|||
|
Path string
|
|||
|
}
|
|||
|
menuInfos := make([]*MenuInfo, 0)
|
|||
|
err = queryMenu.Find(&menuInfos).Error
|
|||
|
if err != nil {
|
|||
|
ctx.JSON(http.StatusOK, &ApiRes{
|
|||
|
Code: -1,
|
|||
|
Message: err.Error(),
|
|||
|
})
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
menuInfoMap := make(map[string]string)
|
|||
|
for _, v := range menuInfos {
|
|||
|
menuInfoMap[v.Path] = v.Name
|
|||
|
}
|
|||
|
|
|||
|
for _, v := range list {
|
|||
|
v.MenuName = menuInfoMap[v.RequestPath]
|
|||
|
}
|
|||
|
|
|||
|
ctx.JSON(http.StatusOK, &ApiRes{
|
|||
|
Message: "ok",
|
|||
|
Data: gin.H{
|
|||
|
"List": list,
|
|||
|
"TotalCount": GetSimplePageCount(req.Page, req.Limit, len(list)),
|
|||
|
},
|
|||
|
})
|
|||
|
|
|||
|
})
|
|||
|
|
|||
|
g.POST("/SelectInfo", func(ctx *gin.Context) {
|
|||
|
type AdminName struct {
|
|||
|
AdminName string
|
|||
|
}
|
|||
|
|
|||
|
names := make([]*AdminName, 0)
|
|||
|
err := db.GetDB().Table("sql_query_log").Select("distinct admin_name").
|
|||
|
Where("admin_name != ''").
|
|||
|
Order("id desc").
|
|||
|
Find(&names).Error
|
|||
|
if err != nil {
|
|||
|
ctx.JSON(http.StatusOK, &ApiRes{
|
|||
|
Code: -1,
|
|||
|
Message: err.Error(),
|
|||
|
})
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
res := make(map[string]interface{})
|
|||
|
|
|||
|
res["AdminNameList"] = names
|
|||
|
|
|||
|
ctx.JSON(http.StatusOK, &ApiRes{Data: res})
|
|||
|
})
|
|||
|
|
|||
|
zlog.Infof("web server listen on %s", conf.App.WebAddr)
|
|||
|
|
|||
|
g.Run(conf.App.WebAddr)
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
func GetSimplePageCount(pageNum int, limit int, rowsNum int) int {
|
|||
|
count := (pageNum-1)*limit + rowsNum
|
|||
|
// 查出来的条数和limit数量相等,加1让下一页按钮能够点击
|
|||
|
if rowsNum == limit {
|
|||
|
count = count + 1
|
|||
|
}
|
|||
|
|
|||
|
return count
|
|||
|
}
|