mirror of
https://github.com/lollipopkit/server_box_monitor.git
synced 2025-01-07 03:17:06 +08:00
#5 new: bark
push
This commit is contained in:
parent
8fefb56032
commit
8f5e4043cf
48
cmd/conf.go
48
cmd/conf.go
@ -1,10 +1,6 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
// "strings"
|
||||
|
||||
// "github.com/lollipopkit/gommon/res"
|
||||
// "github.com/lollipopkit/gommon/term"
|
||||
"github.com/lollipopkit/server_box_monitor/model"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
@ -21,12 +17,6 @@ func init() {
|
||||
Usage: "Initialize config file",
|
||||
Action: handleConfInit,
|
||||
},
|
||||
// {
|
||||
// Name: "edit",
|
||||
// Aliases: []string{"e"},
|
||||
// Usage: "Edit config file",
|
||||
// Action: handleConfEdit,
|
||||
// },
|
||||
},
|
||||
})
|
||||
}
|
||||
@ -34,41 +24,3 @@ func init() {
|
||||
func handleConfInit(c *cli.Context) error {
|
||||
return model.InitConfig()
|
||||
}
|
||||
|
||||
// func handleConfEdit(c *cli.Context) error {
|
||||
// if err := model.ReadAppConfig(); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// typeOptions := []string{"interval", "rate", "name", "rules", "pushes", "exit"}
|
||||
// opOptions := []string{"add", "remove", "edit", "exit"}
|
||||
|
||||
// for {
|
||||
// ruleIds := []string{}
|
||||
// for _, rule := range model.Config.Rules {
|
||||
// ruleIds = append(ruleIds, rule.Id())
|
||||
// }
|
||||
// pushNames := []string{}
|
||||
// for _, push := range model.Config.Pushes {
|
||||
// pushNames = append(pushNames, push.Name)
|
||||
// }
|
||||
// var buf strings.Builder
|
||||
// buf.WriteString(res.GREEN + "interval: " + res.NOCOLOR + model.Config.Interval + "\n")
|
||||
// buf.WriteString(res.GREEN + "rate: " + res.NOCOLOR + model.Config.Rate + "\n")
|
||||
// buf.WriteString(res.GREEN + "name: " + res.NOCOLOR + model.Config.Name + "\n")
|
||||
// buf.WriteString(res.GREEN + "rules: " + res.NOCOLOR + strings.Join(ruleIds, " | ") + "\n")
|
||||
// buf.WriteString(res.GREEN + "pushes: " + res.NOCOLOR + strings.Join(pushNames, " | ") + "\n")
|
||||
// print(buf.String())
|
||||
|
||||
// op := term.Option("What to do?", opOptions, len(opOptions)-1)
|
||||
// if op == 3 {
|
||||
// break
|
||||
// }
|
||||
// question := "Which type to " + opOptions[op] + "?"
|
||||
// typ := term.Option(question, typeOptions, len(typeOptions)-1)
|
||||
// switch typ {
|
||||
// case 0:
|
||||
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
|
2
go.sum
2
go.sum
@ -5,8 +5,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||
github.com/labstack/echo/v4 v4.11.0 h1:4Dmi59tmrnFzOchz4EXuGjJhUfcEkU28iDKsiZVOQgw=
|
||||
github.com/labstack/echo/v4 v4.11.0/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
|
||||
github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
|
||||
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
|
||||
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@ -41,6 +42,12 @@ func (p *Push) GetIface() (PushIface, error) {
|
||||
return nil, err
|
||||
}
|
||||
return iface, nil
|
||||
case PushTypeBark:
|
||||
var iface PushIfaceBark
|
||||
err := json.Unmarshal(p.Iface, &iface)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return nil, errors.New("unknown push type")
|
||||
}
|
||||
@ -98,6 +105,7 @@ const (
|
||||
PushTypeIOS PushType = "ios"
|
||||
PushTypeWebhook = "webhook"
|
||||
PushTypeServerChan = "server_chan"
|
||||
PushTypeBark = "bark"
|
||||
)
|
||||
|
||||
type PushIface interface {
|
||||
@ -218,3 +226,55 @@ func (p PushIfaceServerChan) push(args []*PushPair) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type barkLevel string
|
||||
|
||||
const (
|
||||
barkLevelActive barkLevel = "active"
|
||||
barkLevelSensitive = "timeSensitive"
|
||||
barkLevelPassive = "passive"
|
||||
)
|
||||
|
||||
type PushIfaceBark struct {
|
||||
Server string `json:"server"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Level barkLevel `json:"level"`
|
||||
BodyRegex string `json:"body_regex"`
|
||||
Code int `json:"code"`
|
||||
}
|
||||
|
||||
func (p PushIfaceBark) push(args []*PushPair) error {
|
||||
body := p.Body
|
||||
for _, arg := range args {
|
||||
body = strings.Replace(body, arg.key, arg.value, 1)
|
||||
}
|
||||
if len(p.Server) == 0 {
|
||||
p.Server = "https://api.day.app"
|
||||
}
|
||||
url := path.Join(
|
||||
p.Server,
|
||||
p.Title,
|
||||
body,
|
||||
)
|
||||
if len(p.Level) != 0 {
|
||||
url += fmt.Sprintf("?level=%s", p.Level)
|
||||
}
|
||||
resp, code, err := http.Do("GET", url, nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if p.Code != 0 && code != p.Code {
|
||||
return fmt.Errorf("code: %d, resp: %s", code, string(resp))
|
||||
}
|
||||
if p.BodyRegex != "" {
|
||||
reg, err := regexp.Compile(p.BodyRegex)
|
||||
if err != nil {
|
||||
return fmt.Errorf("compile regex failed: %s", err.Error())
|
||||
}
|
||||
if !reg.Match(resp) {
|
||||
return fmt.Errorf("resp: %s", string(resp))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user