func Poll(w http.ResponseWriter, r *http.Request) { sess, err := us.MakeSession(r) poll, err := findPoll(r) if err != nil { http.Error(w, "Error: "+err.Error(), 500) return } locals := sess.Locals() locals["Poll"] = poll err = template.RenderTemplate(w, "poll.tmpl", locals) if err != nil { panic(err) } /*w.Header().Set("Content-Type", "text/html") w.Write([]byte(fmt.Sprintf("%v", poll.Collated()))) fmt.Fprintf(w, `<form method="post" action="%s"> <input type="text" name="answer"> <input type="submit" name="submit"> </form>`, r.URL.Path)*/ }
func Home(w http.ResponseWriter, r *http.Request) { sess, err := us.MakeSession(r) if err != nil { http.Error(w, err.Error(), 500) return } sess.Save(w, r) err = template.RenderTemplate(w, "index.tmpl", sess.Locals()) if err != nil { panic(err) } }
func Logout(w http.ResponseWriter, r *http.Request) { sess, err := us.MakeSession(r) if err != nil { http.Error(w, err.Error(), 500) return } err = sess.Logout() sess.Save(w, r) if err != nil { w.Write([]byte("Not logged in")) } w.Write([]byte("Logged out")) }
func LoginPage(w http.ResponseWriter, r *http.Request) { sess, err := us.MakeSession(r) if err != nil { http.Error(w, err.Error(), 500) return } sess.Save(w, r) w.Header().Set("Content-Type", "text/html") w.Write([]byte(sess.Debug())) w.Write([]byte("<form method=\"post\" action=\"/login\">")) w.Write([]byte("<input type=\"text\" name=\"username\">")) w.Write([]byte("<input type=\"password\" name=\"password\">")) w.Write([]byte("<input type=\"submit\" name=\"submit\">")) w.Write([]byte("</form>")) }
func Login(w http.ResponseWriter, r *http.Request) { sess, err := us.MakeSession(r) if err != nil { http.Error(w, err.Error(), 500) return } login := sess.LogIn(r.FormValue("username"), r.FormValue("password")) sess.Save(w, r) w.Write([]byte(sess.Debug())) w.Write([]byte("Gorilla!\n")) w.Write([]byte(r.FormValue("username"))) w.Write([]byte(":")) w.Write([]byte(r.FormValue("password"))) w.Write([]byte("\nGorilla!\n")) w.Write([]byte(fmt.Sprintf("%v\n", sess.UserId))) w.Write([]byte(fmt.Sprintf("Logged in successfully: %v", login))) }
func Answer(w http.ResponseWriter, r *http.Request) { poll, err := findPoll(r) sess, err := us.MakeSession(r) user, err := sess.CurrentUser() if err != nil { http.Error(w, "Error: "+err.Error(), 500) return } r.ParseForm() ans, err := strconv.Atoi(r.FormValue("answer")) if err != nil { http.Error(w, "Error: "+err.Error(), 500) return } poll.AddAnswer(ans, user.Id) http.Redirect(w, r, fmt.Sprintf("/polls/%v", poll.Id), http.StatusFound) }