mirror of
https://github.com/lollipopkit/server_box_monitor.git
synced 2025-01-08 11:47:36 +08:00
#7 fix & opt.
- opt.: no time in PushPair - fix: `bark` push - opt.: newline - fix: clear PushPairs after all pushed
This commit is contained in:
parent
c521a4087a
commit
9cd4ec1f0a
@ -3,10 +3,9 @@ package model
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lollipopkit/gommon/http"
|
||||
"github.com/lollipopkit/server_box_monitor/res"
|
||||
@ -56,6 +55,7 @@ func (p *Push) GetIface() (PushIface, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return iface, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unknown push type: %s", p.Type)
|
||||
}
|
||||
@ -72,30 +72,29 @@ type PushFormat string
|
||||
type PushPair struct {
|
||||
key string
|
||||
value string
|
||||
time string
|
||||
}
|
||||
|
||||
func NewPushPair(key, value string) *PushPair {
|
||||
return &PushPair{
|
||||
key: key,
|
||||
value: value,
|
||||
time: time.Now().Format("15:04:05"),
|
||||
}
|
||||
}
|
||||
|
||||
func (pf PushFormat) Format(args []*PushPair) string {
|
||||
func (pf PushFormat) Format(args []*PushPair, raw bool) string {
|
||||
newline := `\n`
|
||||
if !raw {
|
||||
newline = "\n"
|
||||
}
|
||||
ss := []string{}
|
||||
for _, arg := range args {
|
||||
if len(arg.time) == 0 {
|
||||
arg.time = time.Now().Format("15:04:05")
|
||||
}
|
||||
kv := fmt.Sprintf(`%s\n%s: %s`, arg.time, arg.key, arg.value)
|
||||
kv := fmt.Sprintf("%s: %s", arg.key, arg.value)
|
||||
ss = append(ss, kv)
|
||||
}
|
||||
msgReplaced := strings.Replace(
|
||||
string(pf),
|
||||
res.PushFormatMsgLocator,
|
||||
strings.Join(ss, `\n`),
|
||||
strings.Join(ss, newline),
|
||||
1,
|
||||
)
|
||||
nameReplaced := strings.Replace(
|
||||
@ -120,21 +119,17 @@ type PushIfaceIOS struct {
|
||||
}
|
||||
|
||||
func (p PushIfaceIOS) push(args []*PushPair) error {
|
||||
content := p.Content.Format(args)
|
||||
title := p.Title.Format(args)
|
||||
content := p.Content.Format(args, false)
|
||||
title := p.Title.Format(args, true)
|
||||
body := map[string]string{
|
||||
"token": p.Token,
|
||||
"title": title,
|
||||
"content": content,
|
||||
}
|
||||
bodyBytes, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, code, err := http.Do(
|
||||
"POST",
|
||||
"https://push.lolli.tech/v1/ios",
|
||||
bodyBytes,
|
||||
body,
|
||||
map[string]string{
|
||||
"AppID": "com.lollipopkit.toolbox",
|
||||
"Content-Type": "application/json",
|
||||
@ -142,6 +137,9 @@ func (p PushIfaceIOS) push(args []*PushPair) error {
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if p.Code != 0 && code != p.Code {
|
||||
return fmt.Errorf("code: %d, resp: %s", code, string(resp))
|
||||
}
|
||||
@ -167,7 +165,7 @@ type PushIfaceWebhook struct {
|
||||
}
|
||||
|
||||
func (p PushIfaceWebhook) push(args []*PushPair) error {
|
||||
body := PushFormat(p.Body).Format(args)
|
||||
body := PushFormat(p.Body).Format(args, true)
|
||||
switch p.Method {
|
||||
case "GET", "POST":
|
||||
resp, code, err := http.Do(p.Method, p.Url, body, p.Headers)
|
||||
@ -200,8 +198,8 @@ type PushIfaceServerChan struct {
|
||||
}
|
||||
|
||||
func (p PushIfaceServerChan) push(args []*PushPair) error {
|
||||
desp := p.Desp.Format(args)
|
||||
title := p.Title.Format(args)
|
||||
desp := p.Desp.Format(args, true)
|
||||
title := p.Title.Format(args, true)
|
||||
url := fmt.Sprintf(
|
||||
"https://sctapi.ftqq.com/%s.send?title=%s&desp=%s",
|
||||
p.SCKey,
|
||||
@ -246,21 +244,21 @@ type PushIfaceBark struct {
|
||||
}
|
||||
|
||||
func (p PushIfaceBark) push(args []*PushPair) error {
|
||||
body := p.Body.Format(args)
|
||||
title := p.Title.Format(args)
|
||||
body := p.Body.Format(args, false)
|
||||
title := p.Title.Format(args, true)
|
||||
if len(p.Server) == 0 {
|
||||
p.Server = "https://api.day.app"
|
||||
}
|
||||
url := path.Join(
|
||||
p.Server,
|
||||
p.Key,
|
||||
title,
|
||||
body,
|
||||
)
|
||||
if len(p.Level) != 0 {
|
||||
url += fmt.Sprintf("?level=%s", p.Level)
|
||||
if strings.HasSuffix("/", p.Server) {
|
||||
p.Server = p.Server[:len(p.Server)-1]
|
||||
}
|
||||
resp, code, err := http.Do("GET", url, nil, nil)
|
||||
titleEscape := url.QueryEscape(title)
|
||||
bodyEscape := url.QueryEscape(body)
|
||||
url_ := fmt.Sprintf(
|
||||
"%s/%s/%s/%s",
|
||||
p.Server, p.Key, titleEscape, bodyEscape,
|
||||
)
|
||||
resp, code, err := http.Do("GET", url_, nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -91,10 +91,11 @@ func runCheck() {
|
||||
model.RateLimiter.Acquire(push.Name)
|
||||
log.Suc("[PUSH] %s success", push.Name)
|
||||
|
||||
pushPairsLock.Lock()
|
||||
pushPairs = pushPairs[:0]
|
||||
pushPairsLock.Unlock()
|
||||
}
|
||||
|
||||
pushPairsLock.Lock()
|
||||
pushPairs = pushPairs[:0]
|
||||
pushPairsLock.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user