func newVisitor(req *http.Request) (*http.Cookie, error) { m := initialModel() id, err := uuid.NewV4() if err != nil { log.Println("ERROR newVisitor uuid.NewV4", err) return nil, err } return makeCookie(m, id.String(), req) }
func logout(res http.ResponseWriter, req *http.Request) { cookie, err := newVisitor(req) if err != nil { log.Println("ERROR logout getCookie", err) http.Error(res, err.Error(), http.StatusInternalServerError) return } http.SetCookie(res, cookie) http.Redirect(res, req, "/", http.StatusSeeOther) }
func index(res http.ResponseWriter, req *http.Request) { //ctx := appengine.NewContext(req)//from 049 var id string cookie, err := getCookie(res, req) if err != nil { // problem retrieving cookie log.Println("ERROR index getCookie", err) //http.Error(res, err.Error(), http.StatusInternalServerError) //comment out? return } id = cookie.Value _, err = req.Cookie("session-id") if err != nil { id = req.FormValue("User") } if req.Method == "POST" { src, _, err := req.FormFile("data") if err != nil { log.Println("ERROR index req.FormFile", err) // TODO: create error page to show user http.Redirect(res, req, "/", http.StatusSeeOther) return } err = uploadPhoto(src, id, req) if err != nil { log.Println("ERROR index uploadPhoto", err) // expired cookie may exist on client http.Redirect(res, req, "/logout", http.StatusSeeOther) return } } m, err := retrieveMemc(id, req) if err != nil { log.Println("ERROR index retrieveMemc", err) // expired cookie may exist on client http.Redirect(res, req, "/logout", http.StatusSeeOther) return } tpl.ExecuteTemplate(res, "index.html", m) }
func login(res http.ResponseWriter, req *http.Request) { cookie, err := getCookie(res, req) if err != nil { log.Println("ERROR login getCookie", err) http.Error(res, err.Error(), http.StatusInternalServerError) return } id := cookie.Value if req.Method == "POST" && req.FormValue("password") == "secret" { m, err := retrieveMemc(id, req) if err != nil { log.Println("ERROR index retrieveMemc", err) // expired cookie may exist on client http.Redirect(res, req, "/logout", http.StatusSeeOther) return } m.State = true m.Name = req.FormValue("name") cookie, err := currentVisitor(m, id, req) if err != nil { log.Println("ERROR login currentVisitor", err) http.Redirect(res, req, "/", http.StatusSeeOther) return } http.SetCookie(res, cookie) //http.Redirect(res, req, "/", http.StatusSeeOther) http.Redirect(res, req, "/?User="******"login.html", nil) }