GOTTH+
GitHub

Building

Request lifecycle

The middleware chain every request passes through, and the layers a handler uses.

Middleware order

buildHandler in internal/app/app.go wraps the router. Outermost runs first, so cheap rejections happen before any handler work:

  • Request ID: reads or creates X-Request-ID and puts it on every log line.
  • Security headers: Content-Security-Policy, nosniff, Referrer-Policy, Permissions-Policy, Cross-Origin-Opener-Policy, and HSTS in production.
  • Compression: gzip and Brotli.
  • Rate limit: per client address, with a separate bucket for mutations.
  • Cross-origin protection: Go's http.CrossOriginProtection, using Sec-Fetch-Site and Origin.
  • Body limit: 1 MiB on every request.
  • CSRF: issues the token cookie and checks the header on every non-safe method.
  • With sign-in: the session and the signed-in user (auth.Middleware), skipped for /static/.

Layers

Dependencies point inward: model, then repository, service, and dto, then factory and handler, then view. A handler parses the request, validates it, calls a service or repository, and renders a fragment or a page. It contains no SQL and no business rules.

  • Repositories are interfaces defined next to their implementation, so handlers can be tested with fakes.
  • Services contain rules and have no HTTP and no SQL.
  • Factories are closures over their dependencies, so routes are wired in one place.

Errors

Clients never see internal errors. The handler logs the detail with its request ID and sends a generic message. For htmx requests that is a toast (respondWithToast) with HX-Reswap: none, which leaves the page as it was. Full page loads get the error page.

Was this page helpful?

Be the first to answer.