1
0

Error handlers are almost ready.

This commit is contained in:
Alexander Andreev 2023-02-05 23:23:55 +04:00
parent fcfe75dfae
commit 4387eb95dc
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
4 changed files with 37 additions and 10 deletions

View File

@ -121,7 +121,10 @@ func (h *Handlers) Guestbook(w http.ResponseWriter, r *http.Request) {
entriesCount, _ := h.guestbookDB.Count() entriesCount, _ := h.guestbookDB.Count()
pageCount := int64(math.Ceil(float64(entriesCount) / float64(h.guestbookPageSize))) 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) { 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" { if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
r.ParseForm() 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") == "" { if r.FormValue("name") == "" {
r.Form.Set("name", "Anonymous") 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"), entry, err = guestbook.NewEntry(r.FormValue("name"), r.FormValue("message"),
r.FormValue("website"), r.FormValue("hide_website") != "") r.FormValue("website"), r.FormValue("hide_website") != "")
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) h.InternalError(err.Error(), "Here's your message:"+r.FormValue("message"))
return return
} }
} }
if err = h.guestbookDB.NewEntry(entry); err != nil { 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 return
} }
@ -164,10 +172,27 @@ func (h *Handlers) Robots(w http.ResponseWriter, r *http.Request) {
w.Write(data) 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) { func (h *Handlers) NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
web.NotFound("Not Found", w) 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 *****************************************************************/ /**** CAPTCHA *****************************************************************/
func (h *Handlers) CaptchaNew(w http.ResponseWriter, r *http.Request) { func (h *Handlers) CaptchaNew(w http.ResponseWriter, r *http.Request) {

View File

@ -23,11 +23,12 @@ block nav
h1 Whoa whoa, watcha tryin'? h1 Whoa whoa, watcha tryin'?
block content block content
:go:func ForbiddenError(title, err, msg string)
section#error section#error
h1 403 h1 403
| Forbidden | Forbidden
if (error) if (err != "")
section#description section#description
p= error.Error p= err
p= error.Message p= msg

View File

@ -23,11 +23,12 @@ block nav
h1 Ohh... Shite! h1 Ohh... Shite!
block content block content
:go:func InternalError(title, err, msg string)
section#error section#error
h1 50x h1 50x
| Internal Server Error | Internal Server Error
if (error) if (err != "")
section#description section#description
p= error.Error p= err
p= error.Message p= msg

View File

@ -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 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 //go:embed assets
var assetsDir embed.FS var assetsDir embed.FS