增加字符串批量替换函数

pull/21/merge
ouqiang 2017-04-28 14:55:24 +08:00
parent a780076a57
commit 317741b187
4 changed files with 22 additions and 13 deletions

View File

@ -7,8 +7,9 @@ import (
var SlackUrl string
type Message map[string]interface{}
type Notifiable interface {
send(msg Message)
Send(msg Message)
}
var queue chan Message = make(chan Message, 100)
@ -25,7 +26,7 @@ func Push(msg Message) {
func run() {
slack := new(Slack)
for msg := range queue {
// 根据任务配置执行相应通知
// 根据任务配置发送通知
go slack.Send(msg)
time.Sleep(1 * time.Second)
}

View File

@ -3,7 +3,6 @@ package notify
import (
"fmt"
"strings"
"github.com/ouqiang/gocron/modules/httpclient"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/utils"
@ -45,9 +44,7 @@ func (slack *Slack) format(content string) string {
content = utils.EscapeJson(content)
specialChars := []string{"&", "<", ">"}
replaceChars := []string{"&amp;", "&lt;", "&gt;"}
for i, v := range specialChars {
content = strings.Replace(content, v, replaceChars[i], 1000)
}
content = utils.ReplaceStrings(content, specialChars, replaceChars)
return fmt.Sprintf(`{"text":"%s","username":"监控"}`, content)
}

View File

@ -21,8 +21,6 @@ func ExecShellWithTimeout(timeout int, command string, args... string) (string,
output ,err := cmd.CombinedOutput()
return string(output), err
}
d := time.Duration(timeout) * time.Second
timer := time.AfterFunc(d, func() {
// 超时kill进程
@ -75,13 +73,26 @@ func GBK2UTF8(s string) (string, bool) {
return dec.ConvertStringOK(s)
}
// 批量替换字符串
func ReplaceStrings(s string, old []string, replace []string) string {
if s == "" {
return s
}
if len(old) != len(replace) {
return s
}
for i, v := range old {
s = strings.Replace(s, v, replace[i], 1000)
}
return s
}
// 转义json特殊字符
func EscapeJson(s string) string {
specialChars := []string{"\\", "\b","\f", "\n", "\r", "\t", "\"",}
replaceChars := []string{ "\\\\", "\\b", "\\f", "\\n", "\\r", "\\t", "\\\"",}
for i, v := range specialChars {
s = strings.Replace(s, v, replaceChars[i], 1000)
}
return s
return ReplaceStrings(s, specialChars, replaceChars)
}

View File

@ -69,7 +69,7 @@ func Register(m *macaron.Macaron) {
m.Group("/admin", func() {
m.Group("/setting/", func() {
m.Get("/slack", setting.EditSlack)
m.Post("/slack", setting.StoreSlack)
})
}, adminAuth)