Merge pull request #7 from jaseemabid/jabid/url-parse
Validate ntfy.sh URL before use
This commit is contained in:
commit
c669b87012
1 changed files with 7 additions and 1 deletions
8
main.go
8
main.go
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
|
@ -42,7 +43,7 @@ func WebhookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
req, err := http.NewRequest("POST", os.Getenv("NTFY_TOPIC"), strings.NewReader(alert.Annotations["description"]))
|
||||
if err != nil {
|
||||
log.Printf("Building request to %s failed: %s", req.RemoteAddr, err)
|
||||
log.Printf("Building request failed: %s", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
@ -90,6 +91,11 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
_, err := url.Parse(os.Getenv("NTFY_TOPIC"))
|
||||
if err != nil {
|
||||
log.Fatal("Environment variable NTFY_TOPIC is not a valid URL")
|
||||
}
|
||||
|
||||
http.HandleFunc("/", WebhookHandler)
|
||||
var listenAddr = fmt.Sprintf("%v:%v", os.Getenv("HTTP_ADDRESS"), os.Getenv("HTTP_PORT"))
|
||||
log.Printf("Listening for HTTP requests (webhooks) on %v\n", listenAddr)
|
||||
|
|
Loading…
Reference in a new issue