提交 22ee99f2 编写于 作者: U UlricQin

Merge branch 'master' of https://github.com/didi/nightingale

......@@ -104,3 +104,7 @@ wechat:
corp_id: "xxxxxxxxxxxxx"
agent_id: 1000000
secret: "xxxxxxxxxxxxxxxxx"
#webhook:
# - addr: http://localhost:2008/api/event
# token: "1234"
......@@ -21,6 +21,12 @@ type ConfigT struct {
WeChat wechatSection `yaml:"wechat"`
I18n i18n.I18nSection `yaml:"i18n"`
Auth authSection `yaml:"auth"`
Webhook []webhook `yaml:"webhook"`
}
type webhook struct {
Addr string `yaml:"addr"`
Token string `yaml:"token"`
}
type authSection struct {
......
......@@ -2,11 +2,15 @@ package http
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/net/httplib"
"github.com/toolkits/pkg/str"
"github.com/didi/nightingale/src/models"
"github.com/didi/nightingale/src/modules/rdb/config"
)
func nodeGet(c *gin.Context) {
......@@ -154,6 +158,7 @@ func nodePost(c *gin.Context) {
dangerous(models.NodeNew(node, f.AdminIds))
go models.OperationLogNew(me.Username, "node", node.Id, fmt.Sprintf("NodeCreate path: %s, name: %s", node.Path, node.Name))
go nodeEvent("create", node)
// 把节点详情返回,便于前端易用性处理
renderData(c, node, nil)
......@@ -175,6 +180,7 @@ func nodePost(c *gin.Context) {
child, err := parent.CreateChild(f.Ident, f.Name, f.Note, f.Cate, me.Username, f.Leaf, f.Proxy, f.AdminIds)
if err == nil {
go models.OperationLogNew(me.Username, "node", child.Id, fmt.Sprintf("NodeCreate path: %s, name: %s", child.Path, child.Name))
go nodeEvent("create", child)
}
renderData(c, child, err)
}
......@@ -206,6 +212,7 @@ func nodePut(c *gin.Context) {
err := node.Modify(f.Name, f.Cate, f.Note, f.AdminIds)
go models.OperationLogNew(me.Username, "node", node.Id, fmt.Sprintf("NodeModify path: %s, name: %s clientIP: %s", node.Path, node.Name, c.ClientIP()))
go nodeEvent("update", node)
renderData(c, node, err)
}
......@@ -217,5 +224,26 @@ func nodeDel(c *gin.Context) {
dangerous(node.Del())
go models.OperationLogNew(me.Username, "node", node.Id, fmt.Sprintf("NodeDelete path: %s, name: %s clientIP: %s", node.Path, node.Name, c.ClientIP()))
go nodeEvent("delete", node)
renderMessage(c, nil)
}
type eventEntity struct {
Action string `json:"action"`
Kind string `json:"kind"`
Data interface{} `json:"data"`
}
func nodeEvent(action string, node *models.Node) {
for _, backend := range config.Config.Webhook {
header := map[string]string{}
if backend.Token != "" {
header["Authorization"] = "Bearer " + backend.Token
}
event := &eventEntity{Action: action, Kind: "node", Data: node}
res, code, err := httplib.PostJSON(backend.Addr, 3*time.Second, event, header)
logger.Infof("Webhook, api:%s, event:%+v, error:%v, response:%s, statuscode:%d",
backend.Addr, event, err, string(res), code)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册