mirror of
https://github.com/lollipopkit/server_box_monitor.git
synced 2025-01-09 04:10:01 +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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/lollipopkit/gommon/http"
|
"github.com/lollipopkit/gommon/http"
|
||||||
"github.com/lollipopkit/server_box_monitor/res"
|
"github.com/lollipopkit/server_box_monitor/res"
|
||||||
@ -56,6 +55,7 @@ func (p *Push) GetIface() (PushIface, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return iface, nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("unknown push type: %s", p.Type)
|
return nil, fmt.Errorf("unknown push type: %s", p.Type)
|
||||||
}
|
}
|
||||||
@ -72,30 +72,29 @@ type PushFormat string
|
|||||||
type PushPair struct {
|
type PushPair struct {
|
||||||
key string
|
key string
|
||||||
value string
|
value string
|
||||||
time string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPushPair(key, value string) *PushPair {
|
func NewPushPair(key, value string) *PushPair {
|
||||||
return &PushPair{
|
return &PushPair{
|
||||||
key: key,
|
key: key,
|
||||||
value: value,
|
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{}
|
ss := []string{}
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
if len(arg.time) == 0 {
|
kv := fmt.Sprintf("%s: %s", arg.key, arg.value)
|
||||||
arg.time = time.Now().Format("15:04:05")
|
|
||||||
}
|
|
||||||
kv := fmt.Sprintf(`%s\n%s: %s`, arg.time, arg.key, arg.value)
|
|
||||||
ss = append(ss, kv)
|
ss = append(ss, kv)
|
||||||
}
|
}
|
||||||
msgReplaced := strings.Replace(
|
msgReplaced := strings.Replace(
|
||||||
string(pf),
|
string(pf),
|
||||||
res.PushFormatMsgLocator,
|
res.PushFormatMsgLocator,
|
||||||
strings.Join(ss, `\n`),
|
strings.Join(ss, newline),
|
||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
nameReplaced := strings.Replace(
|
nameReplaced := strings.Replace(
|
||||||
@ -120,21 +119,17 @@ type PushIfaceIOS struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p PushIfaceIOS) push(args []*PushPair) error {
|
func (p PushIfaceIOS) push(args []*PushPair) error {
|
||||||
content := p.Content.Format(args)
|
content := p.Content.Format(args, false)
|
||||||
title := p.Title.Format(args)
|
title := p.Title.Format(args, true)
|
||||||
body := map[string]string{
|
body := map[string]string{
|
||||||
"token": p.Token,
|
"token": p.Token,
|
||||||
"title": title,
|
"title": title,
|
||||||
"content": content,
|
"content": content,
|
||||||
}
|
}
|
||||||
bodyBytes, err := json.Marshal(body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
resp, code, err := http.Do(
|
resp, code, err := http.Do(
|
||||||
"POST",
|
"POST",
|
||||||
"https://push.lolli.tech/v1/ios",
|
"https://push.lolli.tech/v1/ios",
|
||||||
bodyBytes,
|
body,
|
||||||
map[string]string{
|
map[string]string{
|
||||||
"AppID": "com.lollipopkit.toolbox",
|
"AppID": "com.lollipopkit.toolbox",
|
||||||
"Content-Type": "application/json",
|
"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 {
|
if p.Code != 0 && code != p.Code {
|
||||||
return fmt.Errorf("code: %d, resp: %s", code, string(resp))
|
return fmt.Errorf("code: %d, resp: %s", code, string(resp))
|
||||||
}
|
}
|
||||||
@ -167,7 +165,7 @@ type PushIfaceWebhook struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p PushIfaceWebhook) push(args []*PushPair) error {
|
func (p PushIfaceWebhook) push(args []*PushPair) error {
|
||||||
body := PushFormat(p.Body).Format(args)
|
body := PushFormat(p.Body).Format(args, true)
|
||||||
switch p.Method {
|
switch p.Method {
|
||||||
case "GET", "POST":
|
case "GET", "POST":
|
||||||
resp, code, err := http.Do(p.Method, p.Url, body, p.Headers)
|
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 {
|
func (p PushIfaceServerChan) push(args []*PushPair) error {
|
||||||
desp := p.Desp.Format(args)
|
desp := p.Desp.Format(args, true)
|
||||||
title := p.Title.Format(args)
|
title := p.Title.Format(args, true)
|
||||||
url := fmt.Sprintf(
|
url := fmt.Sprintf(
|
||||||
"https://sctapi.ftqq.com/%s.send?title=%s&desp=%s",
|
"https://sctapi.ftqq.com/%s.send?title=%s&desp=%s",
|
||||||
p.SCKey,
|
p.SCKey,
|
||||||
@ -246,21 +244,21 @@ type PushIfaceBark struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p PushIfaceBark) push(args []*PushPair) error {
|
func (p PushIfaceBark) push(args []*PushPair) error {
|
||||||
body := p.Body.Format(args)
|
body := p.Body.Format(args, false)
|
||||||
title := p.Title.Format(args)
|
title := p.Title.Format(args, true)
|
||||||
if len(p.Server) == 0 {
|
if len(p.Server) == 0 {
|
||||||
p.Server = "https://api.day.app"
|
p.Server = "https://api.day.app"
|
||||||
}
|
}
|
||||||
url := path.Join(
|
if strings.HasSuffix("/", p.Server) {
|
||||||
p.Server,
|
p.Server = p.Server[:len(p.Server)-1]
|
||||||
p.Key,
|
|
||||||
title,
|
|
||||||
body,
|
|
||||||
)
|
|
||||||
if len(p.Level) != 0 {
|
|
||||||
url += fmt.Sprintf("?level=%s", p.Level)
|
|
||||||
}
|
}
|
||||||
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -91,10 +91,11 @@ func runCheck() {
|
|||||||
model.RateLimiter.Acquire(push.Name)
|
model.RateLimiter.Acquire(push.Name)
|
||||||
log.Suc("[PUSH] %s success", 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