journal/httpserver/errors.go
Vojtěch Mareš 943922a6e1
feat: add app code
- journal domain package
- httpserver package
- html templates
- main.go in root dir
2025-04-22 22:01:53 +02:00

34 lines
2.4 KiB
Go

package httpserver
import "net/http"
var (
ErrorPageBadRequest = `<!DOCTYPE html><html><head><title>Bad request | Journal</title><style>html { font-family: Consolas, monospace; } #error { margin: 0 auto; margin-top: 8rem; max-width: 40rem; } h1 { font-size: 3rem; } p { font-size: 1.5rem; margin-top: -1rem; }</style></head><body><div id="error"><h1>Bad request</h1><p>error 400</p></div></body></html>` // 400
ErrorPageForbidden = `<!DOCTYPE html><html><head><title>Forbidden | Journal</title><style>html { font-family: Consolas, monospace; } #error { margin: 0 auto; margin-top: 8rem; max-width: 40rem; } h1 { font-size: 3rem; } p { font-size: 1.5rem; margin-top: -1rem; }</style></head><body><div id="error"><h1>Forbidden</h1><p>error 403</p></div></body></html>` // 403
ErrorPageNotFound = `<!DOCTYPE html><html><head><title>Not found | Journal</title><style>html { font-family: Consolas, monospace; } #error { margin: 0 auto; margin-top: 8rem; max-width: 40rem; } h1 { font-size: 3rem; } p { font-size: 1.5rem; margin-top: -1rem; }</style></head><body><div id="error"><h1>Not found</h1><p>error 404</p></div></body></html>` // 404
ErrorPageInternalServerError = `<!DOCTYPE html><html><head><title>Internal server error | Journal</title></head><style>html { font-family: Consolas, monospace; } #error { margin: 0 auto; margin-top: 8rem; max-width: 40rem; } h1 { font-size: 3rem; } p { font-size: 1.5rem; margin-top: -1rem; }</style><body><div id="error"><h1>Internal server error</h1><p>error 500</p></div></body></html>` // 500
)
func BadRequest(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(ErrorPageBadRequest))
}
func Forbidden(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(ErrorPageForbidden))
}
func NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(ErrorPageNotFound))
}
func InternalServerError(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(ErrorPageInternalServerError))
}