@@ -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
@@ -65,18 +67,32 @@ type discordEmbedField struct {
65
67
Value string `json:"value"`
66
68
}
67
69
70
+ const defaultListenAddress = "127.0.0.1:9094"
71
+
68
72
func main () {
69
- webhookUrl := os .Getenv ("DISCORD_WEBHOOK" )
70
- whURL := flag .String ("webhook.url" , webhookUrl , "" )
73
+ envWhURL := os .Getenv ("DISCORD_WEBHOOK" )
74
+ whURL := flag .String ("webhook.url" , envWhURL , "Discord WebHook URL." )
75
+
76
+ envListenAddress := os .Getenv ("LISTEN_ADDRESS" )
77
+ listenAddress := flag .String ("listen.address" , envListenAddress , "Address:Port to listen on." )
78
+
71
79
flag .Parse ()
72
80
73
- if webhookUrl == "" && * whURL == "" {
74
- fmt .Fprintf (os .Stderr , "error: environment variable DISCORD_WEBHOOK not found\n " )
75
- os .Exit (1 )
81
+ if * whURL == "" {
82
+ log .Fatalf ("Environment variable 'DISCORD_WEBHOOK' or CLI parameter 'webhook.url' not found." )
83
+ }
84
+
85
+ if * listenAddress == "" {
86
+ * listenAddress = defaultListenAddress
87
+ }
88
+
89
+ re := regexp .MustCompile (`https://discord(?:app)?.com/api/webhooks/[0-9]{18}/[a-zA-Z0-9_-]+` )
90
+ if ok := re .Match ([]byte (* whURL )); ! ok {
91
+ log .Fatalf ("The Discord WebHook URL doesn't seem to be valid." )
76
92
}
77
93
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 ) {
94
+ log . Printf ( " Listening on: %s" , * listenAddress )
95
+ http .ListenAndServe (* listenAddress , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
80
96
b , err := ioutil .ReadAll (r .Body )
81
97
if err != nil {
82
98
panic (err )
0 commit comments