mirror of
https://github.com/Li4n0/revsuit.git
synced 2026-09-25 08:01:54 +08:00
refactor: standardization of code structure (#7)
This commit is contained in:
+3
-4
@@ -81,10 +81,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
|
||||
}
|
||||
|
||||
func (s *Server) Run() {
|
||||
@@ -153,7 +152,7 @@ func (s *Server) Receive(c *gin.Context) {
|
||||
// create new record
|
||||
r, err := NewRecord(_rule, flag, c.Request.Method, u, ip, area, string(raw))
|
||||
if err != nil {
|
||||
log.Error("HTTP record[rule_id:%d] created failed :%s", _rule.ID, err.Error())
|
||||
log.Error("HTTP record[rule_id:%d] created failed :%s", _rule.ID, err)
|
||||
code, err := strconv.Atoi(compileTpl(c, _rule.ResponseStatusCode, vars))
|
||||
if err != nil || code < 100 || code > 600 {
|
||||
code = 400
|
||||
|
||||
+4
-5
@@ -41,8 +41,7 @@ func NewRecord(rule *Rule, flag, method, url, ip, area, raw string) (r *Record,
|
||||
RawRequest: raw,
|
||||
Rule: *rule,
|
||||
}
|
||||
err = database.DB.Create(r).Error
|
||||
return r, err
|
||||
return r, database.DB.Create(r).Error
|
||||
}
|
||||
|
||||
func ListRecords(c *gin.Context) {
|
||||
@@ -83,20 +82,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.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
|
||||
|
||||
+7
-7
@@ -102,20 +102,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
|
||||
@@ -138,7 +138,7 @@ func UpsertRules(c *gin.Context) {
|
||||
if err := c.ShouldBind(&httpRule); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
@@ -151,7 +151,7 @@ func UpsertRules(c *gin.Context) {
|
||||
if err := httpRule.CreateOrUpdate(); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"result": nil,
|
||||
})
|
||||
return
|
||||
@@ -177,7 +177,7 @@ func DeleteRules(c *gin.Context) {
|
||||
if err := c.ShouldBind(&httpRule); err != nil {
|
||||
c.JSON(400, gin.H{
|
||||
"status": "failed",
|
||||
"error": err.Error(),
|
||||
"error": err,
|
||||
"data": nil,
|
||||
})
|
||||
return
|
||||
@@ -186,7 +186,7 @@ func DeleteRules(c *gin.Context) {
|
||||
if err := httpRule.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