aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/server.go
diff options
context:
space:
mode:
authorhistoria <[not public]>2026-09-16 17:05:17 -0400
committerhistoria <[not public]>2026-09-16 17:05:17 -0400
commit1dad15698845d370e998f7cc5feb8aece3fab58f (patch)
tree8d58058782de9f422573e1f8efd5d130ce7d1ce9 /internal/admin/server.go
parenta6cf03a6ae98e4fb675da25046d148e92bcf7206 (diff)
downloadthehouseoficarus-1dad15698845d370e998f7cc5feb8aece3fab58f.tar.gz
refactor: remove dead code, consolidate save/error handling, add lint gate
Diffstat (limited to 'internal/admin/server.go')
-rw-r--r--internal/admin/server.go31
1 files changed, 22 insertions, 9 deletions
diff --git a/internal/admin/server.go b/internal/admin/server.go
index fb0c53b..2c8681a 100644
--- a/internal/admin/server.go
+++ b/internal/admin/server.go
@@ -1,3 +1,6 @@
+// Package admin implements the HTTP admin server: authenticated routes for
+// editing game data (rooms, items, mobs, drops, triggers, colors, …), the
+// embedded web UI, and the undo stack backing it.
package admin
import (
@@ -516,6 +519,16 @@ func writeJSONError(w http.ResponseWriter, msg string, code int) {
json.NewEncoder(w).Encode(map[string]any{"error": msg})
}
+// failJSON writes a JSON error body without changing the HTTP status. This
+// is the long-standing wire behavior most admin editors depend on: they read
+// res.error out of a 200 response, while api wrappers in admin.js treat any
+// non-200 as a thrown raw-text error. Unifying status codes requires a
+// coordinated change across the static JS clients; do not silently switch
+// call sites to non-200 codes here.
+func failJSON(w http.ResponseWriter, msg string) {
+ writeJSONError(w, msg, http.StatusOK)
+}
+
func readJSON(r *http.Request, v any) error {
body, err := io.ReadAll(r.Body)
if err != nil {
@@ -580,20 +593,20 @@ func (s *AdminServer) handleEntityDirs(entityType string) http.HandlerFunc {
NewName string `json:"new_name"`
}
if err := readJSON(r, &body); err != nil {
- writeJSON(w, map[string]any{"error": "invalid"})
+ failJSON(w, "invalid")
return
}
switch body.Action {
case "create":
if err := createEntityDir(s.dataDir, entityType, body.Name); err != nil {
- writeJSON(w, map[string]any{"error": err.Error()})
+ failJSON(w, err.Error())
return
}
writeJSON(w, map[string]any{"ok": true, "name": body.Name})
case "delete":
changes, err := deleteEntityDir(s.dataDir, entityType, body.Dir)
if err != nil {
- writeJSON(w, map[string]any{"error": err.Error()})
+ failJSON(w, err.Error())
return
}
for _, ch := range changes {
@@ -602,12 +615,12 @@ func (s *AdminServer) handleEntityDirs(entityType string) http.HandlerFunc {
writeJSON(w, map[string]any{"ok": true, "moved": len(changes) - 1})
case "rename_dir":
if err := renameEntityDir(s.dataDir, entityType, body.Dir, body.NewName); err != nil {
- writeJSON(w, map[string]any{"error": err.Error()})
+ failJSON(w, err.Error())
return
}
writeJSON(w, map[string]any{"ok": true, "name": body.NewName})
default:
- writeJSON(w, map[string]any{"error": "unknown action"})
+ failJSON(w, "unknown action")
}
}
}
@@ -624,12 +637,12 @@ func (s *AdminServer) handleEntityRename(entityType string) http.HandlerFunc {
}
if err := readJSON(r, &body); err != nil || body.ID == "" || body.NewID == "" ||
!validEntityID(body.NewID) {
- writeJSON(w, map[string]any{"error": "invalid"})
+ failJSON(w, "invalid")
return
}
oldPath, newPath, data, err := renameEntityFile(s.dataDir, entityType, body.ID, body.NewID)
if err != nil {
- writeJSON(w, map[string]any{"error": err.Error()})
+ failJSON(w, err.Error())
return
}
desc := fmt.Sprintf("rename %s %s to %s", entityType, body.ID, body.NewID)
@@ -656,12 +669,12 @@ func (s *AdminServer) handleEntityMove(entityType string) http.HandlerFunc {
Dir string `json:"dir"`
}
if err := readJSON(r, &body); err != nil || body.ID == "" {
- writeJSON(w, map[string]any{"error": "invalid"})
+ failJSON(w, "invalid")
return
}
oldPath, newPath, data, err := moveEntityFile(s.dataDir, entityType, body.ID, body.Dir)
if err != nil {
- writeJSON(w, map[string]any{"error": err.Error()})
+ failJSON(w, err.Error())
return
}
desc := fmt.Sprintf("move %s %s to %s/", entityType, body.ID, body.Dir)