|
1 | | -# Repository Guidelines |
2 | | - |
3 | | -## Project Structure & Module Organization |
4 | | -- Source layout: `cmd/cli` and `cmd/httpserver` are entrypoints; shared packages live in `common/`, `httpserver/`, `database/`, and `metrics/`. |
5 | | -- Tests: co-located `*_test.go` files (e.g., `httpserver/handler_test.go`, `database/database_test.go`). |
6 | | -- Artifacts: binaries output to `build/` via Makefile targets. Dockerfiles: `cli.dockerfile`, `httpserver.dockerfile`. |
7 | | -- Go version: 1.24 (see `go.mod`). Module path: `github.com/flashbots/go-template`. |
8 | | - |
9 | | -## Coding Style & Naming Conventions |
10 | | -- Use tabs instead of spaces for indentation. |
11 | | -- Always run `make fmt` and `make lint` (and `make test`) before committing. |
12 | | -- Formatting: run `make fmt` before committing. Go files must pass `golangci-lint` (config in `.golangci.yaml`). |
13 | | -- Style: idiomatic Go; exported identifiers use CamelCase; packages lower-case short names; errors returned, not panicked. |
14 | | -- JSON tags: prefer snake_case (configured via `tagliatelle`). |
15 | | -- Logging: use `common.SetupLogger` (slog) and structured fields; respect `--log-json` and `--log-debug` flags. |
16 | | - |
17 | | -## Build, Test, and Development Commands |
18 | | -- `make build`: builds CLI and HTTP server into `build/`. |
19 | | -- `make build-cli` / `make build-httpserver`: build individual binaries. |
20 | | -- `make lint`: run formatters and linters (`gofmt`, `gofumpt`, `go vet`, `staticcheck`, `golangci-lint`). |
21 | | -- `make test` / `make test-race`: run tests (optionally with race detector). |
22 | | -- `make fmt`: apply formatting (`gofmt`, `gci`, `gofumpt`) and `go mod tidy`. |
23 | | -- `make cover` / `make cover-html`: coverage summary / HTML report. |
24 | | -- Docker: `make docker-cli`, `make docker-httpserver` build images using the respective Dockerfiles. |
25 | | - |
26 | | -## Testing Guidelines |
27 | | -- Framework: standard `testing` with `testify/require` for assertions. |
28 | | -- Run: `make test` (or `go test ./...`). |
29 | | -- Database tests: gated by `RUN_DB_TESTS=1` and a Postgres instance. Example: |
30 | | - - `docker run -d --name postgres-test -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres` |
31 | | - - `RUN_DB_TESTS=1 make test` |
32 | | -- Coverage: aim to keep or increase coverage; use `make cover`. |
33 | | - |
34 | | -## Commit & Pull Request Guidelines |
35 | | -- Commits: concise, imperative mood; scope prefixes like `ci:`, `docs:`, `fix:`, `feat:` when helpful. Reference issues/PRs. |
36 | | -- PRs: include a clear description, linked issues, test plan (commands run), and any config notes. Ensure `make fmt lint test` pass locally. |
37 | | - |
38 | | -## Security & Configuration Tips |
39 | | -- Configuration: prefer flags/env via `common.GetEnv` and CLI options; avoid hardcoding secrets. |
40 | | -- HTTP server: `/debug` (pprof) is opt-in; do not expose publicly. Metrics on `/metrics` (Prometheus format). |
| 1 | +This file provides guidance to LLMs when working with code in this repository. |
| 2 | + |
| 3 | +## Build Commands |
| 4 | + |
| 5 | +```bash |
| 6 | +make build-cli # Build CLI binary to ./build/cli |
| 7 | +make build-httpserver # Build HTTP server binary to ./build/httpserver |
| 8 | +make build # Build all binaries |
| 9 | +``` |
| 10 | + |
| 11 | +## Test Commands |
| 12 | + |
| 13 | +```bash |
| 14 | +make test # Run all tests |
| 15 | +make test-race # Run tests with race detector |
| 16 | +go test ./... -run TestName # Run a single test |
| 17 | + |
| 18 | +# Database tests require a running Postgres instance: |
| 19 | +docker run -d --name postgres-test -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres postgres |
| 20 | +RUN_DB_TESTS=1 make test |
| 21 | +``` |
| 22 | + |
| 23 | +## Lint and Format |
| 24 | + |
| 25 | +```bash |
| 26 | +make lint # Run all linters (gofmt, gofumpt, go vet, staticcheck, golangci-lint) |
| 27 | +make fmt # Format code (gofmt, gci, gofumpt, go mod tidy) |
| 28 | +make lt # Run both lint and test |
| 29 | +``` |
| 30 | + |
| 31 | +## Architecture |
| 32 | + |
| 33 | +This is a Go project template with two entry points: |
| 34 | + |
| 35 | +- **cmd/cli/main.go** - CLI application entry point using urfave/cli |
| 36 | +- **cmd/httpserver/main.go** - HTTP server entry point with graceful shutdown |
| 37 | + |
| 38 | +### Key Packages |
| 39 | + |
| 40 | +- **httpserver/** - HTTP server with chi router, includes `/livez`, `/readyz`, `/drain`, `/undrain` endpoints, metrics middleware, and optional pprof |
| 41 | +- **database/** - Postgres database layer using sqlx with sql-migrate for migrations |
| 42 | +- **database/migrations/** - In-memory migrations registered in `Migrations` variable |
| 43 | +- **metrics/** - VictoriaMetrics-based Prometheus metrics with HTTP middleware |
| 44 | +- **common/** - Shared utilities including structured logging setup (slog-based via httplog) |
| 45 | + |
| 46 | +### HTTP Server Pattern |
| 47 | + |
| 48 | +The server runs two HTTP servers: main API (default :8080) and metrics (default :8090). Supports graceful shutdown with configurable drain duration for load balancer compatibility. |
| 49 | + |
| 50 | +### Database Migrations |
| 51 | + |
| 52 | +Migrations are defined as Go code in `database/migrations/` and registered in `migrations.Migrations`. They run automatically on database connection unless `DB_DONT_APPLY_SCHEMA` env var is set. |
0 commit comments