Skip to content

eannchen/go-backend-architecture

Repository files navigation

Go Backend Architecture

Go Version Go Report Card

Clean Architecture Go backend template for building maintainable, testable APIs with built-in caching and observability, designed for engineers and AI agents.

Purpose

For both engineers and AI agents. Shared rules in AGENTS.md keep structure consistent. The template:

  • Provides a reusable backend starter that follows clean architecture.
  • Keeps business logic isolated from frameworks and vendors.
  • Offers production-ready foundations: HTTP, DB (PostgreSQL with pgvector), cache-aside user store, KV-backed auth (session/OTP/OAuth), migrations, tracing, metrics, and structured logging.
  • Serves as a base repo to clone for new app projects.

Architecture and Principles

Architecture & structure

Structural patterns

Behavioral & creational

Data & transport

See package-level README.md files and AGENTS.md for implementation guidance and shared architecture rules for both engineers and AI agents.

Third-Party Tools

Transport

Database

Why SQL-first data access (no ORM)

  • Raw SQL with sqlc + squirrel provides explicit query control, predictable performance tuning, and compile-time type safety.
  • Common downsides are handled by:
    • sqlc generated typed mappings to reduce runtime schema/query mismatch risk.
    • squirrel composable dynamic SQL to avoid fragile string concatenation.
    • Clean Architecture + repository boundaries to isolate SQL in infra adapters and keep usecases storage-agnostic.

Cache

Observability & logging

Development & infra

Requirements

Use as a Starter

  1. Create a new repository from this template.
  2. Bootstrap the project:
./scripts/bootstrap-template.sh --module github.com/your-org/your-backend

This updates module/import paths, service and stack naming, OpenAPI title, and README title.

  1. If you cloned this repo directly, rename your project directory and set the Git remote to your new repository. The script does not change directory names or remotes.
  2. Validate with make openapi-generate && make test.
  3. Review docker-compose.yml, .env.example, and docs/openapi.yaml for project-specific values. The auth (pluggable OTP/OAuth + session), cached user store, and health modules are production-ready foundations—extend them and add your own migrations and features.
  4. Review AGENTS.md and package-level README.md files before feature development.

Setup and Run

  1. cp .env.example .env
  2. make install
  3. make dev-up && make migrate-up
  4. make run
  5. Verify GET /health?check=ready

Common commands: make dev-logs, make dev-down, make migrate-status, make openapi-generate, make run-stop.

Default local ports: Postgres 5432, Redis 6379, OTel 4317/4318, HyperDX 8081.

About

Clean Architecture Go backend template for maintainable, testable APIs with observability, designed for AI agents.

Topics

Resources

License

Stars

Watchers

Forks

Contributors