diff options
author | Alan Pearce | 2024-06-26 11:20:15 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-26 11:20:15 +0200 |
commit | f8bf946b090aa4fb0b8beb4ad09c1f06078f0b5b (patch) | |
tree | 4f190e4cf9b2cba36147a6c580bfe8a7295d7bb6 /internal | |
parent | 56ac09928f9aea7a25577e9da716afcd09d2d6b8 (diff) | |
download | website-f8bf946b090aa4fb0b8beb4ad09c1f06078f0b5b.tar.lz website-f8bf946b090aa4fb0b8beb4ad09c1f06078f0b5b.tar.zst website-f8bf946b090aa4fb0b8beb4ad09c1f06078f0b5b.zip |
make error pages dynamic
Diffstat (limited to 'internal')
-rw-r--r-- | internal/builder/builder.go | 5 | ||||
-rw-r--r-- | internal/http/error.go | 7 | ||||
-rw-r--r-- | internal/website/mux.go | 15 |
3 files changed, 12 insertions, 15 deletions
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, } |