2018-02-10 19:20:27 +00:00
|
|
|
package responses
|
|
|
|
|
|
|
|
import (
|
2019-05-17 03:12:47 +00:00
|
|
|
"encoding/json"
|
2018-02-10 19:20:27 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2018-12-24 00:21:50 +00:00
|
|
|
"github.com/kirsle/blog/src/sessions"
|
2018-02-10 19:20:27 +00:00
|
|
|
)
|
|
|
|
|
2018-02-10 23:07:10 +00:00
|
|
|
// Error handlers to be filled in by the blog app.
|
|
|
|
var (
|
|
|
|
NotFound func(http.ResponseWriter, *http.Request, string)
|
|
|
|
Forbidden func(http.ResponseWriter, *http.Request, string)
|
|
|
|
BadRequest func(http.ResponseWriter, *http.Request, string)
|
|
|
|
Error func(http.ResponseWriter, *http.Request, string)
|
|
|
|
)
|
|
|
|
|
2018-02-10 19:20:27 +00:00
|
|
|
// Flash adds a flash message to the user's session.
|
|
|
|
func Flash(w http.ResponseWriter, r *http.Request, message string, args ...interface{}) {
|
|
|
|
session := sessions.Get(r)
|
|
|
|
session.AddFlash(fmt.Sprintf(message, args...))
|
|
|
|
session.Save(r, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlashAndRedirect flashes and redirects in one go.
|
|
|
|
func FlashAndRedirect(w http.ResponseWriter, r *http.Request, location, message string, args ...interface{}) {
|
|
|
|
Flash(w, r, message, args...)
|
|
|
|
Redirect(w, location)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlashAndReload flashes and sends a redirect to the same path.
|
|
|
|
func FlashAndReload(w http.ResponseWriter, r *http.Request, message string, args ...interface{}) {
|
|
|
|
Flash(w, r, message, args...)
|
|
|
|
Redirect(w, r.URL.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect sends an HTTP redirect response.
|
|
|
|
func Redirect(w http.ResponseWriter, location string) {
|
|
|
|
w.Header().Set("Location", location)
|
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
}
|
2019-05-17 03:12:47 +00:00
|
|
|
|
|
|
|
// JSON serializes a JSON response to the browser.
|
|
|
|
func JSON(w http.ResponseWriter, statusCode int, v interface{}) {
|
|
|
|
w.Header().Set("Content-Type", "application/json; encoding=utf-8")
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
|
|
|
|
serial, err := json.MarshalIndent(v, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
serial, _ = json.Marshal(err.Error())
|
|
|
|
}
|
|
|
|
w.Write(serial)
|
|
|
|
}
|