Item 0 of the roadmap's order of work, and it blocked everything after it: core
sat at 2965 of 3000 while the review scheduled four core-bound items, the first
of which — logging — wanted the whole remainder.
/robots.txt and /sitemap.xml are exact paths somebody else's software asks for by
name. They own no core concept and pass every test the architecture applies to a
feature; they lived in internal/web only because a feature could not own a route
until ADR-0081. internal/ext/discover/ now holds them. Core 2965 → 2913.
The seam gained one parameter to make it possible: a func() *content.Site, since a
sitemap must list what is served now and the index is swapped whole on every
rebuild (ADR-0077). A captured pointer would have frozen the site at startup —
which is the kind of bug that only shows up after a rebuild, in production.
The ceiling rises to 3400 as well as the move, because the move alone could not buy
the room. feed.go and web/extras.go cannot follow discover out: a feed lives at
/{section}/feed.xml and extras under a bundle's own URL, so both are resolver cases
while the seam mounts exact paths only. Raising by the minimum that unblocks one
item produces a ceiling nobody believes, so 3400 fits the View cluster with
headroom. HARNESS.md asks that a raise be read as evidence something belongs in
ext before evidence the number was small; both readings were true, so both actions
were taken.
web no longer reserves those two paths, so a clash between features is wire.go's:
it merges route maps in declaration order, keeps the earlier claim, logs the loser.
Verified — a site shipping root/robots.txt starts, serves the engine's robots.txt,
and logs the passthrough claim, where an unguarded mux.Handle would have panicked.
Evidence: robots.txt and sitemap.xml are byte-identical before and after the move
against the demo site (67 and 2701 bytes, cmp clean), and the sitemap keeps its
application/xml type.
One real cost, recorded in both places rather than hidden. internal/web's
visibility test asserted that a listing, a feed *and* a sitemap all hide
unpublished bundles — one property, one test, because all three share a Query. The
sitemap half moved to the feature instead of a web test importing ext, which would
invert the one-way layering the architecture gate enforces. That property is now
asserted twice, once per package owning a surface.
Three gates caught real mistakes on the way: the staged-tree check found a partial
stage where git rm had staged a deletion while the caller edits were unstaged, the
coupling gates demanded state.md and HARNESS.md, and the nesting advisory rejected
a closure that put the merge loop one level too deep — fixed by making it a plain
function rather than tolerated.
Extensions 6 → 7. Routing cases unmoved: exact paths are mux entries, never
resolver cases, which is what that counter's exclusion column already said.
13 files. Core 2913/3400, ext 2495/3500.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
99 lines
3.9 KiB
Go
99 lines
3.9 KiB
Go
package discover
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"log/slog"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"khosra/internal/content"
|
|
)
|
|
|
|
// The two paths, exported so the wiring can reason about precedence without restating strings.
|
|
const (
|
|
RobotsPath = "/robots.txt"
|
|
SitemapPath = "/sitemap.xml"
|
|
)
|
|
|
|
// Routes returns the two exact paths this feature owns.
|
|
//
|
|
// site is a callback rather than a value because a sitemap must list what is served *now*: the index is
|
|
// swapped whole on every rebuild (ADR-0077), and a captured pointer would serve the site as it was at
|
|
// startup. settings is copied, since editing `site.yaml` needs a restart anyway (ADR-0055).
|
|
func Routes(siteFS fs.FS, settings content.Settings, site func() *content.Site) map[string]http.Handler {
|
|
return map[string]http.Handler{
|
|
RobotsPath: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
robots(w, siteFS, settings.Base)
|
|
}),
|
|
SitemapPath: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
sitemap(w, req, site(), settings.Base)
|
|
}),
|
|
}
|
|
}
|
|
|
|
// robots answers /robots.txt, preferring the site's own file.
|
|
//
|
|
// A site that ships robots.txt has said something deliberate, so it is served verbatim; otherwise the engine
|
|
// emits the minimum that is true — everything is public, and here is the sitemap. The Sitemap line only
|
|
// appears with a declared base, because a relative sitemap reference is not something a crawler accepts.
|
|
func robots(w http.ResponseWriter, siteFS fs.FS, base string) {
|
|
if siteFS != nil {
|
|
if data, err := fs.ReadFile(siteFS, "robots.txt"); err == nil {
|
|
writeAs(w, "text/plain; charset=utf-8", data, "robots.txt")
|
|
return
|
|
}
|
|
}
|
|
var out strings.Builder
|
|
out.WriteString("User-agent: *\nDisallow:\n")
|
|
if base != "" {
|
|
fmt.Fprintf(&out, "Sitemap: %s\n", content.Absolute(base, SitemapPath))
|
|
}
|
|
writeAs(w, "text/plain; charset=utf-8", []byte(out.String()), "robots.txt")
|
|
}
|
|
|
|
// sitemap answers /sitemap.xml with every bundle in every language it exists in.
|
|
//
|
|
// It needs a declared base: the sitemap format has no room for a relative URL, so without one the honest
|
|
// answer is that this file does not exist rather than a file full of paths no crawler can use (ADR-0039).
|
|
// Every URL comes from content.URL, like every other path the engine emits, so a sitemap can never disagree
|
|
// with what is actually served.
|
|
func sitemap(w http.ResponseWriter, req *http.Request, site *content.Site, base string) {
|
|
if base == "" {
|
|
slog.Warn("no sitemap: the site declares no base URL", "file", content.SettingsFile)
|
|
http.NotFound(w, req)
|
|
return
|
|
}
|
|
var out strings.Builder
|
|
out.WriteString(`<?xml version="1.0" encoding="utf-8"?>` + "\n")
|
|
out.WriteString(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` + "\n")
|
|
for _, entry := range site.Everything() {
|
|
fmt.Fprintf(&out, "<url><loc>%s</loc>", xmlEscape(content.Absolute(base, content.URL(entry.Route, entry.Lang))))
|
|
if !entry.Date.IsZero() {
|
|
fmt.Fprintf(&out, "<lastmod>%s</lastmod>", entry.Date.Format("2006-01-02"))
|
|
}
|
|
out.WriteString("</url>\n")
|
|
}
|
|
out.WriteString("</urlset>\n")
|
|
writeAs(w, "application/xml; charset=utf-8", []byte(out.String()), "sitemap.xml")
|
|
}
|
|
|
|
// xmlEscape escapes the five characters XML reserves. A URL should contain none of them, and a sitemap that
|
|
// silently breaks on the one that does is worse than a slightly paranoid replacement.
|
|
func xmlEscape(s string) string {
|
|
return strings.NewReplacer(
|
|
"&", "&", "<", "<", ">", ">", `"`, """, "'", "'",
|
|
).Replace(s)
|
|
}
|
|
|
|
// writeAs sets the type and writes, logging a failed write rather than pretending it succeeded.
|
|
//
|
|
// Five lines copied from internal/web rather than shared: a feature may not import web (conventions.md), and
|
|
// two copies of five obvious lines is cheaper than a package existing to hold them.
|
|
func writeAs(w http.ResponseWriter, contentType string, out []byte, what string) {
|
|
w.Header().Set("Content-Type", contentType)
|
|
if _, err := w.Write(out); err != nil {
|
|
slog.Warn("write failed", "what", what, "err", err)
|
|
}
|
|
}
|