From d05f7f868d7545ea0ecb651ae1da4cca97dd2750 Mon Sep 17 00:00:00 2001 From: video-prize-ranch Date: Mon, 23 May 2022 11:30:17 -0400 Subject: [PATCH] Add 404 page --- pages/media.go | 5 +++++ pages/post.go | 7 +++---- pages/tag.go | 4 ++++ pages/user.go | 5 +++++ static/css/base.css | 4 ++++ views/errors/404.hbs | 20 ++++++++++++++++++++ 6 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 views/errors/404.hbs diff --git a/pages/media.go b/pages/media.go index 22973d2..ac4bcd4 100644 --- a/pages/media.go +++ b/pages/media.go @@ -32,6 +32,11 @@ func handleMedia(c *fiber.Ctx, url string) error { return err } + if res.StatusCode == 404 { + c.Status(404) + return c.Render("errors/404", nil) + } + c.Set("Content-Type", res.Header.Get("Content-Type")); contentLen, _ := strconv.Atoi(res.Header.Get("Content-Length")) return c.SendStream(res.Body, contentLen) diff --git a/pages/post.go b/pages/post.go index f36d56a..4a41358 100644 --- a/pages/post.go +++ b/pages/post.go @@ -1,7 +1,6 @@ package pages import ( - "fmt" "strings" "codeberg.org/video-prize-ranch/rimgo/api" @@ -18,14 +17,14 @@ func HandlePost(c *fiber.Ctx) error { switch { case strings.HasPrefix(c.Path(), "/a"): post, err = api.FetchAlbum(c.Params("postID")) - println(post.Title) case strings.HasPrefix(c.Path(), "/gallery"): post, err = api.FetchPosts(c.Params("postID")) default: post, err = api.FetchMedia(c.Params("postID")) } - if post.Id == "" || strings.Contains(err.Error(), "404") { - return fmt.Errorf("404 page not found") + if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) { + c.Status(404) + return c.Render("errors/404", nil) } if err != nil { return err diff --git a/pages/tag.go b/pages/tag.go index 588375d..6ef36bb 100644 --- a/pages/tag.go +++ b/pages/tag.go @@ -32,6 +32,10 @@ func HandleTag(c *fiber.Ctx) error { if err != nil { return err } + if tag.Display == "" { + c.Status(404) + return c.Render("errors/404", nil) + } return c.Render("tag", fiber.Map{ "tag": tag, diff --git a/pages/user.go b/pages/user.go index 793c6de..85538b2 100644 --- a/pages/user.go +++ b/pages/user.go @@ -46,6 +46,11 @@ func HandleUser(c *fiber.Ctx) error { } wg.Wait() + if user.Username == "" { + c.Status(404) + return c.Render("errors/404", nil) + } + return c.Render("user", fiber.Map{ "user": user, "submissions": submissions, diff --git a/static/css/base.css b/static/css/base.css index cd95a3f..623fc0e 100644 --- a/static/css/base.css +++ b/static/css/base.css @@ -37,6 +37,10 @@ main { margin: 0 24vw; } +.errorTitle { + text-align: center; +} + .posts { margin-top: 1em; display: grid; diff --git a/views/errors/404.hbs b/views/errors/404.hbs new file mode 100644 index 0000000..2cb1b83 --- /dev/null +++ b/views/errors/404.hbs @@ -0,0 +1,20 @@ + + + + + {{> 'partials/head' }} + + 404 - rimgo + + + + {{> 'partials/header' }} + +
+

404 Not Found

+

Click here to return to home.

+
+ + + + \ No newline at end of file