Last active
May 6, 2020 20:08
-
-
Save jasonmccallister/8f4eafda22e4507663a7e45242fc33f8 to your computer and use it in GitHub Desktop.
website-status-check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
func main() { | |
wait := flag.Int("wait", 3, "the number of seconds to wait when checking the URL") | |
flag.Parse() | |
args := os.Args | |
if len(args) < 1 { | |
fmt.Println("you must pass a URL to monitor as the first argument") | |
flag.PrintDefaults() | |
os.Exit(1) | |
} | |
url := args[1] | |
var status int | |
for status != http.StatusOK { | |
resp, err := http.Get(url) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if resp.StatusCode == 200 { | |
status = resp.StatusCode | |
break | |
} | |
log.Println(fmt.Sprintf("received the response code %d from %s", resp.StatusCode, url)) | |
time.Sleep(time.Duration(*wait) * time.Second) | |
} | |
log.Println(fmt.Sprintf("received the response code 200 from %s", url)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment