Newer
Older
package main
import (
"errors"
"fmt"
"html/template"
"net/http"
"strconv"
func (h Handler) index(write WriteStorage, read ReadStorage) http.Handler {
var (
once sync.Once
tpl *template.Template
)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
once.Do(func() {
tpl = template.Must(template.ParseFS(templateFS, "templates/base.tpl", "templates/index.tpl"))
})
if r.Method == "GET" {
httpError(w, r, "can't list wines", err, http.StatusInternalServerError)
return
}
w.Header().Add("content-type", "text/html")
Wines: wines,
User: auth.Get(r),
}
err = tpl.ExecuteTemplate(w, "index.tpl", data)
if err != nil {
level.Error(log.Get(r)).
Log("error", err,
"msg", "can't execute index template")
httpError(w, r, "invalid method", errors.New(r.Method), http.StatusMethodNotAllowed)
if len(name) > 80 || len(name) == 0 {
httpError(w, r, "bad name", errors.New("name must be between 1 and 80 chars"), http.StatusBadRequest)
if ratingVal != "" {
rating, err = strconv.Atoi(ratingVal)
if err != nil {
httpError(w, r, "can't convert rating", err, http.StatusBadRequest)
err = r.ParseMultipartForm(16 * 1024 * 1024)
level.Warn(log.Get(r)).Log("error", err, "msg", "can't parse multipart form data")
Name: name,
Rating: rating,
}
// See if there's an image file uploaded
if r.MultipartForm != nil && len(r.MultipartForm.File["picture"]) != 0 {
picFile, picHdr, err := r.FormFile("picture")
if err != nil {
httpError(w, r, "can't open picture file", err, http.StatusInternalServerError)
return
}
defer picFile.Close()
err = vino.AddPicture(picFile, picHdr.Header.Get("Content-Type"))
if err != nil {
httpError(w, r, "can't load picture", err, http.StatusBadRequest)
httpError(w, r, fmt.Sprintf("can't store wine %q", vino), err, http.StatusInternalServerError)
http.Redirect(w, r, "/details?id="+strconv.Itoa(vino.ID), http.StatusSeeOther) // TODO: Is this the correct status?