From f8bf946b090aa4fb0b8beb4ad09c1f06078f0b5b Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Wed, 26 Jun 2024 11:20:15 +0200 Subject: make error pages dynamic --- internal/builder/builder.go | 5 ----- internal/http/error.go | 7 +++++++ internal/website/mux.go | 15 +++++---------- 3 files changed, 12 insertions(+), 15 deletions(-) create mode 100644 internal/http/error.go (limited to 'internal') diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 08fa374..d855451 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -235,11 +235,6 @@ func build(outDir string, config config.Config) (*Result, error) { h, _ = getHTMLStyleHash(publicDir, "index.html") r.Hashes = append(r.Hashes, h) - log.Debug("rendering 404 page") - if err := renderToFile(templates.NotFound(config, "/404.html"), privateDir, "404.html"); err != nil { - return nil, err - } - log.Debug("rendering sitemap") if err := writerToFile(sitemap, publicDir, "sitemap.xml"); err != nil { return nil, err diff --git a/internal/http/error.go b/internal/http/error.go new file mode 100644 index 0000000..8ad3e16 --- /dev/null +++ b/internal/http/error.go @@ -0,0 +1,7 @@ +package http + +type Error struct { + Error error + Message string + Code int +} diff --git a/internal/website/mux.go b/internal/website/mux.go index a6e7ba0..fbd648e 100644 --- a/internal/website/mux.go +++ b/internal/website/mux.go @@ -6,6 +6,7 @@ import ( "path" "strings" "website/internal/config" + ihttp "website/internal/http" "website/internal/log" "website/templates" @@ -14,12 +15,6 @@ import ( "github.com/pkg/errors" ) -type HTTPError struct { - Error error - Message string - Code int -} - func canonicalisePath(path string) (cPath string, differs bool) { cPath = path if strings.HasSuffix(path, "/index.html") { @@ -31,7 +26,7 @@ func canonicalisePath(path string) (cPath string, differs bool) { return cPath, differs } -type webHandler func(http.ResponseWriter, *http.Request) *HTTPError +type webHandler func(http.ResponseWriter, *http.Request) *ihttp.Error type WrappedWebHandler struct { config *config.Config @@ -55,7 +50,7 @@ func (fn WrappedWebHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := fn.handler(w, r); err != nil { if strings.Contains(r.Header.Get("Accept"), "text/html") { w.WriteHeader(err.Code) - err := templates.NotFound(*fn.config, r.URL.Path).Render(r.Context(), w) + err := templates.Error(*fn.config, r.URL.Path, err).Render(r.Context(), w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -76,7 +71,7 @@ func NewMux(cfg *config.Config, root string) (mux *http.ServeMux, err error) { } templates.Setup() - mux.Handle("/", wrapHandler(cfg, func(w http.ResponseWriter, r *http.Request) *HTTPError { + mux.Handle("/", wrapHandler(cfg, func(w http.ResponseWriter, r *http.Request) *ihttp.Error { urlPath, shouldRedirect := canonicalisePath(r.URL.Path) if shouldRedirect { http.Redirect(w, r, urlPath, 302) @@ -85,7 +80,7 @@ func NewMux(cfg *config.Config, root string) (mux *http.ServeMux, err error) { } file := GetFile(urlPath) if file == nil { - return &HTTPError{ + return &ihttp.Error{ Message: "File not found", Code: http.StatusNotFound, } -- cgit 1.4.1