mirror of
https://github.com/Li4n0/revsuit.git
synced 2026-09-25 16:11:53 +08:00
refactor: standardization of code structure (#7)
This commit is contained in:
+11
-14
@@ -47,10 +47,9 @@ func (s *Server) getRules() []*Rule {
|
||||
|
||||
func (s *Server) updateRules() error {
|
||||
db := database.DB.Model(new(Rule))
|
||||
defer s.rulesLock.Unlock()
|
||||
s.rulesLock.Lock()
|
||||
db.Order("rank desc").Find(&s.rules)
|
||||
s.rulesLock.Unlock()
|
||||
return nil
|
||||
return db.Order("rank desc").Find(&s.rules).Error
|
||||
}
|
||||
|
||||
// NewConnection is part of the mysql.Handler interface.
|
||||
@@ -90,28 +89,26 @@ func (s *Server) NewConnection(c *vmysql.Conn) {
|
||||
func (s *Server) ConnectionClosed(c *vmysql.Conn) {
|
||||
log.Trace("MySQL Client leaved, ID [%d]", c.ConnectionID)
|
||||
|
||||
var (
|
||||
user = c.User
|
||||
schema = c.SchemaName
|
||||
supportLoadLocalData = c.SupportLoadDataLocal
|
||||
cr, ok = s.connRulePool.Load(c.ConnectionID)
|
||||
clientName, clientOS, flag, flagGroup string
|
||||
)
|
||||
var clientName, clientOS, flag, flagGroup string
|
||||
|
||||
user := c.User
|
||||
schema := c.SchemaName
|
||||
supportLoadLocalData := c.SupportLoadDataLocal
|
||||
|
||||
cr, ok := s.connRulePool.Load(c.ConnectionID)
|
||||
if !ok {
|
||||
log.Error("MySQL Connection rule(%d) not match flag", c.ConnectionID)
|
||||
return
|
||||
}
|
||||
|
||||
_rule := cr.(*Rule)
|
||||
// flag must not be empty
|
||||
for _, s := range []string{user, schema} {
|
||||
flag, flagGroup, _ = _rule.Match(s)
|
||||
if flag != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if flag == "" {
|
||||
log.Error("MySQL Connection rule(%d) not match flag", c.ConnectionID)
|
||||
}
|
||||
|
||||
if c.ConnAttrs != nil {
|
||||
clientName = c.ConnAttrs["_client_name"] + " " + c.ConnAttrs["_client_version"]
|
||||
@@ -130,7 +127,7 @@ func (s *Server) ConnectionClosed(c *vmysql.Conn) {
|
||||
|
||||
r, err := newRecord(_rule, flag, user, clientName, clientOS, ip, qqwry.Area(ip), supportLoadLocalData, files)
|
||||
if err != nil {
|
||||
log.Error("MySQL record(rule_id:%s) created failed :%s", _rule.Name, err.Error())
|
||||
log.Error("MySQL record(rule_id:%s) created failed :%s", _rule.Name, err)
|
||||
return
|
||||
}
|
||||
log.Info("MySQL record[id:%d rule:%s remote_ip:%s] has been created", r.ID, _rule.Name, ip)
|
||||
|
||||
+4
-5
@@ -45,8 +45,7 @@ func newRecord(rule *Rule, flag, username, clientName, clientOS, remoteIp, ipAre
|
||||
Files: files,
|
||||
Rule: *rule,
|
||||
}
|
||||
err = database.DB.Create(r).Error
|
||||
return r, err
|
||||
return r, database.DB.Create(r).Error
|
||||
}
|
||||
|
||||
func ListRecords(c *gin.Context) {
|
||||
@@ -93,20 +92,20 @@ func ListRecords(c *gin.Context) {
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"result": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if order != "desc" && order != "asc" {
|
||||
if order != "asc" {
|
||||
order = "desc"
|
||||
}
|
||||
|
||||
if err := db.Preload("Files").Order("id" + " " + order).Count(&count).Offset((page - 1) * 10).Limit(10).Find(&res).Error; err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
|
||||
+8
-8
@@ -68,7 +68,7 @@ func ListRules(c *gin.Context) {
|
||||
if err := c.ShouldBind(&mysqlRule); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"result": nil,
|
||||
})
|
||||
return
|
||||
@@ -84,20 +84,20 @@ func ListRules(c *gin.Context) {
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"result": nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if order != "desc" && order != "asc" {
|
||||
if order != "asc" {
|
||||
order = "desc"
|
||||
}
|
||||
|
||||
if err := db.Order("rank desc").Order("id" + " " + order).Count(&count).Offset((page - 1) * 10).Limit(10).Find(&res).Error; err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
@@ -120,7 +120,7 @@ func UpsertRules(c *gin.Context) {
|
||||
if err := c.ShouldBind(&mysqlRule); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
@@ -133,7 +133,7 @@ func UpsertRules(c *gin.Context) {
|
||||
if err := mysqlRule.CreateOrUpdate(); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
@@ -159,7 +159,7 @@ func DeleteRules(c *gin.Context) {
|
||||
if err := c.ShouldBind(&mysqlRule); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
@@ -168,7 +168,7 @@ func DeleteRules(c *gin.Context) {
|
||||
if err := mysqlRule.Delete(); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user