diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-21 22:38:01 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-21 22:38:01 +0800 |
commit | e13bbd0c081e7918c23a84a79fdb842c6fe15a1f (patch) | |
tree | 6a30a4ef814613c52e614d0299ea576d944ed16b | |
parent | Fix Is_file -> IsFile naming (diff) | |
download | forge-e13bbd0c081e7918c23a84a79fdb842c6fe15a1f.tar.gz forge-e13bbd0c081e7918c23a84a79fdb842c6fe15a1f.tar.zst forge-e13bbd0c081e7918c23a84a79fdb842c6fe15a1f.zip |
Add a "Proper" 404 page
-rw-r--r-- | http_error_page.go | 12 | ||||
-rw-r--r-- | http_server.go | 15 | ||||
-rw-r--r-- | static/style.css | 11 | ||||
-rw-r--r-- | templates/404.tmpl | 24 |
4 files changed, 55 insertions, 7 deletions
diff --git a/http_error_page.go b/http_error_page.go new file mode 100644 index 0000000..2dd1f4f --- /dev/null +++ b/http_error_page.go @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-FileContributor: Runxi Yu <https://runxiyu.org> + +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 35b3ca5..c883cdf 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 @@ func (router *forgeHTTPRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) 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 @@ func (router *forgeHTTPRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) 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 @@ func (router *forgeHTTPRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) } 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 @@ func (router *forgeHTTPRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) 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 e5398ce..4f84182 100644 --- a/static/style.css +++ b/static/style.css @@ -408,3 +408,14 @@ td > ul { 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 0000000..c994e1a --- /dev/null +++ b/templates/404.tmpl @@ -0,0 +1,24 @@ +{{/* + SPDX-License-Identifier: AGPL-3.0-only + SPDX-FileContributor: Runxi Yu <https://runxiyu.org> +*/}} +{{- define "404" -}} +<!DOCTYPE html> +<html lang="en"> + <head> + {{- template "head_common" . -}} + <title>404 Not Found – {{ .global.forge_title }}</title> + </head> + <body class="404"> + {{- template "header" . -}} + <div class="padding-wrapper complete-error-page"> + <h1>404 Not Found</h1> + <hr /> + <address>Lindenii Forge</address> + </div> + <footer> + {{- template "footer" . -}} + </footer> + </body> +</html> +{{- end -}} |