@@ -6,8 +6,10 @@ import (
6
6
"flag"
7
7
"fmt"
8
8
"io/ioutil"
9
+ "log"
9
10
"net/http"
10
11
"os"
12
+ "regexp"
11
13
"strings"
12
14
)
13
15
@@ -66,17 +68,22 @@ type discordEmbedField struct {
66
68
}
67
69
68
70
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." )
71
74
flag .Parse ()
72
75
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." )
76
78
}
77
79
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 ) {
80
87
b , err := ioutil .ReadAll (r .Body )
81
88
if err != nil {
82
89
panic (err )
0 commit comments