Skip to content

Commit

Permalink
Merge pull request #57 from axiaoxin/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
axiaoxin authored Mar 11, 2020
2 parents 1544c74 + 8ff2585 commit d1a8023
Show file tree
Hide file tree
Showing 33 changed files with 528 additions and 297 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ go mod
## 接口实现

`app/apis/routes.go`中添加URL并指定你的handleFunc,handleFunc推荐在`app/apis`下按业务模块新建文件的形式组织
可复用的代码可以在`app/services`下以目录或者文件的形式按需组织
可复用的代码可以在`app/handlers`下以目录或者文件的形式按需组织

API版本号定义在`app/api/apis.go`中,可以手动修改值,但不要修改代码格式,自动生成API文档依赖这个格式。

Expand All @@ -45,7 +45,7 @@ API版本号定义在`app/api/apis.go`中,可以手动修改值,但不要修
- `app/apis/routes.go`
- `app/apis/demo*`
- `app/models/demomod/`
- `app/services/demosvc/`
- `app/handlers/demohdl/`

## 访问DB

Expand Down
16 changes: 8 additions & 8 deletions app/apis/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package apis

import (
"pink-lady/app/database"
"pink-lady/app/handlers/demohdl"
"pink-lady/app/response"
"pink-lady/app/services/demosvc"

"github.com/gin-gonic/gin"
)
Expand All @@ -27,7 +27,7 @@ func CreateAlertPolicy(c *gin.Context) {
// 使用外层appid和uin作为告警策略的字段
p.AlertPolicy.AppID = p.AppID
p.AlertPolicy.Uin = p.Uin
result, err := demosvc.CreateAlertPolicy(c, db, p.AlertPolicy)
result, err := demohdl.CreateAlertPolicy(c, db, p.AlertPolicy)
if err != nil {
response.ErrJSON(c, err)
return
Expand All @@ -53,7 +53,7 @@ func DescribeAlertPolicy(c *gin.Context) {
return
}
db := database.UTDB()
result, err := demosvc.DescribeAlertPolicy(c, db, p.AppID, p.Uin, p.ID)
result, err := demohdl.DescribeAlertPolicy(c, db, p.AppID, p.Uin, p.ID)
if err != nil {
response.ErrJSON(c, err)
return
Expand All @@ -79,7 +79,7 @@ func DeleteAlertPolicy(c *gin.Context) {
return
}
db := database.UTDB()
err := demosvc.DeleteAlertPolicy(c, db, p.AppID, p.Uin, p.ID)
err := demohdl.DeleteAlertPolicy(c, db, p.AppID, p.Uin, p.ID)
if err != nil {
response.ErrJSON(c, err)
return
Expand All @@ -89,7 +89,7 @@ func DeleteAlertPolicy(c *gin.Context) {

// ModifyAlertPolicy godoc
// @Summary 更新告警策略
// @Description 更新告警策略返回对应ID
// @Description 更新告警策略成功返回true
// @Tags demo
// @Accept json
// @Produce json
Expand All @@ -106,12 +106,12 @@ func ModifyAlertPolicy(c *gin.Context) {
// 使用外层appid和uin作为告警策略的字段
p.AlertPolicy.AppID = p.AppID
p.AlertPolicy.Uin = p.Uin
result, err := demosvc.ModifyAlertPolicy(c, db, p.AlertPolicy)
err := demohdl.ModifyAlertPolicy(c, db, p.AlertPolicy)
if err != nil {
response.ErrJSON(c, err)
return
}
response.JSON(c, result)
response.JSON(c, true)
}

// DescribeAlertPolicies godoc
Expand Down Expand Up @@ -140,7 +140,7 @@ func DescribeAlertPolicies(c *gin.Context) {
return
}
db := database.UTDB()
result, count, err := demosvc.DescribeAlertPolicies(c, db, p.AppID, p.Uin, p.Offset, p.Limit, p.Order, p.ID, p.Name)
result, count, err := demohdl.DescribeAlertPolicies(c, db, p.AppID, p.Uin, p.Offset, p.Limit, p.Order, p.ID, p.Name)
if err != nil {
response.ErrJSON(c, err)
return
Expand Down
10 changes: 2 additions & 8 deletions app/apis/demo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,8 @@ func TestUpdate(t *testing.T) {
if data.Get("code").ToInt() != 0 {
t.Fatal("接口返回了错误信息,没有成功获取告警策略:", string(body))
}
if data.Get("data", "alert_channel").ToString() != "weixin" {
t.Fatal("修改策略表的alert_channel字段失败:", string(body))
}
if data.Get("data", "alert_filter_rules").Size() != 0 {
t.Fatal("修改策略清空过滤条件未生效:", string(body))
}
if data.Get("data", "alert_trigger_rules").Size() != 1 {
t.Fatal("修改策略触发条件未生效:", string(body))
if data.Get("data").ToBool() == false {
t.Fatal("修改策略表的字段失败:", string(body))
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/apis/docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2019-12-02 18:25:14.962373 +0800 CST m=+0.059366310
// 2020-02-18 16:28:52.636591 +0800 CST m=+0.036580649

package docs

Expand Down Expand Up @@ -107,7 +107,7 @@ var doc = `{
}
},
"put": {
"description": "更新告警策略返回对应ID",
"description": "更新告警策略成功返回true",
"consumes": [
"application/json"
],
Expand Down
2 changes: 1 addition & 1 deletion app/apis/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
}
},
"put": {
"description": "更新告警策略返回对应ID",
"description": "更新告警策略成功返回true",
"consumes": [
"application/json"
],
Expand Down
2 changes: 1 addition & 1 deletion app/apis/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ paths:
put:
consumes:
- application/json
description: 更新告警策略返回对应ID
description: 更新告警策略成功返回true
parameters:
- description: 参数包含BaseParam和demomod.AlertPolicy
in: body
Expand Down
17 changes: 12 additions & 5 deletions app/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,18 @@ postgres = [
]

[redis]
mode = "single-instance"
address = "localhost:6379"
password = ""
dbindex = 0
master = ""
[redis.local0]
mode = "normal"
address = "localhost:6379"
password = ""
dbindex = 0
master = ""
[redis.local1]
mode = "normal"
address = "localhost:6379"
password = ""
dbindex = 1
master = ""

[apidocs]
rootRedirect = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package demosvc
package demohdl

import (
"pink-lady/app/database"
Expand Down Expand Up @@ -135,19 +135,19 @@ func DescribeAlertTriggerRules(c *gin.Context, db *gorm.DB, policyID int64) ([]d
// ModifyAlertPolicy 更新告警策略
// 使用Association Replace来更新关联条件如果传了主键ID则更新对应记录,没有主键ID的全部新增记录,剔除了关联并不会删除记录只会把关联的id置为0
// 这里需要真实删除记录 使用事务删除全部关联
func ModifyAlertPolicy(c *gin.Context, db *gorm.DB, policy *demomod.AlertPolicy) (*demomod.AlertPolicy, error) {
func ModifyAlertPolicy(c *gin.Context, db *gorm.DB, policy *demomod.AlertPolicy) error {
// 查询被更新的记录是否存在
rawPolicy := &demomod.AlertPolicy{}
if err := db.Where("appid = ? AND uin = ? AND id = ?", policy.AppID, policy.Uin, policy.ID).Find(rawPolicy).Error; err != nil {
logging.CtxLogger(c).Error("ModifyAlertPolicy Find error", zap.Error(err))
return nil, ErrAlertPolicyModifyFailed.AppendError(err)
return ErrAlertPolicyModifyFailed.AppendError(err)
}

// 开启事务
tx := db.Begin()
if err := tx.Error; err != nil {
logging.CtxLogger(c).Error("ModifyAlertPolicy Begin tx error", zap.Error(err))
return nil, ErrAlertPolicyModifyFailed.AppendError(err)
return ErrAlertPolicyModifyFailed.AppendError(err)
}
defer tx.Rollback()

Expand All @@ -164,36 +164,42 @@ func ModifyAlertPolicy(c *gin.Context, db *gorm.DB, policy *demomod.AlertPolicy)
"callback_url": policy.CallbackURL,
}).Error; err != nil {
logging.CtxLogger(c).Error("ModifyAlertPolicy Updates policy error", zap.Error(err))
return nil, ErrAlertPolicyModifyFailed.AppendError(err)
return ErrAlertPolicyModifyFailed.AppendError(err)
}

// 删除过滤条件
if err := tx.Where("alert_policy_id = ?", policy.ID).Delete(demomod.AlertFilterRule{}).Error; err != nil {
logging.CtxLogger(c).Error("ModifyAlertPolicy Delete filter rules error", zap.Error(err))
return nil, ErrAlertFilterRuleDeleteFailed.AppendError(err)
return ErrAlertFilterRuleDeleteFailed.AppendError(err)
}
// 删除触发条件
if err := tx.Where("alert_policy_id = ?", policy.ID).Delete(demomod.AlertTriggerRule{}).Error; err != nil {
logging.CtxLogger(c).Error("ModifyAlertPolicy Delete trigger rules error", zap.Error(err))
return nil, ErrAlertTriggerRuleDeleteFailed.AppendError(err)
return ErrAlertTriggerRuleDeleteFailed.AppendError(err)
}

// 更新关联数据
tx.Model(rawPolicy).Association("AlertFilterRules").Replace(policy.AlertFilterRules)
tx.Model(rawPolicy).Association("AlertTriggerRules").Replace(policy.AlertTriggerRules)

// 保存更新
if err := tx.Save(rawPolicy).Error; err != nil {
logging.CtxLogger(c).Error("ModifyAlertPolicy Save rules error", zap.Error(err))
return nil, ErrAlertPolicyModifyFailed.AppendError(err)
// 插入新的关联数据
for _, r := range policy.AlertFilterRules {
r.AlertPolicyID = policy.ID
if err := tx.Create(r).Error; err != nil {
logging.CtxLogger(c).Error(err.Error())
return ErrAlertPolicyModifyFailed.AppendError(err)
}
}
for _, r := range policy.AlertTriggerRules {
r.AlertPolicyID = policy.ID
if err := tx.Create(r).Error; err != nil {
logging.CtxLogger(c).Error(err.Error())
return ErrAlertPolicyModifyFailed.AppendError(err)
}
}

// 提交事务
if err := tx.Commit().Error; err != nil {
logging.CtxLogger(c).Error("ModifyAlertPolicy Commit error", zap.Error(err))
return nil, ErrAlertPolicyModifyFailed.AppendError(err)
return ErrAlertPolicyModifyFailed.AppendError(err)
}
return rawPolicy, nil
return nil
}

// DescribeAlertPolicies 查询告警策略列表
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package demosvc
package demohdl

import (
"fmt"
Expand Down Expand Up @@ -304,7 +304,7 @@ func TestModifyAlertPolicy(t *testing.T) {
ContinuousCycleCount: 1,
}
p.AlertTriggerRules = []*demomod.AlertTriggerRule{tr2, ntr}
mPolicy, err := ModifyAlertPolicy(utctx, utdb, p)
err = ModifyAlertPolicy(utctx, utdb, p)
if err != nil {
t.Fatal("ModifyAlertPolicy err:", err)
}
Expand All @@ -331,10 +331,10 @@ func TestModifyAlertPolicy(t *testing.T) {
}

// 检查修改返回的结果和查询修改后的结果是否一致
if qPolicy.AlertChannel != mPolicy.AlertChannel {
if qPolicy.AlertChannel != p.AlertChannel {
t.Fatal("TestModifyAlertPolicy 对比更新返回的policy和查询的policy字段失败")
}
if qPolicy.AlertTriggerRules[0].Relation != mPolicy.AlertTriggerRules[0].Relation {
if qPolicy.AlertTriggerRules[0].Relation != tr2.Relation {
t.Fatal("TestModifyAlertPolicy 对比更新返回的关联条件的字段失败")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 业务自身的错误码 (云API子错误码)

package demosvc
package demohdl

import "pink-lady/app/response"

Expand Down
2 changes: 2 additions & 0 deletions app/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package handlers save common business logic code
package handlers
1 change: 1 addition & 0 deletions app/handlers/handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package handlers
12 changes: 12 additions & 0 deletions app/logging/ctxlogger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package logging

import (
"net/http"
"net/http/httptest"

"github.com/gin-gonic/gin"
uuid "github.com/satori/go.uuid"
"go.uber.org/zap"
Expand All @@ -13,6 +16,15 @@ const (
RequestIDKey = "X-Request-Id"
)

// GenGinContext 生成gin.context
func GenGinContext(logger *zap.Logger, requestID string) *gin.Context {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("GET", "http://fake.url", nil)
SetCtxLogger(c, logger)
SetCtxRequestID(c, requestID)
return c
}

// SetCtxLogger add a logger with given field into the gin.Context
// and set requestid field get from context
func SetCtxLogger(c *gin.Context, ctxLogger *zap.Logger) {
Expand Down
Loading

0 comments on commit d1a8023

Please sign in to comment.