Skip to content

Commit

Permalink
支持自定义bark server
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Apr 17, 2022
1 parent 6f6dc08 commit 312b681
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bark:
key: "" # Bark 通知推送的 Key
server: "https://api.day.app" # 自定义 Bark Server 地址, 不填默认为 https://api.day.app
key: "" # Bark 通知推送的 Key
push_plus:
token: "" # Push Plus 通知推送的 Token
regular:
Expand Down
16 changes: 11 additions & 5 deletions pkg/notice/bark.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@ import (
"fmt"
"io/ioutil"
"net/http"
"path"
)

const barkURL = "https://api.day.app/push"

type BarkConfig struct {
Key string `json:"key"`
Server string `json:"server"`
Key string `json:"key"`
}

func NewBark(cfg *BarkConfig) Engine {
if cfg.Key == "" {
return nil
}
return &bark{key: cfg.Key}
return &bark{cfg: cfg}
}

type bark struct {
key string
cfg *BarkConfig
}

func (b *bark) Name() string {
Expand All @@ -45,7 +47,7 @@ func (b *bark) Name() string {

func (b *bark) Send(title, body string) error {
data := &barkData{
DeviceKey: b.key,
DeviceKey: b.cfg.Key,
Title: title,
Body: body,
Sound: "alarm.caf",
Expand All @@ -55,7 +57,11 @@ func (b *bark) Send(title, body string) error {
return err
}

resp, err := http.Post(barkURL, "application/json; charset=utf-8", bytes.NewReader(bs))
uri := barkURL
if b.cfg.Server != "" {
uri = path.Join(b.cfg.Server, "push")
}
resp, err := http.Post(uri, "application/json; charset=utf-8", bytes.NewReader(bs))
if err != nil {
return err
}
Expand Down

0 comments on commit 312b681

Please sign in to comment.