func setEnabledRoute(c web.C, w http.ResponseWriter, r *http.Request) { status := common.GetStatus() enabled, _ := strconv.Atoi(r.URL.Query().Get("enabled")) status.Disabled = enabled != 1 log.Printf("setting disabled to %t\n", status.Disabled) common.SetStatus(status) http.Redirect(w, r, "/", http.StatusFound) }
func pingSite(c *cli.Context) { simulateDown := c.GlobalBool("down") config := common.GetConfig() status := common.GetStatus() if config.URL == "" { return } defer func() { if e := recover(); e != nil { switch x := e.(type) { case error: common.HipchatMessage(x.Error()) default: common.HipchatMessage(fmt.Sprintf("%v", x)) } panic(e) } }() client := &http.Client{ Timeout: time.Duration(20 * time.Second), } req, err := http.NewRequest("GET", config.URL, nil) if err != nil { log.Fatalln(err) } req.Close = true req.Header.Set("User-Agent", "SupPinger") isError := false statusCode := 0 resp, err := client.Do(req) if err != nil && err != io.EOF { fmt.Printf("err: %+v\n", err) fmt.Printf("resp: %+v\n", resp) isError = true } if resp != nil { defer resp.Body.Close() statusCode = resp.StatusCode } if simulateDown || isError || statusCode != http.StatusOK { log.Println("Site is down. Status is ", statusCode) status.NumErrors++ if status.NumErrors >= 5 && !status.Disabled { callDevTeam() } } else { status.NumErrors = 0 } status.LastStatus = statusCode status.LastRunAt = time.Now() common.SetStatus(status) }