Files
CableGUI/internal/server/handlers.go
mAi c206a331ec rename: mCables → CableGUI (project + repo + image + paths)
Full project rename per m's call. Single atomic commit because the
codebase rename is a coupled change — go module path, env vars, DB
default, Docker artefact names, and on-disk mDock paths all flip
together.

- go.mod: module mgit.msbls.de/m/mcables → mgit.msbls.de/m/cablegui
- cmd/mcables → cmd/cablegui (git mv)
- All Go imports rewritten to the new module path
- Env vars: MCABLES_ADDR/MCABLES_DB → CABLEGUI_ADDR/CABLEGUI_DB
- DB default path: data/mcables.db → data/cablegui.db
- Dockerfile + docker-compose.yml: image, container_name, env vars,
  bind-mount /home/m/stacks/mcables → /home/m/stacks/cablegui,
  secrets /home/m/secrets/mcables → /home/m/secrets/cablegui
- Makefile: bin target + run/build commands point at cmd/cablegui
- .gitignore + .dockerignore: /mcables → /cablegui
- README, docs/design.md, CLAUDE.md: prose + paths + image name
- web/static/index.html: <title> + brand
- web/static/main.js + web/web.go: header comment
- internal/exporter: Scene.Source "mcables" → "cablegui"
- internal/server/export.go: error-detail secrets path
- internal/db/migrations/*.sql: header comments (mCables vN → CableGUI vN)

Memory group_id kept as "mcables" to preserve existing memory continuity.
Documented as historical in CLAUDE.md.

go build ./... clean; go test -race ./... green
2026-05-16 15:35:42 +02:00

239 lines
6.5 KiB
Go

package server
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"mgit.msbls.de/m/cablegui/internal/db"
)
type handlers struct {
store *db.Store
}
// ---------------------------------------------------------------- utility
// writeJSON encodes v as JSON at the given status. Errors during encoding
// are logged-silent (the response has already started) — this is the
// last-resort path; callers should validate inputs early.
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}
type errorBody struct {
Error string `json:"error"`
Details any `json:"details,omitempty"`
}
// writeError maps a Store sentinel to an HTTP status + JSON body.
func writeError(w http.ResponseWriter, err error, details any) {
switch {
case errors.Is(err, db.ErrNotFound):
writeJSON(w, http.StatusNotFound, errorBody{Error: err.Error(), Details: details})
case errors.Is(err, db.ErrConflict):
writeJSON(w, http.StatusConflict, errorBody{Error: err.Error(), Details: details})
case errors.Is(err, db.ErrInUse):
writeJSON(w, http.StatusConflict, errorBody{Error: err.Error(), Details: details})
case errors.Is(err, db.ErrConfirmName):
writeJSON(w, http.StatusBadRequest, errorBody{Error: err.Error(), Details: details})
case errors.Is(err, db.ErrInvalidInput):
writeJSON(w, http.StatusBadRequest, errorBody{Error: err.Error(), Details: details})
case errors.Is(err, db.ErrForbidden):
writeJSON(w, http.StatusForbidden, errorBody{Error: err.Error(), Details: details})
default:
writeJSON(w, http.StatusInternalServerError, errorBody{Error: err.Error(), Details: details})
}
}
func parseInt64Path(r *http.Request, key string) (int64, bool) {
raw := r.PathValue(key)
v, err := strconv.ParseInt(raw, 10, 64)
if err != nil || v <= 0 {
return 0, false
}
return v, true
}
// ---------------------------------------------------------------- health
func (h *handlers) healthz(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}
// ---------------------------------------------------------------- projects
type projectCreate struct {
Name string `json:"name"`
DrawingName string `json:"drawing_name"`
Description string `json:"description"`
}
type projectPatch struct {
Name *string `json:"name,omitempty"`
DrawingName *string `json:"drawing_name,omitempty"`
Description *string `json:"description,omitempty"`
}
func (h *handlers) listProjects(w http.ResponseWriter, _ *http.Request) {
ps, err := h.store.ListProjects()
if err != nil {
writeError(w, err, nil)
return
}
if ps == nil {
ps = []db.Project{}
}
writeJSON(w, http.StatusOK, ps)
}
func (h *handlers) createProject(w http.ResponseWriter, r *http.Request) {
var body projectCreate
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeError(w, errors.Join(db.ErrInvalidInput, err), nil)
return
}
p, err := h.store.CreateProject(body.Name, body.DrawingName, body.Description)
if err != nil {
writeError(w, err, nil)
return
}
writeJSON(w, http.StatusCreated, p)
}
func (h *handlers) getProject(w http.ResponseWriter, r *http.Request) {
id, ok := parseInt64Path(r, "pid")
if !ok {
writeError(w, db.ErrInvalidInput, "pid must be a positive integer")
return
}
snap, err := h.store.Snapshot(id)
if err != nil {
writeError(w, err, nil)
return
}
writeJSON(w, http.StatusOK, snap)
}
func (h *handlers) patchProject(w http.ResponseWriter, r *http.Request) {
id, ok := parseInt64Path(r, "pid")
if !ok {
writeError(w, db.ErrInvalidInput, "pid must be a positive integer")
return
}
var body projectPatch
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeError(w, errors.Join(db.ErrInvalidInput, err), nil)
return
}
p, err := h.store.UpdateProject(id, db.ProjectUpdate{
Name: body.Name,
DrawingName: body.DrawingName,
Description: body.Description,
})
if err != nil {
writeError(w, err, nil)
return
}
writeJSON(w, http.StatusOK, p)
}
func (h *handlers) deleteProject(w http.ResponseWriter, r *http.Request) {
id, ok := parseInt64Path(r, "pid")
if !ok {
writeError(w, db.ErrInvalidInput, "pid must be a positive integer")
return
}
confirm := r.URL.Query().Get("confirm")
if confirm == "" {
writeError(w, db.ErrConfirmName,
"DELETE requires ?confirm=<project name> matching the project's current name")
return
}
if err := h.store.DeleteProject(id, confirm); err != nil {
writeError(w, err, nil)
return
}
w.WriteHeader(http.StatusNoContent)
}
// ---------------------------------------------------------------- cable_types
type cableTypeCreate struct {
Name string `json:"name"`
Color string `json:"color"`
}
type cableTypePatch struct {
Name *string `json:"name,omitempty"`
Color *string `json:"color,omitempty"`
}
func (h *handlers) listCableTypes(w http.ResponseWriter, _ *http.Request) {
ts, err := h.store.ListCableTypes()
if err != nil {
writeError(w, err, nil)
return
}
writeJSON(w, http.StatusOK, ts)
}
func (h *handlers) createCableType(w http.ResponseWriter, r *http.Request) {
var body cableTypeCreate
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeError(w, errors.Join(db.ErrInvalidInput, err), nil)
return
}
t, err := h.store.CreateCableType(body.Name, body.Color)
if err != nil {
writeError(w, err, nil)
return
}
writeJSON(w, http.StatusCreated, t)
}
func (h *handlers) patchCableType(w http.ResponseWriter, r *http.Request) {
id, ok := parseInt64Path(r, "id")
if !ok {
writeError(w, db.ErrInvalidInput, "id must be a positive integer")
return
}
var body cableTypePatch
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeError(w, errors.Join(db.ErrInvalidInput, err), nil)
return
}
t, err := h.store.UpdateCableType(id, db.CableTypeUpdate{
Name: body.Name,
Color: body.Color,
})
if err != nil {
writeError(w, err, nil)
return
}
writeJSON(w, http.StatusOK, t)
}
func (h *handlers) deleteCableType(w http.ResponseWriter, r *http.Request) {
id, ok := parseInt64Path(r, "id")
if !ok {
writeError(w, db.ErrInvalidInput, "id must be a positive integer")
return
}
if err := h.store.DeleteCableType(id); err != nil {
// On ErrInUse, count referencing cables so the client can show
// "blocked by N cables".
if errors.Is(err, db.ErrInUse) {
n, _ := h.store.CountCablesUsingType(id)
writeError(w, err, map[string]int{"in_use_by_cables": n})
return
}
writeError(w, err, nil)
return
}
w.WriteHeader(http.StatusNoContent)
}