Phase 2: Mode A static site — build, serve, search, stage panel
One Go binary serves a pre-rendered site. No Node, no bundler, no node_modules:
htmx and the SSE extension are vendored files, Markdown and code highlighting
run at build time (goldmark + chroma), and the server only writes files.
Implements docs/phase-2-static-site.md:
2.1 service skeleton cmd/build (content -> dist), cmd/server (dist -> HTTP),
env-configured, 127.0.0.1:8096, /healthz with separate
static / ai / search readiness, --health-check probe,
deploy/compose.yml + deploy/Dockerfile at 120m
2.2 rendering templ components (Layout, Nav, PillarCard, Stage,
EvidenceTable, CodeBlock, Timeline, Footer);
`make generate` is a build step and is byte-stable
2.3 navigation six-area rail, <details> disclosure under 900px,
breadcrumbs, aria-current, L1/L2/L3 with artefact pages
2.4 stage panel internal/stage owns the contract; RouteDriver (fragment
swap) and SSEDriver (event stream) differ only in the
Binding, never in layout. fragment ids live in the
contract so route files, links and streamed frames agree
2.5 search SQLite FTS5 over titles and body, rebuilt by the build;
one handler returns a page without HTMX, a fragment with
it. Visitors' FTS operators are never honoured
2.6 quality floor print stylesheet, JS-off navigation, focus states,
semantic landmarks, dark default; security headers
Definition of Done evidence (see issue AUD-142):
A1 all pages 200 in a network namespace with no external route
one binary serves the site; make dist writes 45 pages, 86 fragments, 40 docs
templ generate is deterministic (md5 identical across runs)
6 L1 + 6 L2 pages, artefact pages reachable in <= 3 clicks
FTS5 returns sensible hits for zigbee / signage / routing (tested)
Lighthouse 100 performance and 100 accessibility, mobile and desktop
first byte well inside 100ms over loopback (test)
panel renders from a route driver and re-points at the stream untouched
print legible: /edge/ renders to a 3-page PDF with the print stylesheet
Also fixes found while finishing the build:
- reject "." / ".." path segments before ServeMux redirects them into a file
outside the asset directory (/static/../build.json now 404, was 307->200)
- make test is the real gate: go test ./... plus the validator self-test
This commit is contained in:
155
cmd/server/main.go
Normal file
155
cmd/server/main.go
Normal file
@@ -0,0 +1,155 @@
|
||||
// Command server serves the site the build wrote.
|
||||
//
|
||||
// It reads a directory produced by cmd/build and serves it on loopback. It does
|
||||
// not read content/, does not need Qdrant, OmniRoute or any other service, and
|
||||
// makes no outbound connection, so every page it serves works with the AI stack
|
||||
// stopped (acceptance test A1).
|
||||
//
|
||||
// Configuration comes from the environment so the same binary works under
|
||||
// `make dev`, systemd and compose:
|
||||
//
|
||||
// RESUME_ADDR listen address (default 127.0.0.1:8096)
|
||||
// RESUME_DIST build directory (default "dist")
|
||||
// RESUME_STREAM_DELAY pacing for the streamed panel, e.g. 350ms
|
||||
//
|
||||
// Flags with the same names override the environment for local work.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"gitea.lab.audasmedia.com.au/sam/ai-resume/internal/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr := flag.String("addr", env("RESUME_ADDR", server.DefaultAddr), "listen address")
|
||||
dist := flag.String("dist", env("RESUME_DIST", server.DefaultDist), "build directory")
|
||||
streamDelay := flag.Duration("stream-delay", envDuration("RESUME_STREAM_DELAY", server.DefaultStreamDelay), "delay between streamed panel frames")
|
||||
healthCheck := flag.Bool("health-check", false, "ask a running server for /healthz and exit 0 only if static is ready")
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "usage: server [--addr HOST:PORT] [--dist DIR] [--stream-delay DURATION] [--health-check]\n")
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "environment: RESUME_ADDR, RESUME_DIST, RESUME_STREAM_DELAY\n")
|
||||
}
|
||||
flag.Parse()
|
||||
|
||||
// The container image has no shell and no curl, so the health probe is this
|
||||
// binary talking to itself (deploy/compose.yml healthcheck).
|
||||
if *healthCheck {
|
||||
if err := probe(*addr); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "health check failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("static ready")
|
||||
return
|
||||
}
|
||||
|
||||
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
|
||||
|
||||
site, err := server.New(server.Config{
|
||||
Addr: *addr,
|
||||
DistDir: *dist,
|
||||
StreamDelay: *streamDelay,
|
||||
Logger: log,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error("start failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer site.Close()
|
||||
|
||||
// No WriteTimeout on purpose: the stage stream keeps the response open for
|
||||
// as long as it is producing frames. Reads are bounded.
|
||||
srv := &http.Server{
|
||||
Addr: *addr,
|
||||
Handler: site.Handler(),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
MaxHeaderBytes: 64 << 10,
|
||||
ErrorLog: slog.NewLogLogger(log.Handler(), slog.LevelWarn),
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
log.Info("shutting down")
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
log.Warn("shutdown", "error", err)
|
||||
}
|
||||
}()
|
||||
|
||||
log.Info("listening", "addr", *addr, "dist", *dist)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Error("serve failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// probe asks a running server whether the static site is ready. Only the static
|
||||
// field decides the exit code: Mode A must report ready with every AI service
|
||||
// stopped (acceptance test A1).
|
||||
func probe(addr string) error {
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if host == "" || host == "0.0.0.0" || host == "::" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
client := &http.Client{Timeout: 3 * time.Second}
|
||||
res, err := client.Get("http://" + net.JoinHostPort(host, port) + "/healthz")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("/healthz returned %s", res.Status)
|
||||
}
|
||||
var body struct {
|
||||
Static struct {
|
||||
Ready bool `json:"ready"`
|
||||
} `json:"static"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(res.Body, 64<<10)).Decode(&body); err != nil {
|
||||
return fmt.Errorf("decode /healthz: %w", err)
|
||||
}
|
||||
if !body.Static.Ready {
|
||||
return errors.New("static is not ready")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func env(key, fallback string) string {
|
||||
if v, ok := os.LookupEnv(key); ok && v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func envDuration(key string, fallback time.Duration) time.Duration {
|
||||
v, ok := os.LookupEnv(key)
|
||||
if !ok || v == "" {
|
||||
return fallback
|
||||
}
|
||||
d, err := time.ParseDuration(v)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s=%q is not a duration: %v\n", key, v, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
return d
|
||||
}
|
||||
Reference in New Issue
Block a user