1
1
mirror of https://github.com/cooperspencer/gickup synced 2024-10-18 13:48:07 +02:00
gickup/metrics/ntfy/ntfy.go
2023-05-02 16:40:56 +02:00

43 lines
858 B
Go

package ntfy
import (
"fmt"
"net/http"
"strings"
"github.com/cooperspencer/gickup/types"
)
func Notify(msg string, config types.PushConfig) error {
url := config.Url
payload := strings.NewReader(msg)
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "text/plain")
req.Header.Add("Title", "Backup done")
if config.Token != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", config.Token))
} else if config.Password != "" && config.User != "" {
req.SetBasicAuth(config.User, config.Password)
} else {
return fmt.Errorf("neither user, password and token are set")
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("received status %d from %s", res.StatusCode, config.Url)
}
return nil
}