Error handlers are almost ready.
This commit is contained in:
parent
fcfe75dfae
commit
4387eb95dc
@ -121,7 +121,10 @@ func (h *Handlers) Guestbook(w http.ResponseWriter, r *http.Request) {
|
||||
entriesCount, _ := h.guestbookDB.Count()
|
||||
pageCount := int64(math.Ceil(float64(entriesCount) / float64(h.guestbookPageSize)))
|
||||
|
||||
web.Guestbook("/ Guestbook", h.guestbookOwner, "", pageCount, entries, r, w)
|
||||
dwc := dwcaptcha.NewDwellingCaptcha(h.captchaExpire)
|
||||
_, id := inmemdb.New(r.RemoteAddr, dwc)
|
||||
|
||||
web.Guestbook("/ Guestbook", h.guestbookOwner, string(id), pageCount, entries, r, w)
|
||||
}
|
||||
|
||||
func (h *Handlers) GuestbookPost(w http.ResponseWriter, r *http.Request) {
|
||||
@ -131,6 +134,11 @@ func (h *Handlers) GuestbookPost(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
||||
r.ParseForm()
|
||||
|
||||
if !inmemdb.Solve(captcha.ID(r.FormValue("captcha_id")), captcha.Answer(r.FormValue("captcha_answer"))) {
|
||||
h.ForbiddenError("wrong answer", "Here's your message:"+r.FormValue("message"))
|
||||
return
|
||||
}
|
||||
|
||||
if r.FormValue("name") == "" {
|
||||
r.Form.Set("name", "Anonymous")
|
||||
}
|
||||
@ -138,14 +146,14 @@ func (h *Handlers) GuestbookPost(w http.ResponseWriter, r *http.Request) {
|
||||
entry, err = guestbook.NewEntry(r.FormValue("name"), r.FormValue("message"),
|
||||
r.FormValue("website"), r.FormValue("hide_website") != "")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
h.InternalError(err.Error(), "Here's your message:"+r.FormValue("message"))
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if err = h.guestbookDB.NewEntry(entry); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
h.InternalError(err.Error(), "Here's your message:"+r.FormValue("message"))
|
||||
return
|
||||
}
|
||||
|
||||
@ -164,10 +172,27 @@ func (h *Handlers) Robots(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
/**** Errors ******************************************************************/
|
||||
|
||||
func (h *Handlers) ForbiddenError(err, msg string) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
web.ForbiddenError("/ Forbidden", err, msg, w)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handlers) NotFound(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
web.NotFound("Not Found", w)
|
||||
}
|
||||
|
||||
func (h *Handlers) InternalError(err, msg string) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
web.InternalError("/ Internal Error", err, msg, w)
|
||||
}
|
||||
}
|
||||
|
||||
/**** CAPTCHA *****************************************************************/
|
||||
|
||||
func (h *Handlers) CaptchaNew(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -23,11 +23,12 @@ block nav
|
||||
h1 Whoa whoa, watcha tryin'?
|
||||
|
||||
block content
|
||||
:go:func ForbiddenError(title, err, msg string)
|
||||
section#error
|
||||
h1 403
|
||||
| Forbidden
|
||||
|
||||
if (error)
|
||||
if (err != "")
|
||||
section#description
|
||||
p= error.Error
|
||||
p= error.Message
|
||||
p= err
|
||||
p= msg
|
@ -23,11 +23,12 @@ block nav
|
||||
h1 Ohh... Shite!
|
||||
|
||||
block content
|
||||
:go:func InternalError(title, err, msg string)
|
||||
section#error
|
||||
h1 50x
|
||||
| Internal Server Error
|
||||
|
||||
if (error)
|
||||
if (err != "")
|
||||
section#description
|
||||
p= error.Error
|
||||
p= error.Message
|
||||
p= err
|
||||
p= msg
|
@ -16,7 +16,7 @@ import (
|
||||
|
||||
//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer article.pug
|
||||
|
||||
//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer error404.pug
|
||||
//go:generate $GOPATH/bin/jade -basedir ./templates -pkg=web -stdbuf -stdlib -writer error403.pug error404.pug error500.pug
|
||||
|
||||
//go:embed assets
|
||||
var assetsDir embed.FS
|
||||
|
Loading…
Reference in New Issue
Block a user