增加字符串批量替换函数
parent
a780076a57
commit
317741b187
|
@ -7,8 +7,9 @@ import (
|
||||||
var SlackUrl string
|
var SlackUrl string
|
||||||
|
|
||||||
type Message map[string]interface{}
|
type Message map[string]interface{}
|
||||||
|
|
||||||
type Notifiable interface {
|
type Notifiable interface {
|
||||||
send(msg Message)
|
Send(msg Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
var queue chan Message = make(chan Message, 100)
|
var queue chan Message = make(chan Message, 100)
|
||||||
|
@ -25,7 +26,7 @@ func Push(msg Message) {
|
||||||
func run() {
|
func run() {
|
||||||
slack := new(Slack)
|
slack := new(Slack)
|
||||||
for msg := range queue {
|
for msg := range queue {
|
||||||
// 根据任务配置执行相应通知
|
// 根据任务配置发送通知
|
||||||
go slack.Send(msg)
|
go slack.Send(msg)
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package notify
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"github.com/ouqiang/gocron/modules/httpclient"
|
"github.com/ouqiang/gocron/modules/httpclient"
|
||||||
"github.com/ouqiang/gocron/modules/logger"
|
"github.com/ouqiang/gocron/modules/logger"
|
||||||
"github.com/ouqiang/gocron/modules/utils"
|
"github.com/ouqiang/gocron/modules/utils"
|
||||||
|
@ -45,9 +44,7 @@ func (slack *Slack) format(content string) string {
|
||||||
content = utils.EscapeJson(content)
|
content = utils.EscapeJson(content)
|
||||||
specialChars := []string{"&", "<", ">"}
|
specialChars := []string{"&", "<", ">"}
|
||||||
replaceChars := []string{"&", "<", ">"}
|
replaceChars := []string{"&", "<", ">"}
|
||||||
for i, v := range specialChars {
|
content = utils.ReplaceStrings(content, specialChars, replaceChars)
|
||||||
content = strings.Replace(content, v, replaceChars[i], 1000)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf(`{"text":"%s","username":"监控"}`, content)
|
return fmt.Sprintf(`{"text":"%s","username":"监控"}`, content)
|
||||||
}
|
}
|
|
@ -21,8 +21,6 @@ func ExecShellWithTimeout(timeout int, command string, args... string) (string,
|
||||||
output ,err := cmd.CombinedOutput()
|
output ,err := cmd.CombinedOutput()
|
||||||
return string(output), err
|
return string(output), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
d := time.Duration(timeout) * time.Second
|
d := time.Duration(timeout) * time.Second
|
||||||
timer := time.AfterFunc(d, func() {
|
timer := time.AfterFunc(d, func() {
|
||||||
// 超时kill进程
|
// 超时kill进程
|
||||||
|
@ -75,13 +73,26 @@ func GBK2UTF8(s string) (string, bool) {
|
||||||
return dec.ConvertStringOK(s)
|
return dec.ConvertStringOK(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 转义json特殊字符
|
// 批量替换字符串
|
||||||
func EscapeJson(s string) string {
|
func ReplaceStrings(s string, old []string, replace []string) string {
|
||||||
specialChars := []string{"\\", "\b","\f", "\n", "\r", "\t", "\"",}
|
if s == "" {
|
||||||
replaceChars := []string{ "\\\\", "\\b", "\\f", "\\n", "\\r", "\\t", "\\\"",}
|
return s
|
||||||
for i, v := range specialChars {
|
}
|
||||||
s = strings.Replace(s, v, replaceChars[i], 1000)
|
if len(old) != len(replace) {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, v := range old {
|
||||||
|
s = strings.Replace(s, v, replace[i], 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 转义json特殊字符
|
||||||
|
func EscapeJson(s string) string {
|
||||||
|
specialChars := []string{"\\", "\b","\f", "\n", "\r", "\t", "\"",}
|
||||||
|
replaceChars := []string{ "\\\\", "\\b", "\\f", "\\n", "\\r", "\\t", "\\\"",}
|
||||||
|
|
||||||
|
return ReplaceStrings(s, specialChars, replaceChars)
|
||||||
|
}
|
|
@ -69,7 +69,7 @@ func Register(m *macaron.Macaron) {
|
||||||
m.Group("/admin", func() {
|
m.Group("/admin", func() {
|
||||||
m.Group("/setting/", func() {
|
m.Group("/setting/", func() {
|
||||||
m.Get("/slack", setting.EditSlack)
|
m.Get("/slack", setting.EditSlack)
|
||||||
|
m.Post("/slack", setting.StoreSlack)
|
||||||
})
|
})
|
||||||
}, adminAuth)
|
}, adminAuth)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue