2017-10-08 04:48:58 +00:00
|
|
|
package core
|
|
|
|
|
2017-10-31 16:42:15 +00:00
|
|
|
import (
|
2017-11-20 05:49:19 +00:00
|
|
|
"fmt"
|
2017-10-31 16:42:15 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
2017-10-08 04:48:58 +00:00
|
|
|
|
2017-11-20 05:49:19 +00:00
|
|
|
// Flash adds a flash message to the user's session.
|
|
|
|
func (b *Blog) Flash(w http.ResponseWriter, r *http.Request, message string, args ...interface{}) {
|
|
|
|
session := b.Session(r)
|
|
|
|
session.AddFlash(fmt.Sprintf(message, args...))
|
|
|
|
session.Save(r, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlashAndRedirect flashes and redirects in one go.
|
|
|
|
func (b *Blog) FlashAndRedirect(w http.ResponseWriter, r *http.Request, location, message string, args ...interface{}) {
|
|
|
|
b.Flash(w, r, message, args...)
|
|
|
|
b.Redirect(w, location)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlashAndReload flashes and sends a redirect to the same path.
|
|
|
|
func (b *Blog) FlashAndReload(w http.ResponseWriter, r *http.Request, message string, args ...interface{}) {
|
|
|
|
b.Flash(w, r, message, args...)
|
|
|
|
b.Redirect(w, r.URL.Path)
|
|
|
|
}
|
|
|
|
|
2017-10-08 04:48:58 +00:00
|
|
|
// Redirect sends an HTTP redirect response.
|
2017-10-31 16:42:15 +00:00
|
|
|
func (b *Blog) Redirect(w http.ResponseWriter, location string) {
|
2017-11-15 14:55:15 +00:00
|
|
|
log.Error("Redirect: %s", location)
|
2017-10-08 04:48:58 +00:00
|
|
|
w.Header().Set("Location", location)
|
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
}
|
2017-10-31 16:42:15 +00:00
|
|
|
|
|
|
|
// NotFound sends a 404 response.
|
|
|
|
func (b *Blog) NotFound(w http.ResponseWriter, r *http.Request, message ...string) {
|
|
|
|
if len(message) == 0 {
|
|
|
|
message = []string{"The page you were looking for was not found."}
|
|
|
|
}
|
|
|
|
|
2017-11-15 14:55:15 +00:00
|
|
|
log.Error("HERE 2")
|
2017-10-31 16:42:15 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2017-11-07 17:01:02 +00:00
|
|
|
err := b.RenderTemplate(w, r, ".errors/404", &Vars{
|
|
|
|
Message: message[0],
|
2017-10-31 16:42:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
2017-11-03 16:52:40 +00:00
|
|
|
w.Write([]byte("Unrecoverable template error for NotFound()"))
|
2017-10-31 16:42:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 16:52:40 +00:00
|
|
|
// Forbidden sends an HTTP 403 Forbidden response.
|
2017-10-31 16:42:15 +00:00
|
|
|
func (b *Blog) Forbidden(w http.ResponseWriter, r *http.Request, message ...string) {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2017-11-24 19:56:32 +00:00
|
|
|
err := b.RenderTemplate(w, r, ".errors/403", &Vars{
|
|
|
|
Message: message[0],
|
|
|
|
})
|
2017-10-31 16:42:15 +00:00
|
|
|
if err != nil {
|
2017-11-03 16:52:40 +00:00
|
|
|
log.Error(err.Error())
|
|
|
|
w.Write([]byte("Unrecoverable template error for Forbidden()"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BadRequest sends an HTTP 400 Bad Request.
|
|
|
|
func (b *Blog) BadRequest(w http.ResponseWriter, r *http.Request, message ...string) {
|
2017-11-15 14:55:15 +00:00
|
|
|
log.Error("HERE 4")
|
2017-11-03 16:52:40 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2017-11-07 17:01:02 +00:00
|
|
|
err := b.RenderTemplate(w, r, ".errors/400", &Vars{
|
|
|
|
Message: message[0],
|
2017-11-03 16:52:40 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
w.Write([]byte("Unrecoverable template error for BadRequest()"))
|
2017-10-31 16:42:15 +00:00
|
|
|
}
|
|
|
|
}
|