diff --git a/internal/i18n/locales/en.json b/internal/i18n/locales/en.json index 2c98384..0cdfacb 100644 --- a/internal/i18n/locales/en.json +++ b/internal/i18n/locales/en.json @@ -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", diff --git a/internal/i18n/locales/zh.json b/internal/i18n/locales/zh.json index 47c5dcd..4bba572 100644 --- a/internal/i18n/locales/zh.json +++ b/internal/i18n/locales/zh.json @@ -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": "编辑位置", diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 066a4cb..1f186d1 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -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 } diff --git a/internal/server/server.go b/internal/server/server.go index 1ef1890..0705e63 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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))) diff --git a/internal/server/static/css/styles.css b/internal/server/static/css/styles.css index 8de757e..4e66d69 100644 --- a/internal/server/static/css/styles.css +++ b/internal/server/static/css/styles.css @@ -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; diff --git a/internal/server/templates.go b/internal/server/templates.go index 29984cf..9ef5ad2 100644 --- a/internal/server/templates.go +++ b/internal/server/templates.go @@ -109,6 +109,7 @@ func initTemplates(translator *i18n.Translator) { "shortener.html", "print_labels.html", "location_folder.html", + "404.html", } for _, page := range layoutPages { diff --git a/internal/server/templates/404.html b/internal/server/templates/404.html new file mode 100644 index 0000000..33a0c52 --- /dev/null +++ b/internal/server/templates/404.html @@ -0,0 +1,26 @@ +{{define "title"}}{{T "notfound.title"}}{{end}} + +{{define "content"}} +
+ + +{{end}}