2022-08-10 05:10:47 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-12-22 01:30:34 +00:00
|
|
|
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NotFoundPage is an HTTP handler for 404 pages.
|
|
|
|
var NotFoundPage = func() http.HandlerFunc {
|
|
|
|
return MakeErrorPage("Not Found", "The page you requested was not here.", http.StatusNotFound)
|
|
|
|
}()
|
|
|
|
|
2022-08-13 06:11:36 +00:00
|
|
|
// ForbiddenPage is an HTTP handler for 404 pages.
|
|
|
|
var ForbiddenPage = func() http.HandlerFunc {
|
|
|
|
return MakeErrorPage("Forbidden", "You do not have permission for this page.", http.StatusForbidden)
|
|
|
|
}()
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
func MakeErrorPage(header string, message string, statusCode int) http.HandlerFunc {
|
2023-12-22 01:30:34 +00:00
|
|
|
tmpl, err := LoadTemplate("errors/error.html")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("MakeErrorPage(%s): %s", header, err)
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-10 05:10:47 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
if err := tmpl.Execute(w, r, map[string]interface{}{
|
|
|
|
"Header": header,
|
|
|
|
"Message": message,
|
|
|
|
}); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|