package handlers import ( "fmt" "github.com/gorilla/mux" "github.com/hunterlong/statup/core" "github.com/hunterlong/statup/utils" "net/http" "strconv" ) func RenderServiceChartsHandler(w http.ResponseWriter, r *http.Request) { services := core.CoreApp.Services w.Header().Set("Content-Type", "text/javascript") w.Header().Set("Cache-Control", "max-age=60") ExecuteJSResponse(w, r, "charts.js", services) } func ServicesHandler(w http.ResponseWriter, r *http.Request) { if !IsAuthenticated(r) { http.Redirect(w, r, "/", http.StatusSeeOther) return } ExecuteResponse(w, r, "services.html", core.CoreApp.Services) } func CreateServiceHandler(w http.ResponseWriter, r *http.Request) { if !IsAuthenticated(r) { http.Redirect(w, r, "/", http.StatusSeeOther) return } 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") postData := r.PostForm.Get("post_data") service := &core.Service{ Name: name, Domain: domain, Method: method, Expected: expected, ExpectedStatus: status, Interval: interval, Type: checkType, Port: port, PostData: postData, } _, err := service.Create() if err != nil { utils.Log(3, fmt.Sprintf("Error starting %v check routine. %v", service.Name, err)) } go service.CheckQueue() core.OnNewService(service) http.Redirect(w, r, "/services", http.StatusSeeOther) } func ServicesDeleteHandler(w http.ResponseWriter, r *http.Request) { if !IsAuthenticated(r) { 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) { if !IsAuthenticated(r) { 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") postData := r.PostForm.Get("post_data") serviceUpdate := &core.Service{ Id: service.Id, Name: name, Domain: domain, Method: method, Expected: expected, ExpectedStatus: status, Interval: interval, Type: checkType, Port: port, PostData: postData, } service = service.Update(serviceUpdate) ExecuteResponse(w, r, "service.html", service) } func ServicesDeleteFailuresHandler(w http.ResponseWriter, r *http.Request) { if !IsAuthenticated(r) { 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) { if !IsAuthenticated(r) { 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) }