Skip to content

Commit 462a419

Browse files
authored
Merge pull request benjojo#9 from FinweVI/master
Add listen.address as a CLI parameter and check WebHook
2 parents 2e7369c + c650e0c commit 462a419

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ COPY --from=builder /go/bin/alertmanager-discord /go/bin/alertmanager-discord
2424

2525
EXPOSE 9094
2626
USER appuser
27-
ENTRYPOINT ["/go/bin/alertmanager-discord"]
27+
ENTRYPOINT ["/go/bin/alertmanager-discord", "-listen.address", "0.0.0.0:9094"]
2828

main.go

Lines changed: 23 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

@@ -65,18 +67,32 @@ type discordEmbedField struct {
6567
Value string `json:"value"`
6668
}
6769

70+
const defaultListenAddress = "127.0.0.1:9094"
71+
6872
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+
7179
flag.Parse()
7280

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.")
7692
}
7793

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) {
8096
b, err := ioutil.ReadAll(r.Body)
8197
if err != nil {
8298
panic(err)

0 commit comments

Comments
 (0)