hivedav

A curlable free/busy scheduler with CalDAV integration
git clone https://git.in0rdr.ch/hivedav.git
Log | Files | Refs | Pull requests | README | LICENSE

commit 95621588a755c1c2a0318d1bf7c3ca8267d766e7
parent 0380cb2454291d6121d87650ba4a8354c69678d2
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date:   Sat, 30 Sep 2023 11:39:58 +0200

fix: superfluous WriteHeader, use http.Error

Diffstat:
Mserver.go | 36+++++++++++-------------------------
1 file changed, 11 insertions(+), 25 deletions(-)

diff --git a/server.go b/server.go @@ -181,9 +181,8 @@ func (s *Server) Week(w http.ResponseWriter, req *http.Request, ps httprouter.Pa // overwrite with requested week week, err := strconv.Atoi(ps.ByName("week")) if err != nil { - w.WriteHeader(http.StatusBadRequest) log.Printf("Cannot serve requested week '%d'\n", week) - fmt.Fprintf(w, "Cannot serve requested week '%d'\n", week) + http.Error(w, fmt.Sprintf("Cannot serve requested week '%d'\n", week), http.StatusBadRequest) return } @@ -311,16 +310,13 @@ func (s *Server) Week(w http.ResponseWriter, req *http.Request, ps httprouter.Pa tmpl, err := template.New("index.html").Funcs(funcMap).ParseFiles("./templates/index.html") if err != nil { log.Printf("Error parsing html template: %v\n", err) - w.WriteHeader(http.StatusInternalServerError) - log.Printf("Error parsing html template: %v\n", err) - fmt.Fprintf(w, "Error parsing html template: %v\n", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } err = tmpl.Execute(w, htmlTableData) if err != nil { - w.WriteHeader(http.StatusInternalServerError) log.Printf("Error executing html template: %v\n", err) - fmt.Fprintf(w, "Error executing html template: %v\n", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } } @@ -487,8 +483,7 @@ func (s *Server) Healthz(w http.ResponseWriter, req *http.Request, _ httprouter. if count > 0 { io.WriteString(w, fmt.Sprintf("{\"status\":\"wagwan, hive all good\",\"rows\":\"%d\",\"version\":\"%s\"}", count, hivedavVersion)) } else { - w.WriteHeader(http.StatusInternalServerError) - io.WriteString(w, fmt.Sprintf("{\"status\":\"nah fam, db not initialized\",\"rows\":\"%d\",\"version\":\"%s\"}", count, hivedavVersion)) + http.Error(w, fmt.Sprintf("{\"status\":\"nah fam, db not initialized\",\"rows\":\"%d\",\"version\":\"%s\"}", count, hivedavVersion), http.StatusInternalServerError) } } @@ -511,9 +506,8 @@ func (s *Server) ListCubiclesInWeek(w http.ResponseWriter, req *http.Request, ps // overwrite with requested week week, err := strconv.Atoi(ps.ByName("week")) if err != nil { - w.WriteHeader(http.StatusBadRequest) log.Printf("Cannot serve requested week '%d'\n", week) - fmt.Fprintf(w, "Cannot serve requested week '%d'\n", week) + http.Error(w, fmt.Sprintf("Cannot serve requested week '%d'\n", week), http.StatusBadRequest) return } @@ -598,16 +592,14 @@ func (s *Server) ListCubiclesInWeek(w http.ResponseWriter, req *http.Request, ps tmpl, err := template.New("listcubicles.html").Funcs(funcMap).ParseFiles("./templates/listcubicles.html") if err != nil { - w.WriteHeader(http.StatusInternalServerError) log.Printf("Error parsing html template: %v\n", err) - fmt.Fprintf(w, "Error parsing html template: %v\n", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } err = tmpl.Execute(w, tableData) if err != nil { - w.WriteHeader(http.StatusInternalServerError) log.Printf("Error executing html template: %v\n", err) - fmt.Fprintf(w, "Error executing html template: %v\n", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } } @@ -619,9 +611,8 @@ func (s *Server) BookCubicle(w http.ResponseWriter, req *http.Request, ps httpro //TODO: remove Year from the input format dtStartTimeLocal, err := time.ParseInLocation("2006-01-02-15", dtstart, time.Local) if err != nil { - w.WriteHeader(http.StatusInternalServerError) log.Printf("Error parsing booking dtstart: %v\n", err) - fmt.Fprintf(w, "Error parsing booking dtstart: %v\n", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } log.Printf("dtstart booking request (local time): %v\n", dtStartTimeLocal) @@ -631,29 +622,24 @@ func (s *Server) BookCubicle(w http.ResponseWriter, req *http.Request, ps httpro // https://pkg.go.dev/net/http#Request.ParseMultipartForm err = req.ParseMultipartForm(256) if err != nil { - w.WriteHeader(http.StatusInternalServerError) log.Printf("ParseMultipartForm err: %v\n", err) - fmt.Fprintf(w, "ParseMultipartForm err: %v\n", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } mail := req.FormValue("mail") msg := req.FormValue("msg") - //fmt.Fprintf(w, "Mail: %s\n", mail) - //fmt.Fprintf(w, "Msg: %s\n", msg) log.Printf("Form: %+v\n", req.Form) if mail == "" || msg == "" { - w.WriteHeader(http.StatusBadRequest) - fmt.Fprintf(w, "Mail and msg required, try again: 'curl %s/book/%s -F 'mail=' -F 'msg='\n", s.config.HiveDavHost, dtstart) + http.Error(w, fmt.Sprintf("Mail and msg required, try again: 'curl %s/book/%s -F 'mail=' -F 'msg='\n", s.config.HiveDavHost, dtstart), http.StatusBadRequest) return } err = s.sendMail(mail, msg, dtStartTimeLocal) if err != nil { - w.WriteHeader(http.StatusInternalServerError) log.Printf("Error occured while sending mail: %v\n", err) - fmt.Fprintf(w, "Error occured while sending mail: %v\n", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return } // TODO: print success message to user