package handlers import ( "fmt" "github.com/gorilla/mux" "github.com/hunterlong/statup/core" "github.com/hunterlong/statup/utils" "net/http" "strconv" ) func ServicesHandler(w http.ResponseWriter, r *http.Request) { auth := IsAuthenticated(r) if !auth { http.Redirect(w, r, "/", http.StatusSeeOther) return } ExecuteResponse(w, r, "services.html", core.CoreApp.Services) } func CreateServiceHandler(w http.ResponseWriter, r *http.Request) { auth := IsAuthenticated(r) if !auth { http.Redirect(w, r, "/", http.StatusSeeOther) return } fmt.Println("service adding") r.ParseForm() name := r.PostForm.Get("name") domain := r.PostForm.Get("domain") method := r.PostForm.Get("method") expected := r.PostForm.Get("expected") status, _ := strconv.Atoi(r.PostForm.Get("expected_status")) interval, _ := strconv.Atoi(r.PostForm.Get("interval")) port, _ := strconv.Atoi(r.PostForm.Get("port")) checkType := r.PostForm.Get("check_type") service := &core.Service{ Name: name, Domain: domain, Method: method, Expected: expected, ExpectedStatus: status, Interval: interval, Type: checkType, Port: port, } _, err := service.Create() if err != nil { go service.CheckQueue() } http.Redirect(w, r, "/services", http.StatusSeeOther) } func ServicesDeleteHandler(w http.ResponseWriter, r *http.Request) { auth := IsAuthenticated(r) if !auth { http.Redirect(w, r, "/", http.StatusSeeOther) return } vars := mux.Vars(r) service := core.SelectService(utils.StringInt(vars["id"])) service.Delete() http.Redirect(w, r, "/services", http.StatusSeeOther) } func ServicesViewHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) service := core.SelectService(utils.StringInt(vars["id"])) ExecuteResponse(w, r, "service.html", service) } func ServicesBadgeHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) service := core.SelectService(utils.StringInt(vars["id"])) var badge []byte if service.Online { badge = []byte(`` + service.Name + `` + service.Name + `onlineonline`) } else { badge = []byte(`` + service.Name + `` + service.Name + `offlineoffline`) } w.Header().Set("Content-Type", "image/svg+xml") w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") w.Write(badge) } func ServicesUpdateHandler(w http.ResponseWriter, r *http.Request) { auth := IsAuthenticated(r) if !auth { http.Redirect(w, r, "/", http.StatusSeeOther) return } vars := mux.Vars(r) service := core.SelectService(utils.StringInt(vars["id"])) r.ParseForm() name := r.PostForm.Get("name") domain := r.PostForm.Get("domain") method := r.PostForm.Get("method") expected := r.PostForm.Get("expected") status, _ := strconv.Atoi(r.PostForm.Get("expected_status")) interval, _ := strconv.Atoi(r.PostForm.Get("interval")) port, _ := strconv.Atoi(r.PostForm.Get("port")) checkType := r.PostForm.Get("check_type") service = &core.Service{ Name: name, Domain: domain, Method: method, Expected: expected, ExpectedStatus: status, Interval: interval, Type: checkType, Port: port, } service.Update() ExecuteResponse(w, r, "service.html", service) } func ServicesDeleteFailuresHandler(w http.ResponseWriter, r *http.Request) { auth := IsAuthenticated(r) if !auth { http.Redirect(w, r, "/", http.StatusSeeOther) return } vars := mux.Vars(r) service := core.SelectService(utils.StringInt(vars["id"])) service.DeleteFailures() core.CoreApp.Services, _ = core.SelectAllServices() http.Redirect(w, r, "/services", http.StatusSeeOther) } func CheckinCreateUpdateHandler(w http.ResponseWriter, r *http.Request) { auth := IsAuthenticated(r) if !auth { http.Redirect(w, r, "/", http.StatusSeeOther) return } vars := mux.Vars(r) interval := utils.StringInt(r.PostForm.Get("interval")) service := core.SelectService(utils.StringInt(vars["id"])) checkin := &core.Checkin{ Service: service.Id, Interval: interval, Api: utils.NewSHA1Hash(18), } checkin.Create() fmt.Println(checkin.Create()) ExecuteResponse(w, r, "service.html", service) }