// Checks all enabled Websites func checkAllSites() { // Check for internet-connection if !lib.GetConfiguration().Application.RunCheckIfOffline { res, err := goreq.Request{Uri: "https://google.com", Method: "HEAD", UserAgent: "UpAndRunning2 (https://github.com/MarvinMenzerath/UpAndRunning2)", MaxRedirects: 1, Timeout: 5 * time.Second}.Do() if err != nil { logging.MustGetLogger("").Warning("Did not check Websites because of missing internet-connection: ", err) return } else { if res.StatusCode != 200 { logging.MustGetLogger("").Warning("Did not check Websites because of missing internet-connection.") res.Body.Close() return } } } // Query the Database db := lib.GetDatabase() rows, err := db.Query("SELECT id, protocol, url, checkMethod FROM websites WHERE enabled = 1;") if err != nil { logging.MustGetLogger("").Error("Unable to fetch Websites: ", err) return } defer rows.Close() // Check every Website count := 0 for rows.Next() { var website lib.Website err = rows.Scan(&website.Id, &website.Protocol, &website.Url, &website.CheckMethod) if err != nil { logging.MustGetLogger("").Error("Unable to read Website-Row: ", err) return } go website.RunCheck(false) count++ time.Sleep(time.Millisecond * 200) } // Check for Errors err = rows.Err() if err != nil { logging.MustGetLogger("").Error("Unable to read Website-Rows: ", err) return } logging.MustGetLogger("").Info("Checked " + strconv.Itoa(count) + " active Websites.") }
// Triggers a check of all enabled Websites. func ApiWebsiteCheck(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if !lib.IsLoggedIn(r) { SendJsonMessage(w, http.StatusUnauthorized, false, "Unauthorized.") return } // Get data from Request r.ParseForm() url := ps.ByName("url") // Simple Validation if url == "" { SendJsonMessage(w, http.StatusBadRequest, false, "Unable to process your Request: Submit a valid value.") return } // Query the Database db := lib.GetDatabase() var website lib.Website err := db.QueryRow("SELECT id, protocol, url, checkMethod FROM websites WHERE url = ?;", url).Scan(&website.Id, &website.Protocol, &website.Url, &website.CheckMethod) if err != nil { if err == sql.ErrNoRows { SendJsonMessage(w, http.StatusNotFound, false, "Unable to process your Request: Could not find Website.") return } else { logging.MustGetLogger("").Error("Unable to get Website's notification settings: ", err) SendJsonMessage(w, http.StatusInternalServerError, false, "Unable to process your Request: "+err.Error()) return } } else { // Run the requested check logging.MustGetLogger("").Info("Checking requested Website (" + website.Url + ").") website.RunCheck(false) SendJsonMessage(w, http.StatusOK, true, "") } }