Files
restic-manager/internal/server/ui/ui.go
T
steve 6e47efc146 P3-01/02/03: restore wizard backend + templates + restore-shaped job page
End-to-end wizard from /hosts/{id}/restore (or per-snapshot deep link
/hosts/{id}/snapshots/{sid}/restore) → tree-browse → dispatch →
restore-shaped live job page.

Backend (internal/server/http/ui_restore.go):
- GET handlers render the four-step wizard against the wireframe shape
  in docs/superpowers/specs/2026-05-04-p3-restore-design.md.
- HTMX tree partial endpoint hits fetchTreeWithCache (P3-X2) so each
  directory expansion is a sub-second cached lookup after the first
  miss.
- POST validates: snapshot_id non-empty, ≥1 absolute path, in-place
  mode requires confirm_hostname == host name, agent online. On error
  re-renders the wizard with the operator's input intact. Happy path
  mints a job_id, computes the new-directory target as
  /var/restic-restore/<job-id>/ (operator can't escape the prefix —
  server picks it), creates the job row, ships command.run with
  kind=restore + RestorePayload, writes a host.restore audit row,
  returns HX-Redirect (or 303) to the live job page.

Templates:
- host_restore.html: single-page progressively-enabled wizard matching
  _diag/p3-restore-wizard wireframe. Form-state-driven JS computes a
  running tally of selected paths and the step-4 confirm summary
  client-side; the server re-renders on validation failure with form
  fields preserved.
- partials/tree_node.html: recursive HTMX-served tree fragment.
- Top-level Restore button on host_detail right rail + per-snapshot
  Restore action on snapshot rows replace the previous P3-stub.

Restore-shaped job page (job_detail.html):
- Progress widget rendered as a panel rather than a bare strip when
  the job is active.
- Current-file display under the bar, updated from log.stream stdout
  lines that look like absolute paths. Hidden for non-restore kinds.

Migration 0012:
- Add restore + diff to the jobs.kind CHECK. Rebuild required (SQLite
  can't ALTER CHECK in place); follows the safe pattern from 0005.
  Defensive: stash job_logs into a temp table before the rebuild and
  INSERT OR IGNORE back afterwards so even if SQLite cascades on
  DROP TABLE jobs the log history survives.

Tests:
- ui_restore_test covers GET step-1 render, GET pre-selected snapshot
  summary card, POST missing snapshot, POST missing paths, POST
  in-place wrong-hostname rejection (no command.run leaks to the
  agent), POST happy path (HX-Redirect + correct payload + audit
  row), POST against offline host returns 503.

Restage block (CLAUDE.md) deferred to the end of the restore phase.
2026-05-04 15:34:29 +01:00

159 lines
4.9 KiB
Go

// Package ui renders the HTMX/Tailwind frontend from server-side
// html/templates. Templates live under web/templates and are
// embedded into the binary via web.FS.
//
// Lifecycle:
// - At startup, parse every layout, partial, and page into a
// single *template.Template tree.
// - To render a page, call Render(w, "<page>", ViewData{...}).
// Render walks the page's template definitions (which override
// the {{block "content"}} / {{block "title"}} placeholders in
// the chosen layout) and writes the result.
package ui
import (
"fmt"
"html/template"
"io"
"io/fs"
"path"
"strings"
"gitea.dcglab.co.uk/steve/restic-manager/web"
)
// ViewData is the common frame every template renders against.
// Page handlers populate Page with their own concrete shape and the
// renderer wraps it.
type ViewData struct {
// Title is rendered in <title>; layouts/base default to
// "restic-manager" if absent. Pages that {{define "title"}} win.
Title string
// User is the currently signed-in user, or nil if the session
// cookie is missing/invalid. The nav uses this to decide
// whether to show "Sign out" or "Sign in".
User *User
// Active is the slug of the currently active primary nav tab
// ("dashboard" / "repos" / "alerts" / "audit" / "settings").
// The nav partial highlights the matching tab.
Active string
// OpenAlerts is shown next to the Alerts tab when > 0.
OpenAlerts int
// Version is the build version printed in the top-right of the
// chrome. Falls back to "dev" if the binary wasn't built with
// -ldflags -X main.version=…
Version string
// Username pre-fills the login form on a re-render after a bad
// attempt. Login-only.
Username string
// Error is a single banner-level error string. Login uses it
// today; other pages can adopt the same field.
Error string
// Page carries page-specific data. Concrete type is the page's
// own struct.
Page any
}
// User is the minimal projection of the authenticated user that the
// templates need. Avoids leaking store internals into the view.
type User struct {
ID string
Username string
Role string
}
// Renderer holds the parsed templates.
type Renderer struct {
pages map[string]*template.Template
}
// New parses every layout, partial, and page from web.FS into one
// template tree per page. Pages associate with a layout via the
// path under templates/pages/: anything at templates/pages/login.html
// wraps in templates/layouts/chromeless.html, everything else wraps
// in templates/layouts/base.html.
//
// Returns an error if any template fails to parse — fail loud at
// startup, not at request time.
func New() (*Renderer, error) {
// All layouts + partials are shared.
commonPaths := []string{
"templates/layouts/base.html",
"templates/layouts/chromeless.html",
"templates/partials/nav.html",
"templates/partials/host_row.html",
"templates/partials/toast.html",
"templates/partials/awaiting_agent.html",
"templates/partials/host_chrome.html",
"templates/partials/tree_node.html",
}
pageEntries, err := fs.Glob(web.FS, "templates/pages/*.html")
if err != nil {
return nil, fmt.Errorf("ui: glob pages: %w", err)
}
if len(pageEntries) == 0 {
return nil, fmt.Errorf("ui: no pages found under templates/pages/")
}
r := &Renderer{pages: make(map[string]*template.Template, len(pageEntries))}
for _, p := range pageEntries {
base := strings.TrimSuffix(path.Base(p), ".html")
t, err := template.New(base).Funcs(funcMap()).
ParseFS(web.FS, append(append([]string{}, commonPaths...), p)...)
if err != nil {
return nil, fmt.Errorf("ui: parse %s: %w", p, err)
}
r.pages[base] = t
}
return r, nil
}
// Render writes the named page (e.g. "dashboard", "login") to w,
// wrapped in the appropriate layout. layoutFor decides which.
func (r *Renderer) Render(w io.Writer, page string, data ViewData) error {
t, ok := r.pages[page]
if !ok {
return fmt.Errorf("ui: unknown page %q", page)
}
if data.Version == "" {
data.Version = "dev"
}
return t.ExecuteTemplate(w, layoutFor(page), data)
}
// RenderPartial writes a named partial template to w *without* the
// layout wrap. Used by HTMX endpoints that swap fragments into
// already-rendered pages. The partial is looked up in any page's
// template set (every page parses the full common-paths list, so
// they all see every partial). Pick "dashboard" arbitrarily as the
// lookup point — partials are layout-agnostic.
func (r *Renderer) RenderPartial(w io.Writer, name string, data ViewData) error {
t, ok := r.pages["dashboard"]
if !ok {
return fmt.Errorf("ui: renderer has no pages registered")
}
if data.Version == "" {
data.Version = "dev"
}
return t.ExecuteTemplate(w, name, data)
}
// layoutFor picks the layout name for a page. Login + bootstrap go
// chrome-less; everything else uses the standard navigation chrome.
func layoutFor(page string) string {
switch page {
case "login", "bootstrap":
return "chromeless"
default:
return "base"
}
}