diff --git a/config.go b/config.go index 02c22f5..7f9032e 100644 --- a/config.go +++ b/config.go @@ -9,7 +9,13 @@ import ( type config struct { HTTPAddress string LogLevel string - Topic string + ntfy ntfyConfig +} + +type ntfyConfig struct { + Topic string + User string + Password string } func readConfig(path string) (*config, error) { @@ -37,13 +43,32 @@ func readConfig(path string) (*config, error) { } } - d = cfg.Get("topic") - if d == nil { - return nil, fmt.Errorf("%q missing from config", "topic") + ntfyDir := cfg.Get("ntfy") + if ntfyDir == nil { + return nil, fmt.Errorf("%q directive missing", "ntfy") } - if err := d.ParseParams(&config.Topic); err != nil { + + d = ntfyDir.Children.Get("topic") + if d == nil { + return nil, fmt.Errorf("%q missing from %q directive", "topic", "ntfy") + } + if err := d.ParseParams(&config.ntfy.Topic); err != nil { return nil, err } + d = ntfyDir.Children.Get("user") + if d != nil { + if err := d.ParseParams(&config.ntfy.User); err != nil { + return nil, err + } + } + + d = ntfyDir.Children.Get("password") + if d != nil { + if err := d.ParseParams(&config.ntfy.Password); err != nil { + return nil, err + } + } + return config, nil } diff --git a/main.go b/main.go index e00b773..2810ef2 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/base64" "encoding/json" "flag" "fmt" @@ -83,11 +84,17 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) { } client := &http.Client{Timeout: time.Second * 3} - req, err := http.NewRequest(http.MethodPost, rcv.cfg.Topic, strings.NewReader(body)) + req, err := http.NewRequest(http.MethodPost, rcv.cfg.ntfy.Topic, strings.NewReader(body)) if err != nil { rcv.logger.Error(err) } + // Basic auth + if rcv.cfg.ntfy.Password != "" && rcv.cfg.ntfy.User != "" { + auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", rcv.cfg.ntfy.User, rcv.cfg.ntfy.Password))) + req.Header.Set("Authorization", fmt.Sprintf("Basic %s", auth)) + } + req.Header.Set("X-Title", title) resp, err := client.Do(req) if err != nil {