From 20761920d335beff5533eb90ec6c5aec652d2e74 Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Thu, 21 Nov 2024 16:33:34 +0900 Subject: [PATCH] feat: page not found --- server/error/page.go | 2 +- server/main.go | 1 + server/routing/json_params.go | 13 ------------- server/routing/not_found.go | 6 ++++++ 4 files changed, 8 insertions(+), 14 deletions(-) delete mode 100644 server/routing/json_params.go diff --git a/server/error/page.go b/server/error/page.go index 898c927..1a33b0d 100644 --- a/server/error/page.go +++ b/server/error/page.go @@ -11,9 +11,9 @@ type pageProps struct { } func Page(w http.ResponseWriter, publicMessage string, status int, err error) { + w.WriteHeader(status) if err != nil { log.Printf("%s --- %s\n", publicMessage, err) } templates.Execute(w, "error_page.gotmpl", pageProps{publicMessage}) - http.Error(w, "", http.StatusInternalServerError) } diff --git a/server/main.go b/server/main.go index 3a5f343..fd76a05 100644 --- a/server/main.go +++ b/server/main.go @@ -50,6 +50,7 @@ func main() { r.Html.Private.Handle("POST /list/{userReference}", route.ExpectUser(route.ForeignWishlistPost)) r.Html.Private.Handle("GET /group/{groupReference}", route.ExpectUser(route.GroupPage)) r.Html.Private.HandleFunc("POST /logout", route.LogoutPost) + r.Html.Private.HandleFunc("GET /", routing.NotFound) r.Json.Public.HandleFunc("GET /", routing.NotFoundJson) diff --git a/server/routing/json_params.go b/server/routing/json_params.go deleted file mode 100644 index b550c51..0000000 --- a/server/routing/json_params.go +++ /dev/null @@ -1,13 +0,0 @@ -package routing - -import ( - "encoding/json" - "net/http" -) - -func decodeJsonParams(r *http.Request, v any) error { - dec := json.NewDecoder(r.Body) - dec.DisallowUnknownFields() - err := dec.Decode(&v) - return err -} diff --git a/server/routing/not_found.go b/server/routing/not_found.go index 8cba1a1..f8a6d35 100644 --- a/server/routing/not_found.go +++ b/server/routing/not_found.go @@ -2,6 +2,8 @@ package routing import ( "net/http" + + "lishwist/error" ) func NotFoundJson(w http.ResponseWriter, r *http.Request) { @@ -9,3 +11,7 @@ func NotFoundJson(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) _, _ = w.Write([]byte(`{"GeneralError":"Not Found"}`)) } + +func NotFound(w http.ResponseWriter, r *http.Request) { + error.Page(w, "404 -- Page not found", http.StatusNotFound, nil) +}