Routing, the request context, middleware, validation, sessions, CSRF and error handling. Decisions A1, A3, A4, H1–H3 in brainstorming.md.
A thin layer over http.ServeMux (Go 1.22+ patterns). The router collects route definitions, compiles groups/prefixes into ServeMux patterns at boot, and keeps a name → pattern table.
// routes/web.go
func Web(r *router.Router, app *vukem.App) {
home := controllers.NewHome(app)
posts := controllers.NewPost(app)
r.Get("/", home.Index).Name("home")
r.Group("/admin", func(g *router.Group) {
g.Use(auth.Required())
g.Resource("posts", posts) // posts.index, posts.store, posts.show…
}).Name("admin.")
r.Handle("GET /metrics", promhttp.Handler()) // any http.Handler
}| Feature | API |
|---|---|
| Verbs | Get, Post, Put, Patch, Delete, Options, Match([]string, …), Any |
| Params | {id}, {path...}, {$} (ServeMux syntax); c.Param("id"), c.ParamInt("id") |
| Names | .Name("posts.show"); resources auto-named <name>.<action> |
| URL generation | r.URL("posts.show", "id", 42) → /posts/42; missing/extra params return an error; templates: {{ route "posts.show" "id" .ID }} |
| Groups | Group(prefix, fn) with .Use(mw...), .Name(prefix), nesting; Host(host, fn) restricts a group to one host (ServeMux host pattern, port ignored; URL returns //host/path; RouteInfo.Host; invalid host = compile error); .Only() on a host group makes it the only thing the host serves (a host/ catch-all answers 404/405 through the group's error handler, without router Use middleware) |
| Resources | Resource(name, ctrl), APIResource(name, ctrl) (no create/edit), .Only(...), .Except(...) |
| Listing | vukem route:list prints method, pattern, name, handler, middleware |
Conflicting patterns panic at boot (ServeMux behaviour) and the router wraps the panic into an error naming both routes. A duplicate route name is a build error too, naming both routes with their methods and paths.
Every route the framework registers itself is named under the vukem. prefix (vukem.health for GET /up), so an app can use any name it likes. It used to take the bare name health, and an app registering its own GET /health under that obvious name made the router refuse to boot — with the error pointing at the app's route, the one that looked wrong, and nothing in the docs or the type system saying the name was taken. Do not prefix your own names with vukem. and the two sets can never meet.
| Probe | Where | What it touches |
|---|---|---|
| Liveness | GET /up (vukem.health) |
the process only; answers {"status":"ok"} with Cache-Control: no-store, plus "version" when the app sets vukem.WithVersion (also in the readiness body; off by default so the probe does not fingerprint the build) |
| Readiness | GET /up?checks=1, or app.ReadyHandler() mounted on a path of your own |
every registered health check, in parallel, each bounded by its timeout; probes that arrive while a check is running share that run, and once a probe has abandoned a hung check at its timeout, later probes fail at once instead of starting another goroutine |
Register checks with vukem.WithHealthCheck(name, check) at build time, or app.AddHealthCheck(name, check) / app.AddHealthCheckTimeout(name, timeout, check) from a provider. app.CheckHealth(ctx) runs them and returns each error by name.
Three things decide whether this helps or hurts:
- Point liveness at
/upand nothing else. A liveness probe that runs the checks turns one slow dependency into a restart loop, and a load balancer pointed at them turns a degraded service into zero healthy instances. - Make each check prove the dependency does what the app needs. The database provider registers a
Ping, and a pool hands out a connection to a database whose migrations never ran — the ping passes while no query the app makes can succeed. Add a check that reads a table the app owns (SELECT count(*) FROM <table> WHERE 1 = 0is cheap and index-free, and still fails when the table is absent). For a disk usestorage.Check, because an existence check passes with credentials that cannot write. - The errors stay server-side.
ReadyHandleranswers only"ok"or"fail"per check and logs the detail, because a driver error carries hosts and sometimes credentials.CheckHealthreturns them for the log, never for a response body.
A check that hangs is abandoned at its timeout rather than accumulating goroutines, and while an abandoned run is still going the check fails immediately instead of starting another.
Resource registers only the actions the controller implements, detected through one-method interfaces:
| Interface | Method | Route |
|---|---|---|
Indexer |
Index(c *Ctx) error |
GET /posts |
Creator |
Create |
GET /posts/create |
Storer |
Store |
POST /posts |
Shower |
Show |
GET /posts/{id} |
Editor |
Edit |
GET /posts/{id}/edit |
Updater |
Update |
PUT/PATCH /posts/{id} |
Destroyer |
Destroy |
DELETE /posts/{id} |
HTML forms can use _method (hidden field) for PUT/PATCH/DELETE; the method-override middleware applies only to POST with form content types. Multipart forms put it in the action's query string (action="/posts/1?_method=PUT"), so the body is never read before the handler streams it (uploads).
type HandlerFunc func(c *Ctx) error| Area | Methods |
|---|---|
| Request | Request(), Context(), Param, Query, Header, Cookie, IP(), IsHTTPS(), IsJSON(), WantsJSON() |
| Binding | web.Bind[T](c) (T, error) (a package function, web/parse.go): JSON, form, multipart, query; runs validation; ignores unknown keys (use a .Strict() schema with web.Parse, or BindJSON, to reject them); BindJSON, FormFile(name) |
| Responses | Status, String, HTML, JSON, Created, Accepted, NoContent, Blob, Problem, Redirect, RedirectRoute, Back, Stream, File, Download |
| Conditional | NotModified(etag, lastModified), CheckIfMatch(current) |
| Views | Render(name, data), Fragment(name, block, data) (HTMX), View(v view.View) (templ) |
| Session | Session(), Flash(key, msg), Old(field), Errors() |
| Auth (ALPHA) | Authorize(ability, subject); the current user comes from auth.User[U](c, guard), a package function |
| HTMX | HTMX() returns request/response helpers (frontend.md) |
*web.Ctx wraps http.ResponseWriter and *http.Request; c.Writer() and c.Request() give raw access. A *web.Ctx must not escape its handler goroutine.
JSON bodies go through encoding/json/v2. A number inside a generic map stays a json.Number. A duplicate object name or invalid UTF-8 in a string is rejected.
Adapters: router.Adapt(HandlerFunc) http.Handler and router.Wrap(http.Handler) HandlerFunc.
Signature: func(http.Handler) http.Handler. Three stacks:
| Stack | Runs | Defaults |
|---|---|---|
| Server | every request, even unmatched | Recover, RequestID, Logger (slog), Static |
| Router | only when a route matches | MethodOverride, Session, CSRF, Flash, Auth init (ALPHA) |
| Named | per route/group, may take options | auth.Required(guards...), auth.Guest(), ratelimit.Limit(name), Timeout(d), CORS(cfg) |
Where each call reaches:
| Call | Reaches | Position in the chain |
|---|---|---|
r.UseServer(mw) |
every request, including ones that match no route (404, 405) | outermost |
r.Use(mw) |
every matched route, whether it was registered before or after the call | after the server stack |
g.Use(mw) |
the routes of the group and of its nested groups, whether registered before or after the call | after router middleware, outer groups first |
route.Use(mw) |
that route only | innermost, right before the handler |
Chains are built when Router.Handler() compiles the routes, so the order of Use calls relative to route registration does not matter; the order of the arguments does. TestDefaultMiddlewareOrder and TestMiddlewareScope pin this contract; Router.ServerMiddleware() lists the server stack by name.
middleware.LoopbackOnly(port) guards a local-only server (a development tool, a local mode without login): loopback peer, no proxy headers, exact Host and Origin on the bound port, else 403. *.localhost names are refused on purpose.
middleware.Gzip passes Flush through, also before the first write (it sends the headers, as a first write would), so streaming handlers work behind it.
vukem make:middleware generates the function and registers it as named middleware in bootstrap/middleware.go.
vukem.DefaultMiddleware(app, r) installs the default stacks; vukem.DefaultMiddlewareWith(app, r, vukem.MiddlewareOptions{...}) adjusts them without rebuilding the stack by hand: SecureHeaders edits the SecureHeadersConfig (CSPSources, ReportURI; validated at boot), Stateless drops sessions (CSRF then relies on Origin/Sec-Fetch-Site only; csrfToken renders empty), and SessionBypass / CSRFBypass take ServeMux patterns (a trailing / covers the subtree) served without sessions or CSRF checks (CSRF bypasses are logged at boot), MaxBody sets the router-wide body limit (default 10 MiB; negative is a boot error), and I18n configures the locale resolver (*i18n.MiddlewareConfig: sources, order, selectable locales; validated at boot, needs WithTranslations). The zero options equal DefaultMiddleware. vukem.APIMiddlewareWith(app, r, vukem.APIMiddlewareOptions{SecureHeaders: ..., I18n: ...}) is the same for APIMiddleware (validated at boot; no session or CSRF options since that stack has neither). With WithTranslations, both stacks install the locale resolver; the API stack reads only ?lang= and Accept-Language by default, so its 422 messages and Content-Language follow the request.
Validation is done by the z package — Zod-style typed schemas. Full reference: validation.md.
in, err := web.Parse(c, requests.StorePostSchema) // typed requests.StorePost or 422Failure behaviour (centralised in the error handler):
| Request | Response |
|---|---|
| HTML form | 303 back, errors + old input flashed |
| HTMX | 422 re-rendering the form fragment (htmx 4 swaps 4xx) |
| Inertia (BETA) | 303 back; errors shared as props.errors |
| JSON / API | 422 application/problem+json with an errors map and an issues list (path, code, key, params, on: body, query or params) |
Own session/ package.
| Store | Package | Phase |
|---|---|---|
Cookie (AES-256-GCM, key via HKDF from APP_KEY, 4 KB limit) |
core | Prototype |
| Memory (tests/dev) | core | Prototype |
Database (sessions table, SQLite/Postgres) |
core | Prototype |
| Redis | drivers/redis |
BETA |
Features: Get/Put/Forget/Pull, Flash (next request), Reflash, old input, Regenerate() (new id, keep data — call on login), Invalidate(), idle and absolute timeouts, session.CreatedAt (creation time, kept by login), lazy loading (no store hit unless used), write only when dirty.
http.CrossOriginProtection(Go 1.25) on the router stack: rejects cross-origin unsafe requests usingSec-Fetch-Site/Origin.- Session cookie
SameSite=Lax. - Trusted origins and bypass patterns (webhooks) configured in
config/http.go. - Token fallback (
{{ csrfField }},X-CSRF-Token,hx-headers:inherited) kept available for non-browser clients and old browsers; enabled by config.
var ErrNotFound = errs.New(http.StatusNotFound, "not found")
type HTTPError struct { Status int; Message string; Err error; Headers http.Header; Key string; Params map[string]any }
type ValidationError struct { Fields map[string][]string }-
errs.NewKey(status, key, fallback, params)gives an*HTTPErrora message key: the error handler showsHTTPError.LocalizedText(ctx), translated by the request'serrs.Translator(installed byi18n.Middleware, see i18n), withMessageas the English fallback.errs.Translate(ctx, key, fallback, params)does the same for any message built with a context. -
NewValidation().Add(field, msg)returns the*ValidationErrorfor chaining (return errs.NewValidation().Add(...));Append(field, msg)has no result, for statements that errcheck would otherwise flag. -
Handlers return errors;
web.DefaultErrorHandler(orServices.Errors, aweb.ErrorHandler, when set) maps them:*ValidationError→ see the validation table above.*HTTPError/ sentinels → status page (resources/views/errors/<status>.html, which receives.Noncefor a nonce'd<style>) or problem+json. The built-in status pages have no inline style attributes and nonce their<style>, so they render under the default CSP.orm.ErrNotFound(an*errs.HTTPError, returned byFirst/Findfor no row) → 404.- anything else → 500, logged with request id; debug page in development.
-
Content negotiation:
Accept: application/jsonor/apiroutes → RFC 9457 problem details (4xx*HTTPErrorextension members frome.WithExtension(key, value)/HTTPError.Extensionsare added next to the standard members, never replacing one;e.WithType(uri)/e.WithTitle(title)settypeandtitleof a 4xx; a 5xx never carries them, and carries extension members,type,titleand its message asdetailonly when the app built the error withe.WithExtensionsOn5xx()(a 503codeandretryable; the wrapped cause stays hidden); document the members in the OpenAPIProblemcomponent withopenapi.Info.ProblemExtensions;Services.ProblemDecorator func(*web.Ctx, *web.Problem)edits every problem, including router 404/405 and typed-route 400s, right before it is written); HTMX → fragment/status;Services.ErrorPage(aweb.ErrorPageFunctaking a client-safeweb.ErrorPage{Status, Title, Detail, Errors}; Inertia requests withvukem.WithInertia: theErrorpage component); otherwise HTML. -
Custom handling per error type: set
Services.Errorsto aweb.ErrorHandler, match your types witherrors.AsType[*MyErr](err), and callweb.DefaultErrorHandler(c, err)for the rest. -
Per group:
g.ErrorHandler(func(next web.ErrorHandler) web.ErrorHandler)replaces the handler for the group's routes and nested groups, for errors returned by handlers and for middleware failures (web.Fail);nextis the outer group's, the router's or the default handler. Unmatched paths (404/405) keep the router's handler. Route handlers read it from a private context key, not from the services in the context, soauth.ProblemJSONstill only affects middleware errors (TestGroupErrorHandler). -
Panics are recovered by
middleware.Recoverand converted to 500. -
A 5xx that wraps a
ValidationErroranswers as a plain server error: its field errors are never sent, and a form post is not redirected back with them (TestSecurity_A10_ServerErrorHidesWrappedValidation).
errs.Status(err) reads the status of an *errs.HTTPError, a *errs.ValidationError (422) or any error implementing errs.StatusCoder (HTTPStatus() int), for packages that should not import errs. Implementations in the framework: orm conflicts (409, orm.ErrConflict), z.Issues and z field errors (422), export when the client went away (499), and in drivers/mongo mongo.ErrNotFound (404) and mongo.ErrDuplicate (409). Your own type gets a status the same way:
type quotaError struct{ plan string }
func (e quotaError) Error() string { return "quota exceeded on plan " + e.plan }
func (quotaError) HTTPStatus() int { return http.StatusPaymentRequired }The status only picks the response code and template: a 5xx is still rendered without its message (docs/security.md, A10).
web.MapError(s, fn) registers, once and in Services.ErrorMappers, how errors of a type are rendered: fn(c, e) receives the first E in the chain (errors.AsType, so a wrapped one is found) and returns the error to render instead, usually an *errs.HTTPError. That error goes through web.DefaultErrorHandler, so format negotiation, headers and masking are unchanged:
web.MapError(app.Router.Services(), func(c *web.Ctx, e *billing.InsufficientFunds) error {
return errs.New(http.StatusPaymentRequired, "Not enough balance.").WithExtension("balance", e.Balance)
})The first registered mapper that matches wins (no chaining); a nil result leaves the error alone; the original error is the one logged; a mapped 5xx is masked like any other (TestSecurity_A10_MappedErrorCannotExposeServerFaults); web.Fail errors are mapped too. Register mappers while building the app: the slice is read on every request and is not locked. web.MapErrorIf(s, match, fn) is the same for a condition no type tells apart (a sentinel, a request property). With no mapper matching, DefaultErrorHandler answers context.Canceled with the request context finished (the client went away) as 499 with no body, logged at Info; a context error on a live request stays 500 and context.DeadlineExceeded is never 499 (TestDefaultErrorHandlerClientGone). A mapper you register wins over this.
A problem's type is about:blank unless the app sets APP_PROBLEM_BASE_URL, which must be an http or https URL without credentials, query or fragment (the boot refuses anything else; a trailing slash is dropped). With it, a problem that has no type of its own gets <base>/<status slug> (/not-found, /internal-server-error; only a-z, 0-9 and dashes survive, so 418 is /im-a-teapot), and validation problems get <base>/validation. HTTPError.WithType and Services.ProblemDecorator always win. The debug page shows the same type in "What clients see in production".
With APP_DEBUG=true outside production (Services.Debug), 5xx errors in a browser render a youch-style page (decision F7): the error message and root type, the Unwrap chain, the stack with source snippets (app frames open, framework and standard library frames collapsed), the request (method, URL, route, request id, headers with Authorization, Cookie and token headers redacted) and the Go version. The page carries the CSP nonce. JSON clients keep getting problem+json (with the error text as detail), htmx gets the fragment. In vukem serve the same report is printed in color on stderr (Services.DebugOut).
Stacks come from errs.New, errs.Wrap, errs.WithStack(err) (for errors from other packages) and recovered panics; errs.Stack(err) returns the one closest to the origin. Capturing is cheap and happens only on error paths; sentinel errors declared as package variables carry no stack.
public/served bymiddleware.Staticwithhttp.ServeFileFSover anos.Root(a file namedindex.htmlis served like any other; directories are left to the next handler);/build/and fingerprinted names (a hex or Vite-style hash segment, not any long word) getCache-Control: public, max-age=31536000, immutable; other files revalidate throughLast-Modified, or a content-hashETagfor embedded files.- Request bodies limited by
http.MaxBytesReader(configurable per route: a route'smiddleware.BodyLimitorupload.Rules.MaxSizereplaces the router-wide 10 MiB default, raising or lowering it; the limit is checked when the body is read, against the innermost limit;web.PeekJSONputs the bytes it read back, so aLimitBodyapplied after it still sees the whole body and its limit still applies). The router limit is applied after the server stack, so aUseServer(middleware.BodyLimit(n))is replaced on routes by the router default (innermost wins): set the app-wide limit withMiddlewareOptions.MaxBodyinstead. Urlencoded forms are parsed byMethodOverridebefore routing, so their cap has to be a server-levelBodyLimitplaced ahead of it; a route-level limit never applies to them, and a hit limit is remembered soweb.Parseanswers 413 (TestSecurity_A06_MethodOverrideRemembersMaxBytes). A handler that reads the form withr.FormValuesees an empty form for such a body;web.BodyError(r)returns the remembered error (errs.ErrRequestTooLarge) to answer 413 instead. - Uploads stored through
storage/(ALPHA) usingos.Rootto prevent path traversal; filenames randomised by default. Streaming, resumable and direct uploads: subsystems.md#uploads.
The app server speaks HTTP/1.1 and, outside production (or with HTTP_H2C=true), HTTP/2 cleartext with prior knowledge. With TLS_CERT_FILE and TLS_KEY_FILE (PEM), or vukem.WithTLSConfig(*tls.Config) (for autocert or a custom GetCertificate), it serves HTTPS on PORT instead: ALPN h2 and http/1.1 through stdlib http.Protocols, TLS 1.2 minimum (a lower MinVersion is a boot error), Go's default cipher suites. The file pair is checked at most once a second on handshakes and reloaded when a modification time moves; a pair that fails to load keeps the previous certificate and logs an error. HTTP_H2C with TLS is a boot error. There is no plaintext listener unless TLS_REDIRECT_ADDR (e.g. :80) is set: it redirects every request with 308 to the https APP_URL (never to the request's Host). HSTS stays with the security headers middleware. Site: HTTP/2 and HTTP/3.
contrib/http3 (own module, github.com/quic-go/quic-go v0.64.1 pinned exactly; the stdlib has no HTTP/3 server and golang.org/x/net/quic is not yet covered by the Go security policy, checked 2026-10-29) adds an http3 runner: http3.Serve(app, http3.Options{}) after the routes. It listens on UDP at the app's HOST:PORT (same port as HTTPS), serves the app's compiled handler with the TLS_CERT_FILE/TLS_KEY_FILE pair (reloaded on change, like the core) or Options.TLS, and adds Alt-Svc: h3=":<port>" to HTTP/1.1 and HTTP/2 responses only when it runs. Limits: MaxStreams (100 concurrent streams per connection), IdleTimeout (vukem.DefaultIdleTimeout), MaxHeaderBytes (HTTP_MAX_HEADER_BYTES); shutdown drains within SHUTDOWN_TIMEOUT. No certificate is http3.ErrNoTLS. QUIC is TLS 1.3 only (TestSecurity_A02_HTTP3OnlyTLS13).