All examples

Go HTTP service

Standard-library HTTP service with method-pattern routing, pgx and sqlc, slog logging, and table-driven tests.

Composed from the go-http-service rules pack. Same file from the CLI: aibysitter init --packs go-http-service --format agents

Written for this site, CC0, scored by this linter, not tested against any agent.

File
AGENTS.md, 40 lines
Format
AGENTS.md
Save as
AGENTS.md
Category
Go
Tags
License
CC0 1.0, public domain. No attribution needed.

Download AGENTS.md Lint a copy

Aibysitter lint score

100 / 100 — A

Ruleset v4

No deductions.

Badge

Aibysitter lint score: A 100

Markdown for a README:

[![Aibysitter lint score: A 100](https://aibysitting.net/gallery/go-http-service/badge.svg)](https://aibysitting.net/Gallery/go-http-service)

AGENTS.md

View rendered

  1. # Go HTTP service
  2. Go HTTP service. Standard library `net/http` routing, PostgreSQL through `pgx`, queries generated by `sqlc`.
  3. ## Commands
  4. - Build: `go build ./...`
  5. - Test: `go test -race ./...`
  6. - Vet: `go vet ./...`
  7. - Lint: `golangci-lint run`
  8. - Format check: `gofmt -l .` prints nothing
  9. - Regenerate queries after editing `db/queries/*.sql`: `sqlc generate`
  10. ## Layout
  11. - `cmd/<app>/main.go`: wiring only. Config, logger, server start.
  12. - `internal/http/`: handlers and routes.
  13. - `internal/<domain>/`: domain logic. No `net/http` imports.
  14. - `internal/db/`: generated sqlc code. Files in this directory are not edited by hand.
  15. ## Handlers
  16. - Routes are registered with method patterns: `mux.HandleFunc("POST /messages", h.createMessage)`.
  17. - JSON bodies are decoded with `json.NewDecoder(r.Body)` and `DisallowUnknownFields()`.
  18. - Errors are written with `writeError(w, status, code, message)` from `internal/http/errors.go`.
  19. - Handlers get their dependencies from the `Handler` struct. No package-level state.
  20. ## Errors and context
  21. - Wrap errors with context: `fmt.Errorf("load account: %w", err)`. Compare them with `errors.Is` and `errors.As`.
  22. - Every function that does I/O takes `ctx context.Context` as its first parameter.
  23. - Request paths do not call `context.Background()`.
  24. ## Logging
  25. - `log/slog` with JSON output. Keys are snake_case: `slog.Info("message sent", "message_id", id)`.
  26. - Message bodies and personal data are never logged.
  27. ## Tests
  28. - Table-driven tests with one `t.Run` per case.
  29. - Handlers are tested with `httptest.NewRecorder` and fakes for external clients.
  30. - Database tests carry the `//go:build integration` tag and run with `go test -tags integration ./...`.
  31. ## Dependencies
  32. - Add a module only when the task requires it, and name it in the PR description.