Skip to content

Commit 03a58c8

Browse files
author
Julien Vallée
committed
Add listen.address as a CLI parameter and check WebHook
1 parent 2e7369c commit 03a58c8

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

main.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"flag"
77
"fmt"
88
"io/ioutil"
9+
"log"
910
"net/http"
1011
"os"
12+
"regexp"
1113
"strings"
1214
)
1315

@@ -66,17 +68,22 @@ type discordEmbedField struct {
6668
}
6769

6870
func main() {
69-
webhookUrl := os.Getenv("DISCORD_WEBHOOK")
70-
whURL := flag.String("webhook.url", webhookUrl, "")
71+
envWhURL := os.Getenv("DISCORD_WEBHOOK")
72+
whURL := flag.String("webhook.url", envWhURL, "Discord WebHook URL.")
73+
listenAddress := flag.String("listen.address", "127.0.0.1:9094", "Address:Port to listen on.")
7174
flag.Parse()
7275

73-
if webhookUrl == "" && *whURL == "" {
74-
fmt.Fprintf(os.Stderr, "error: environment variable DISCORD_WEBHOOK not found\n")
75-
os.Exit(1)
76+
if *whURL == "" {
77+
log.Fatalf("Environment variable 'DISCORD_WEBHOOK' or CLI parameter 'webhook.url' not found.")
7678
}
7779

78-
fmt.Fprintf(os.Stdout, "info: Listening on 0.0.0.0:9094\n")
79-
http.ListenAndServe(":9094", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
80+
re := regexp.MustCompile(`https://discord(?:app)?.com/api/webhooks/[0-9]{18}/[a-zA-Z0-9_-]+`)
81+
if ok := re.Match([]byte(*whURL)); !ok {
82+
log.Fatalf("The Discord WebHook URL doesn't seem to be valid.")
83+
}
84+
85+
log.Printf("Listening on: %s", *listenAddress)
86+
http.ListenAndServe(*listenAddress, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8087
b, err := ioutil.ReadAll(r.Body)
8188
if err != nil {
8289
panic(err)

0 commit comments

Comments
 (0)