The default locale stays at the root; every other language is the same key under
/{lang}/ (ADR-0009). /en/… is never live and redirects to the root form so the URL
space cannot fork. Lookup now takes a language and reports which one it served,
following requested → default → any rather than 404ing when a translation is
missing.
That is the second routing case, so the resolver is extracted to resolve.go and
the mux keeps one entry: URL shape is the resolver's business. A leading segment
counts as a language only when some bundle is written in it, so an unknown prefix
is a 404 rather than a stripped path — and a section may not be named after a
language in use, now recorded in content-model.md.
Because the served variant can differ from the URL requested, Page gained
.Canonical (the variant actually served) and .Alternates for hreflang. A theme
must never build a path, so both come from the engine.
Evidence: /bn/pages/about/ serves the Bengali body with lang="bn" and canonical
/bn/pages/about/; /bn/posts/hello-world/ falls back to English with canonical
/posts/hello-world/; /en/pages/about/ 301s to /pages/about/; /fr/… is 404.
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
// Package web maps requests to bundles and writes bytes. It knows content and render, and exposes
|
|
// neither to them.
|
|
package web
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"khosra/internal/content"
|
|
"khosra/internal/render"
|
|
)
|
|
|
|
// Handler serves a site.
|
|
//
|
|
// One mux entry, because URL shape is the resolver's business rather than the mux's: see resolve.
|
|
func Handler(site *content.Site, r *render.Renderer) http.Handler {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("GET /", func(w http.ResponseWriter, req *http.Request) {
|
|
serve(w, req, site, r)
|
|
})
|
|
return mux
|
|
}
|
|
|
|
// serve resolves one request and writes its bundle.
|
|
func serve(w http.ResponseWriter, req *http.Request, site *content.Site, r *render.Renderer) {
|
|
res, ok := resolve(req.URL.Path, site)
|
|
if !ok {
|
|
http.NotFound(w, req)
|
|
return
|
|
}
|
|
// A redirect target only exists for a path that resolves, so check the bundle before sending one:
|
|
// otherwise a nonexistent page answers 301 and confirms nothing.
|
|
b, served, found := site.Lookup(res.key, res.lang)
|
|
if res.redirect != "" && (found || res.key == "") {
|
|
http.Redirect(w, req, res.redirect, http.StatusMovedPermanently)
|
|
return
|
|
}
|
|
if !found {
|
|
http.NotFound(w, req)
|
|
return
|
|
}
|
|
out, err := r.Bundle(b, served, site.Variants(res.key))
|
|
if err != nil {
|
|
// A render failure degrades: log it and say nothing more to the client than that it failed
|
|
// (conventions.md). It must never leak a template or filesystem detail.
|
|
slog.Error("render failed", "key", b.Key, "err", err)
|
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
if _, err := w.Write(out); err != nil {
|
|
slog.Warn("write failed", "key", b.Key, "err", err)
|
|
}
|
|
}
|