Refactoring: Split webhook url check into func

This commit is contained in:
nerves_dev
2021-03-08 16:08:15 +09:00
parent b504e7f09c
commit 49e3076d4e
+13 -11
View File
@@ -75,26 +75,28 @@ var (
listenAddress = flag.String("listen.address", os.Getenv("LISTEN_ADDRESS"), "Address:Port to listen on.")
)
func main() {
flag.Parse()
if *whURL == "" {
func checkWhURL(whURL string) {
if whURL == "" {
log.Fatalf("Environment variable 'DISCORD_WEBHOOK' or CLI parameter 'webhook.url' not found.")
}
if *listenAddress == "" {
*listenAddress = defaultListenAddress
}
_, err := url.Parse(*whURL)
_, err := url.Parse(whURL)
if err != nil {
log.Fatalf("The Discord WebHook URL doesn't seem to be a valid URL.")
}
re := regexp.MustCompile(`https://discord(?:app)?.com/api/webhooks/[0-9]{18}/[a-zA-Z0-9_-]+`)
if ok := re.Match([]byte(*whURL)); !ok {
if ok := re.Match([]byte(whURL)); !ok {
log.Printf("The Discord WebHook URL doesn't seem to be valid.")
}
}
func main() {
flag.Parse()
checkWhURL(*whURL)
if *listenAddress == "" {
*listenAddress = defaultListenAddress
}
log.Printf("Listening on: %s", *listenAddress)
http.ListenAndServe(*listenAddress, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {