From e13bbd0c081e7918c23a84a79fdb842c6fe15a1f Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Fri, 21 Mar 2025 22:38:01 +0800 Subject: [PATCH] Add a "Proper" 404 page --- http_error_page.go | 12 ++++++++++++ http_server.go | 15 ++++++++------- static/style.css | 11 +++++++++++ templates/404.tmpl | 24 ++++++++++++++++++++++++ diff --git a/http_error_page.go b/http_error_page.go new file mode 100644 index 0000000000000000000000000000000000000000..2dd1f4f6e3255326b8e5c82d360de8e2236556e3 --- /dev/null +++ b/http_error_page.go @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-FileContributor: Runxi Yu + +package main + +import ( + "net/http" +) + +func errorPage404(w http.ResponseWriter, params map[string]any) { + _ = templates.ExecuteTemplate(w, "404", params) +} diff --git a/http_server.go b/http_server.go index 35b3ca5e40e8901ec79e148bebf0dd226c92bb70..c883cdf07f6d3ad813b78c8b4de8e1c57f48fc43 100644 --- a/http_server.go +++ b/http_server.go @@ -5,7 +5,6 @@ package main import ( "errors" - "fmt" "net/http" "strconv" "strings" @@ -58,7 +57,7 @@ } if segments[0] == ":" { if len(segments) < 2 { - http.Error(w, "Blank system endpoint", http.StatusNotFound) + errorPage404(w, params) return } else if len(segments) == 2 && redirectDir(w, r) { return @@ -86,7 +85,7 @@ case "gc": httpHandleGC(w, r, params) return default: - http.Error(w, fmt.Sprintf("Unknown system module type: %s", segments[1]), http.StatusNotFound) + errorPage404(w, params) return } } @@ -119,10 +118,10 @@ return } httpHandleGroupIndex(w, r, params) case len(segments) == sepIndex+1: - http.Error(w, "Illegal path 1", http.StatusNotImplemented) + errorPage404(w, params) return case len(segments) == sepIndex+2: - http.Error(w, "Illegal path 2", http.StatusNotImplemented) + errorPage404(w, params) return default: moduleType = segments[sepIndex+1] @@ -213,10 +212,12 @@ default: http.Error(w, "Too many parameters", http.StatusBadRequest) } default: - http.Error(w, fmt.Sprintf("Unknown repo feature: %s", repoFeature), http.StatusNotFound) + errorPage404(w, params) + return } default: - http.Error(w, fmt.Sprintf("Unknown module type: %s", moduleType), http.StatusNotFound) + errorPage404(w, params) + return } } } diff --git a/static/style.css b/static/style.css index e5398ceee48a76abc6c9c1e9996fe02fe1ff969f..4f84182a45edd9448e9bd7bbf7b872e6acafc224 100644 --- a/static/style.css +++ b/static/style.css @@ -408,3 +408,14 @@ padding-left: 1.5rem; margin-top: 0; margin-bottom: 0; } + + + +.complete-error-page { + font-family: 'Comic Sans MS', 'Chalkboard SE', 'Comic Neue', sans-serif; +} + +.complete-error-page hr { + border: 0; + border-bottom: 1px dashed; +} diff --git a/templates/404.tmpl b/templates/404.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..c994e1aebaa36bf06375f2192cbdd7415285997c --- /dev/null +++ b/templates/404.tmpl @@ -0,0 +1,24 @@ +{{/* + SPDX-License-Identifier: AGPL-3.0-only + SPDX-FileContributor: Runxi Yu +*/}} +{{- define "404" -}} + + + + {{- template "head_common" . -}} + 404 Not Found – {{ .global.forge_title }} + + + {{- template "header" . -}} +
+

404 Not Found

+
+
Lindenii Forge
+
+ + + +{{- end -}} -- 2.48.1