Two intertwined refactors that share too many files to split cleanly.
Both are described separately below.
PART 1 — in-dir convention for table+form spec files
Old layout had the spec at the parent and rows in a child:
archive/<party>/
mdl.table.yaml spec
mdl.form.yaml row-edit form
mdl/ rows-dir
row-001.yaml ...
URLs were /<dir>/mdl.table.html and /<dir>/mdl.form.html. Copying
mdl/ elsewhere lost the spec and form because they lived next door.
New layout collapses everything into the rows-dir:
archive/<party>/mdl/ self-contained
table.yaml spec
form.yaml row-edit form
row-001.yaml ... rows
URLs become /<dir>/mdl/table.html and /<dir>/mdl/form.html. The
"copying-the-folder-takes-everything" property the user asked for
falls out by construction; the row-edit URL /<dir>/<id>.yaml.html
keeps the same shape (spec is now in the same dir, not the
grandparent).
Server changes:
- internal/handler/tablehandler.go RecognizeTableRequest fires on
/<dir>/table.html when <dir>/table.yaml exists. The .zddc.tables
alias map is gone — pure presence-based discovery now matches
the form system's existing convention. Default-MDL fallback at
archive/<party>/mdl/ stays for the virgin-archive case (the
rows-dir need not exist on disk; the URL renders fully virtually).
- internal/handler/formhandler.go RecognizeFormRequest fires on
/<dir>/form.html and /<dir>/<id>.yaml.html with spec at
<dir>/form.yaml. specEligible accepts on-disk files OR the
default-MDL virtual path so an empty mdl/ dir still surfaces the
add-row form.
- internal/handler/tablehandler.go IsDefaultMdlSpec moves to
serving archive/<party>/mdl/{table,form}.yaml (5 segments after
ZDDC_ROOT). New isAtArchivePartyMdlLevel predicate; new
isAtArchivePartyMdlDir for directory-based recognition. New
IsDefaultMdlSpecAbs accessor for callers that hold an abs path
rather than a URL (formhandler).
- internal/handler/formhandler.go loadFormSpec(fsRoot, path) falls
back to embedded default-MDL bytes when os.ReadFile returns
NotExist AND the path matches the archive-party-mdl shape. Three
call sites updated to pass cfg.Root.
- internal/handler/formhandler.go serveFormCreate writes
submissions to filepath.Dir(req.SpecPath) — the spec, the form,
and rows all live in one directory. The submissionsDir creation
is idempotent (MkdirAll); cascade falls back one level for ACL
evaluation when the dir hasn't been materialized yet.
- internal/handler/tablehandler.go tableRowsRedirect now points at
/<dir>/table.html (was /<dir>.table.html) when the directory
request maps to a recognized table.
- cmd/zddc-server/main.go dispatch synth flips from
urlPath + ".table.html" to urlPath + "/table.html" for the
no-trailing-slash → tables-app routing.
- internal/apps/availability.go DefaultAppAt comment clarified
that the dir at archive/<party>/mdl/ IS the table (not a child).
Client changes:
- tables/js/context.js walkServer fetches <currentdir>/table.yaml
directly — no .zddc walk for table declarations. Rows are every
*.yaml in current dir EXCLUDING table.yaml and form.yaml. The
.zddc fetch-for-aliases is gated on file:// (online mode 404s
on .zddc reads via the dispatcher's reserve guard, so skipping
the request avoids browser console noise).
- tables/js/main.js add-row button links to relative form.html
(same dir).
- tables/js/render.js + filters.js: every column's autofilter is
uniformly a text-contains input, even enum columns — keeps the
filter row visually consistent and doesn't constrain users to
the enum vocabulary.
PART 2 — unified table+form HTML bundle
The form-render and table-render code paths share field schemas,
the cell editor for excel-mode IS a form widget, and the form
system's POST-back / validation already exists. Combining the two
HTMLs eliminates duplicating jsyaml/jsonschema/theme/source-
detection/.zddc-parsing across two single-file tools.
- tables/template.html grows two top-level mode containers:
#table-mode (toolbar + sortable table) and #form-mode (form +
submit button). Both hidden at parse time; the dispatcher
unhides one. The shared #form-context placeholder was added
here so the server's existing injectFormContext target
resolves.
- tables/js/mode.js (new) sets window.zddcMode synchronously
based on URL pattern: /form.html or /<id>.yaml.html → form,
/table.html → table, else inline-context fallback for
file:// (whichever context blob is non-empty wins). Unhides
the matching container at DOMContentLoaded.
- tables/js/main.js init() and form/js/main.js boot() each guard
early when mode isn't theirs. Both apps live on different
globals (window.tablesApp vs window.formApp) so module
registration doesn't collide.
- form/js/main.js title write falls back from #form-title to
#table-title (the unified bundle's shared header element)
when the dedicated id isn't present.
- tables/build.sh concatenates form modules (widgets, render,
object, array, errors, post, serialize, util) and form CSS.
No new external deps. Bundle grows from ~95KB to ~120KB.
- internal/handler/formhandler.go drops the //go:embed form.html
directive; serveFormRender now writes embeddedTablesHTML via
a small formRenderHTML() accessor (var declared in
tablehandler.go, same package). The embedded form.html file
is removed.
- build script: cp form/dist/form.html → internal/handler/form.html
step is gone (file no longer exists in the source tree). cp
tables/dist/tables.html → internal/handler/tables.html now
runs unconditionally rather than only on beta/stable cuts —
the renderer is a fixed binary component and dev iteration
needs the embedded copy refreshed every build. Channel-cascaded
apps (internal/apps/embedded/) stay channel-gated as before.
- form/dist/form.html still builds for standalone offline-only
use (downloadable from /releases/), but no longer goes into
the binary.
Tests:
- internal/handler/tablehandler_test.go and formhandler_test.go
rewritten for the in-dir layout. New test
TestRecognizeFormRequest_DefaultMdlAtArchiveParty covers
empty-form, create POST, re-edit row, and the negative cases
(Working/, non-mdl name) where the fallback must NOT fire.
- internal/handler/directory_test.go updated for the new
/<dir>/table.html redirect target.
- cmd/zddc-server/main_test.go TestDispatchSlashRouting Location
expectation updated.
- tests/form-safety.spec.js loads tables/dist/tables.html
(named form.html in the temp dir to trigger form-mode in the
dispatcher) so it tests the same bytes the server returns.
Title-element selector switches to #table-title.
- tests/tables.spec.js updates the status-filter test for the
uniform text-input filter.
Docs:
- AGENTS.md form-data system rewrites the URL conventions and
storage layout for in-dir; gains a Tables system section
parallel to forms describing the self-contained-directory
property; subfolder rules ("one table per folder by
construction; subfolders allowed and silently ignored as rows
— legitimate uses: nested sub-tables, per-row attachments,
drafts, future history sidecars") so we don't re-derive this.
Not included (deferred):
- ACL gating on cell-level writes — not relevant until Phase 3.
- Editable cells UI — separate commit (Phase 1).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
297 lines
11 KiB
Go
297 lines
11 KiB
Go
// Package handler — tablehandler.go: directory-of-YAML table view.
|
|
//
|
|
// URL convention:
|
|
//
|
|
// GET /<dir>/<name>.table.html → tables.html, only when <dir>/.zddc
|
|
// declares tables: { <name>: <spec-path> }
|
|
// AND the spec file exists.
|
|
//
|
|
// Discovery is .zddc-declarative (no auto-mount on file presence). The
|
|
// handler's only jobs are:
|
|
//
|
|
// 1. Recognize the URL — does <dir>/.zddc declare a table named <name>,
|
|
// and does the referenced *.table.yaml spec actually exist? If not,
|
|
// fall through to the static-file pipeline.
|
|
// 2. Gate on the cascading ACL at the request directory (read action).
|
|
// 3. Serve the static tables.html bytes.
|
|
//
|
|
// All rendering is client-side. The page (built from tables/) detects
|
|
// HTTP vs file:// mode, then walks the directory via shared/zddc-source.js
|
|
// (HttpDirectoryHandle in HTTP mode, real FileSystemDirectoryHandle in
|
|
// file mode), reads .zddc, parses the spec, and renders rows in the
|
|
// browser. The server does not pre-parse the spec, list rows, or compute
|
|
// per-row "editable" — the client does, and ACL is enforced naturally:
|
|
// HTTP-mode reads/writes go through the cascade-aware file API, and
|
|
// local-mode reads/writes are bounded by whatever the OS gave the
|
|
// FS-Access handle.
|
|
package handler
|
|
|
|
import (
|
|
_ "embed"
|
|
"log/slog"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"codeberg.org/VARASYS/ZDDC/zddc/internal/config"
|
|
"codeberg.org/VARASYS/ZDDC/zddc/internal/policy"
|
|
"codeberg.org/VARASYS/ZDDC/zddc/internal/zddc"
|
|
)
|
|
|
|
//go:embed tables.html
|
|
var embeddedTablesHTML []byte
|
|
|
|
//go:embed default-mdl.table.yaml
|
|
var embeddedDefaultMdlTable []byte
|
|
|
|
//go:embed default-mdl.form.yaml
|
|
var embeddedDefaultMdlForm []byte
|
|
|
|
// DefaultMdlTableYAML returns the embedded default mdl.table.yaml bytes.
|
|
// Used by the static-file handler to serve the default spec at
|
|
// archive/<party>/mdl.table.yaml when no operator file exists on disk.
|
|
func DefaultMdlTableYAML() []byte { return embeddedDefaultMdlTable }
|
|
|
|
// DefaultMdlFormYAML returns the embedded default mdl.form.yaml bytes.
|
|
func DefaultMdlFormYAML() []byte { return embeddedDefaultMdlForm }
|
|
|
|
// IsDefaultMdlSpec reports whether urlPath is one of the default-MDL
|
|
// virtual files served when no operator file exists on disk:
|
|
//
|
|
// <project>/archive/<party>/mdl/table.yaml
|
|
// <project>/archive/<party>/mdl/form.yaml
|
|
//
|
|
// The MDL files live INSIDE the rows-dir along with row YAMLs so the
|
|
// whole directory is self-contained — copying mdl/ moves the spec,
|
|
// the form, and all rows together. Returns embedded bytes + true when
|
|
// the fallback should fire; nil + false when an operator-supplied
|
|
// file exists or the path is not eligible.
|
|
func IsDefaultMdlSpec(fsRoot, urlPath string) ([]byte, bool) {
|
|
base := strings.ToLower(filepath.Base(urlPath))
|
|
var bytes []byte
|
|
switch base {
|
|
case "table.yaml":
|
|
bytes = embeddedDefaultMdlTable
|
|
case "form.yaml":
|
|
bytes = embeddedDefaultMdlForm
|
|
default:
|
|
return nil, false
|
|
}
|
|
if !isAtArchivePartyMdlLevel(fsRoot, urlPath) {
|
|
return nil, false
|
|
}
|
|
// Operator file wins if it exists on disk.
|
|
rel := strings.TrimPrefix(filepath.ToSlash(urlPath), "/")
|
|
abs := filepath.Join(fsRoot, filepath.FromSlash(rel))
|
|
if !strings.HasPrefix(abs, fsRoot+string(filepath.Separator)) && abs != fsRoot {
|
|
return nil, false
|
|
}
|
|
if fileExists(abs) {
|
|
return nil, false
|
|
}
|
|
return bytes, true
|
|
}
|
|
|
|
// IsDefaultMdlSpecAbs is the abs-path-keyed variant of IsDefaultMdlSpec.
|
|
// Used by handlers that hold a filesystem path rather than a URL.
|
|
// Returns the embedded default bytes + true when absPath is the
|
|
// virtual archive/<party>/{mdl.table.yaml, mdl.form.yaml} fallback.
|
|
func IsDefaultMdlSpecAbs(fsRoot, absPath string) ([]byte, bool) {
|
|
if !strings.HasPrefix(absPath, fsRoot+string(filepath.Separator)) && absPath != fsRoot {
|
|
return nil, false
|
|
}
|
|
rel, err := filepath.Rel(fsRoot, absPath)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
urlPath := "/" + filepath.ToSlash(rel)
|
|
return IsDefaultMdlSpec(fsRoot, urlPath)
|
|
}
|
|
|
|
// isAtArchivePartyLevel reports whether urlPath refers to a file
|
|
// directly under <project>/archive/<party>/ (depth-3 directory). The
|
|
// canonical-folder names are case-folded.
|
|
func isAtArchivePartyLevel(fsRoot, urlPath string) bool {
|
|
rel := strings.Trim(filepath.ToSlash(urlPath), "/")
|
|
parts := strings.Split(rel, "/")
|
|
// <project>/archive/<party>/<file> = 4 segments
|
|
if len(parts) != 4 {
|
|
return false
|
|
}
|
|
return strings.EqualFold(parts[1], "archive")
|
|
}
|
|
|
|
// isAtArchivePartyMdlLevel reports whether urlPath refers to a file
|
|
// directly under <project>/archive/<party>/mdl/ (depth-4 directory).
|
|
// Used by the default-MDL fallback after the spec/form moved INSIDE
|
|
// the rows-dir for self-containment.
|
|
func isAtArchivePartyMdlLevel(fsRoot, urlPath string) bool {
|
|
rel := strings.Trim(filepath.ToSlash(urlPath), "/")
|
|
parts := strings.Split(rel, "/")
|
|
// <project>/archive/<party>/mdl/<file> = 5 segments
|
|
if len(parts) != 5 {
|
|
return false
|
|
}
|
|
return strings.EqualFold(parts[1], "archive") && strings.EqualFold(parts[3], "mdl")
|
|
}
|
|
|
|
// TableRequest describes a recognized table-system request.
|
|
type TableRequest struct {
|
|
// Name is the table's URL stem (the key declared in .zddc tables).
|
|
Name string
|
|
// SpecPath is the absolute filesystem path to the *.table.yaml.
|
|
// Validated to exist at recognition time.
|
|
SpecPath string
|
|
// Dir is the absolute path to the request directory (where the
|
|
// .zddc declared the table).
|
|
Dir string
|
|
}
|
|
|
|
// tableRowsRedirect reports the canonical /<dir>/table.html URL to
|
|
// redirect to when (urlPath) names a directory that contains a
|
|
// table.yaml (or matches the default-MDL fallback). Returns "" when
|
|
// no redirect should fire.
|
|
//
|
|
// Recognition reuses RecognizeTableRequest by synthesizing the
|
|
// equivalent <urlPath>table.html and asking the recognizer whether
|
|
// it's a real (or default-MDL) table. Single source of truth for
|
|
// validation.
|
|
func tableRowsRedirect(fsRoot, urlPath string) string {
|
|
// urlPath is the directory request — e.g. "/proj/archive/Acme/mdl/".
|
|
if urlPath == "" || urlPath == "/" {
|
|
return ""
|
|
}
|
|
if !strings.HasSuffix(urlPath, "/") {
|
|
urlPath += "/"
|
|
}
|
|
synthesized := urlPath + "table.html"
|
|
if RecognizeTableRequest(fsRoot, http.MethodGet, synthesized) == nil {
|
|
return ""
|
|
}
|
|
return synthesized
|
|
}
|
|
|
|
// RecognizeTableRequest classifies r as a table-system request, or
|
|
// returns nil if it falls through to other handlers. Discovery is
|
|
// presence-based and self-contained: a /<dir>/table.html URL fires
|
|
// when <dir>/table.yaml exists on disk, or when the default-MDL
|
|
// fallback at archive/<party>/mdl/ applies.
|
|
//
|
|
// The spec, the row-edit form, and all rows live together in <dir>.
|
|
// Copying <dir> elsewhere copies everything needed to re-host the
|
|
// table — that's the whole point of the in-dir layout.
|
|
//
|
|
// The table's "name" is the directory's basename (so the URL
|
|
// /<parent>/mdl/table.html names the "mdl" table, with rows in mdl/).
|
|
//
|
|
// Methods other than GET return nil — the table is read-only at the
|
|
// URL level. Writes go through the file API directly.
|
|
func RecognizeTableRequest(fsRoot, method, urlPath string) *TableRequest {
|
|
if method != http.MethodGet {
|
|
return nil
|
|
}
|
|
if !strings.HasSuffix(urlPath, "/table.html") && urlPath != "/table.html" {
|
|
return nil
|
|
}
|
|
|
|
// Strip /table.html — what remains is the rows-dir.
|
|
rel := strings.TrimSuffix(strings.TrimPrefix(urlPath, "/"), "/table.html")
|
|
rel = strings.TrimSuffix(rel, "table.html") // handles "/table.html" at root → ""
|
|
rel = strings.Trim(rel, "/")
|
|
if rel == "" {
|
|
// /table.html at root has no rows-dir to name.
|
|
return nil
|
|
}
|
|
dirAbs := filepath.Join(fsRoot, filepath.FromSlash(rel))
|
|
if !strings.HasPrefix(dirAbs, fsRoot+string(filepath.Separator)) && dirAbs != fsRoot {
|
|
return nil
|
|
}
|
|
name := filepath.Base(dirAbs)
|
|
|
|
specAbs := filepath.Join(dirAbs, "table.yaml")
|
|
|
|
// Presence-based discovery: <dir>/table.yaml on disk.
|
|
if fileExists(specAbs) {
|
|
return &TableRequest{Name: name, SpecPath: specAbs, Dir: dirAbs}
|
|
}
|
|
|
|
// Default-MDL virtual-spec fallback at archive/<party>/mdl/. The
|
|
// spec bytes come from IsDefaultMdlSpec via the static-file
|
|
// dispatcher when no on-disk file exists at that path; the rows-dir
|
|
// itself doesn't need to exist either (fully virtual archive).
|
|
if isAtArchivePartyMdlDir(fsRoot, dirAbs) {
|
|
return &TableRequest{Name: "mdl", SpecPath: specAbs, Dir: dirAbs}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// isAtArchivePartyMdlDir reports whether dirAbs is exactly
|
|
// <fsRoot>/<project>/archive/<party>/mdl. Used by the default-MDL
|
|
// fallback to recognize the virtual rows-dir whether or not it
|
|
// exists on disk.
|
|
func isAtArchivePartyMdlDir(fsRoot, dirAbs string) bool {
|
|
rel, err := filepath.Rel(fsRoot, dirAbs)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
rel = filepath.ToSlash(rel)
|
|
if strings.HasPrefix(rel, "../") || rel == ".." || rel == "." {
|
|
return false
|
|
}
|
|
parts := strings.Split(rel, "/")
|
|
if len(parts) != 4 {
|
|
return false
|
|
}
|
|
return strings.EqualFold(parts[1], "archive") && strings.EqualFold(parts[3], "mdl")
|
|
}
|
|
|
|
// isNotExistError reports whether err indicates a missing file. Local
|
|
// helper to avoid pulling errors.Is into the handler package.
|
|
func isNotExistError(err error) bool {
|
|
return err != nil && strings.Contains(err.Error(), "no such file or directory")
|
|
}
|
|
|
|
// isArchivePartyDir reports whether dirAbs is a <project>/archive/<party>/
|
|
// directory under fsRoot, with archive case-folded.
|
|
func isArchivePartyDir(fsRoot, dirAbs string) bool {
|
|
rel, err := filepath.Rel(fsRoot, dirAbs)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
rel = filepath.ToSlash(rel)
|
|
if strings.HasPrefix(rel, "../") || rel == ".." || rel == "." {
|
|
return false
|
|
}
|
|
parts := strings.Split(rel, "/")
|
|
if len(parts) != 3 {
|
|
return false
|
|
}
|
|
return strings.EqualFold(parts[1], "archive")
|
|
}
|
|
|
|
// ServeTable serves the static tables.html bytes for a recognized
|
|
// request. ACL gate is the read action at the request directory; on
|
|
// allow, the embedded HTML is written verbatim. The client takes over
|
|
// from there — see tables/js/main.js.
|
|
func ServeTable(cfg config.Config, req *TableRequest, w http.ResponseWriter, r *http.Request) {
|
|
email := EmailFromContext(r)
|
|
decider := DeciderFromContext(r)
|
|
|
|
chain, err := zddc.EffectivePolicy(cfg.Root, req.Dir)
|
|
if err != nil {
|
|
slog.Warn("table: policy error", "path", req.Dir, "err", err)
|
|
}
|
|
if allowed, _ := policy.AllowActionFromChain(r.Context(), decider, chain, email, r.URL.Path, policy.ActionRead); !allowed {
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
return
|
|
}
|
|
|
|
if len(embeddedTablesHTML) == 0 {
|
|
http.Error(w, "table renderer not built into this binary", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
_, _ = w.Write(embeddedTablesHTML)
|
|
}
|