1
1
mirror of https://github.com/cooperspencer/gickup synced 2024-10-18 20:00:08 +02:00
gickup/metrics/gotify/gotify.go

43 lines
790 B
Go
Raw Normal View History

2023-05-02 16:40:56 +02:00
package gotify
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/cooperspencer/gickup/types"
)
func Notify(msg string, config types.PushConfig) error {
if !strings.HasSuffix(config.Url, "/") {
config.Url += "/"
}
url := fmt.Sprintf("%smessage?token=%s", config.Url, config.Token)
payload := map[string]string{}
payload["message"] = msg
payload["title"] = "Backup done"
body, err := json.Marshal(payload)
if err != nil {
return err
}
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body))
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("received status %d from %s", res.StatusCode, config.Url)
}
return nil
}