Fix Edit Location 404 and add styled 404 page
The folder view's Edit Location/Edit Folder links pointed to GET
/admin/locations/edit/{id}, which only existed as a POST route and
fell through to the public wildcard. Add a GET route that opens the
edit form on the Locations page.
All not-found responses now render a styled 404 page with a Back to
Home button and an automatic redirect to the homepage.
Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
c0375f651a
commit
fa78bb02d4
7 files changed
+134
-35
No files matched your search
@@ -185,6 +185,12 @@
|
||||
"drawer.location_hint": "Choosing another location moves the drawer out of this array and leaves an empty slot behind.",
|
||||
"drawer.confirm_remove": "Remove this drawer? Its slot will be left empty. The drawer can be restored from the Locations page.",
|
||||
|
||||
"notfound.title": "Page Not Found",
|
||||
"notfound.heading": "Page not found",
|
||||
"notfound.message": "The page you are looking for doesn't exist or may have been moved.",
|
||||
"notfound.go_home": "Back to Home",
|
||||
"notfound.redirecting": "Taking you home in",
|
||||
|
||||
"folder.home": "Home",
|
||||
"folder.show_removed": "Show removed items",
|
||||
"folder.edit_location": "Edit Location",
|
||||
|
||||
@@ -185,6 +185,12 @@
|
||||
"drawer.location_hint": "选择其他位置会将抽屉移出此阵列,并留下一个空格子。",
|
||||
"drawer.confirm_remove": "移除此抽屉?其格子将保持为空。可在位置页面恢复该抽屉。",
|
||||
|
||||
"notfound.title": "页面未找到",
|
||||
"notfound.heading": "页面未找到",
|
||||
"notfound.message": "您访问的页面不存在或已被移动。",
|
||||
"notfound.go_home": "返回首页",
|
||||
"notfound.redirecting": "即将返回首页,",
|
||||
|
||||
"folder.home": "首页",
|
||||
"folder.show_removed": "显示已移除的项目",
|
||||
"folder.edit_location": "编辑位置",
|
||||
|
||||
+56
-35
@@ -73,7 +73,7 @@ type DrawerCell struct {
|
||||
func (s *Server) handleShortRedirect(w http.ResponseWriter, r *http.Request) {
|
||||
slugVal := r.PathValue("slug")
|
||||
if slugVal == "" {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
slugVal = strings.ToLower(slugVal)
|
||||
@@ -82,7 +82,7 @@ func (s *Server) handleShortRedirect(w http.ResponseWriter, r *http.Request) {
|
||||
link, err := s.db.GetLinkBySlug(slugVal)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
@@ -514,13 +514,13 @@ func (s *Server) handleItemCreate(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleItemEdit(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := s.db.GetItem(id)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -545,13 +545,13 @@ func (s *Server) handleItemEdit(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleItemSave(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := s.db.GetItem(id)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -669,7 +669,7 @@ func (s *Server) handleItemSave(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleItemRemove(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -680,7 +680,7 @@ func (s *Server) handleItemRemove(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleItemArchive(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -691,7 +691,7 @@ func (s *Server) handleItemArchive(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleItemRestore(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -702,7 +702,7 @@ func (s *Server) handleItemRestore(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleItemPermanentDelete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -787,10 +787,22 @@ func parseGridSize(r *http.Request) (int64, int64) {
|
||||
return rows, cols
|
||||
}
|
||||
|
||||
// handleLocationEditPage serves the GET links used around the app (e.g. the
|
||||
// folder view's "Edit Location" button) by opening the edit form on the
|
||||
// Locations page.
|
||||
func (s *Server) handleLocationEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/admin/locations?edit="+strconv.FormatInt(id, 10), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (s *Server) handleLocationEdit(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -817,7 +829,7 @@ func (s *Server) handleLocationEdit(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleLocationRemove(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -828,7 +840,7 @@ func (s *Server) handleLocationRemove(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleLocationArchive(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -839,7 +851,7 @@ func (s *Server) handleLocationArchive(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleLocationRestore(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -850,7 +862,7 @@ func (s *Server) handleLocationRestore(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleLocationPermanentDelete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -901,13 +913,13 @@ func (s *Server) handleDrawerCreate(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleDrawerEdit(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
drawer, err := s.db.GetLocation(id)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -941,13 +953,13 @@ func (s *Server) handleDrawerEdit(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleDrawerRemove(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
drawer, err := s.db.GetLocation(id)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1024,7 +1036,7 @@ func (s *Server) handleTagCreate(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleTagEdit(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1039,7 +1051,7 @@ func (s *Server) handleTagEdit(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleTagRemove(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1050,7 +1062,7 @@ func (s *Server) handleTagRemove(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleTagArchive(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1061,7 +1073,7 @@ func (s *Server) handleTagArchive(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleTagRestore(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1072,7 +1084,7 @@ func (s *Server) handleTagRestore(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleTagPermanentDelete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1169,7 +1181,7 @@ func (s *Server) handleLinkCreate(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleLinkSave(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1202,7 +1214,7 @@ func (s *Server) handleLinkSave(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleLinkRemove(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1213,7 +1225,7 @@ func (s *Server) handleLinkRemove(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleLinkArchive(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1224,7 +1236,7 @@ func (s *Server) handleLinkArchive(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleLinkRestore(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1235,7 +1247,7 @@ func (s *Server) handleLinkRestore(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) handleLinkPermanentDelete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1402,6 +1414,15 @@ func (s *Server) render(w http.ResponseWriter, r *http.Request, name string, dat
|
||||
}
|
||||
}
|
||||
|
||||
// render404 serves the styled 404 page that links (and auto-redirects) back home.
|
||||
func (s *Server) render404(w http.ResponseWriter, r *http.Request) {
|
||||
csrf := s.csrfToken(w, r) // sets the CSRF cookie, so it must run before WriteHeader
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
s.render(w, r, "404.html", PageData{
|
||||
CSRFToken: csrf,
|
||||
})
|
||||
}
|
||||
|
||||
// syncItemShortlink automatically synchronizes the shortlink URL target as /{location_slug}/{item_slug}
|
||||
func (s *Server) syncItemShortlink(itemID int64, locID int64) {
|
||||
var locSlug string
|
||||
@@ -1461,13 +1482,13 @@ func (s *Server) handlePublicFallback(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
}
|
||||
|
||||
func (s *Server) handleFolderView(w http.ResponseWriter, r *http.Request) {
|
||||
slugVal := r.PathValue("location_slug")
|
||||
if slugVal == "" {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
slugVal = strings.ToLower(slugVal)
|
||||
@@ -1475,7 +1496,7 @@ func (s *Server) handleFolderView(w http.ResponseWriter, r *http.Request) {
|
||||
loc, err := s.db.GetLocationBySlug(slugVal)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
@@ -1580,7 +1601,7 @@ func (s *Server) handlePublicItemView(w http.ResponseWriter, r *http.Request) {
|
||||
link, err := s.db.GetLinkBySlug(itemSlug)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
@@ -1588,13 +1609,13 @@ func (s *Server) handlePublicItemView(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if !link.ItemID.Valid {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := s.db.GetItem(link.ItemID.Int64)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
s.render404(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -136,6 +136,7 @@ func (s *Server) Start() error {
|
||||
|
||||
// 7. Admin locations management
|
||||
mux.HandleFunc("GET /admin/locations", s.requireAuth(s.handleAdminLocations))
|
||||
mux.HandleFunc("GET /admin/locations/edit/{id}", s.requireAuth(s.handleLocationEditPage))
|
||||
mux.HandleFunc("POST /admin/locations/new", s.requireAuth(s.requireCSRF(s.handleLocationCreate)))
|
||||
mux.HandleFunc("POST /admin/locations/edit/{id}", s.requireAuth(s.requireCSRF(s.handleLocationEdit)))
|
||||
mux.HandleFunc("POST /admin/locations/delete/{id}", s.requireAuth(s.requireCSRF(s.handleLocationRemove)))
|
||||
|
||||
@@ -1706,6 +1706,44 @@ tr:last-child .actions-dropdown-menu {
|
||||
border-color: #b91c1c;
|
||||
}
|
||||
|
||||
/* 404 Not Found page */
|
||||
.notfound-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
min-height: 60vh;
|
||||
padding: 2rem 1rem;
|
||||
}
|
||||
|
||||
.notfound-code {
|
||||
font-size: 6rem;
|
||||
font-weight: 800;
|
||||
line-height: 1;
|
||||
color: var(--color-brand-primary);
|
||||
text-shadow: 0 4px 0 var(--color-brand-light);
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.notfound-heading {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 700;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.notfound-message {
|
||||
color: var(--color-text-muted);
|
||||
margin-bottom: 1.5rem;
|
||||
max-width: 28rem;
|
||||
}
|
||||
|
||||
.notfound-countdown {
|
||||
color: var(--color-text-light);
|
||||
font-size: 0.85rem;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
/* Drawer modal */
|
||||
.drawer-modal-backdrop[hidden] {
|
||||
display: none !important;
|
||||
|
||||
@@ -109,6 +109,7 @@ func initTemplates(translator *i18n.Translator) {
|
||||
"shortener.html",
|
||||
"print_labels.html",
|
||||
"location_folder.html",
|
||||
"404.html",
|
||||
}
|
||||
|
||||
for _, page := range layoutPages {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
{{define "title"}}{{T "notfound.title"}}{{end}}
|
||||
|
||||
{{define "content"}}
|
||||
<div class="notfound-container">
|
||||
<div class="notfound-code">404</div>
|
||||
<h1 class="notfound-heading">{{T "notfound.heading"}}</h1>
|
||||
<p class="notfound-message">{{T "notfound.message"}}</p>
|
||||
<a href="/" class="btn btn-primary">{{T "notfound.go_home"}}</a>
|
||||
<p class="notfound-countdown">{{T "notfound.redirecting"}} <span id="notfound-seconds">8</span>s</p>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
let seconds = 8;
|
||||
const el = document.getElementById("notfound-seconds");
|
||||
const timer = setInterval(() => {
|
||||
seconds--;
|
||||
if (el) el.textContent = seconds;
|
||||
if (seconds <= 0) {
|
||||
clearInterval(timer);
|
||||
window.location.href = "/";
|
||||
}
|
||||
}, 1000);
|
||||
})();
|
||||
</script>
|
||||
{{end}}
|
||||
Reference in new issue
Block a user