Awesome Go

go-rest-template

CategoryMiscellaneous
SubcategoryProject Layout
Stars176

REST API template with chi, an OpenAPI 3.1 contract, JWT authentication, PostgreSQL or SQLite, and an init tool that removes the features you do not need

About go-rest-template

go-rest-template

A Go REST API template you can cut down to what you need.

CI Release Go Go Reference codecov CodeQL OpenSSF Scorecard License: MIT

Documentation · Quickstart · Changelog

A working service, not a skeleton: authentication, users and an example resource on PostgreSQL or SQLite, described by an OpenAPI 3.1 contract that is enforced at runtime. An init tool renames it and removes the parts you do not want, and CI checks that every combination of options still builds and passes its tests.

Start a project

Create a repository with Use this template (or clone this one), then:

make init

It asks for your module path and what to keep. The same without questions:

go run ./tools/init -yes -module github.com/acme/billing -db postgres -example=false
OptionDefaultRemoves when turned off
-db both|postgres|sqlitebothThe other driver, its migrations and its dependency
-exampleonThe tasks resource and the resource generator
-metricsonPrometheus metrics
-tracingonOpenTelemetry tracing and its dependencies
-ratelimitonPer-IP rate limiting
-observabilityonThe Prometheus, Grafana and Tempo compose profile
-helmonThe Helm chart
-devcontaineronThe dev container

The website, the changelog and the init tool itself belong to the template and are always removed. What is left is your project.

Run it

make run

The API listens on http://localhost:8080 with a SQLite file under data/. The interactive reference is at /docs. To have an account to sign in with, create the first administrator at startup:

BOOTSTRAP_ADMIN_ENABLED=true BOOTSTRAP_ADMIN_USERNAME=admin \
BOOTSTRAP_ADMIN_FIRST_NAME=Ada BOOTSTRAP_ADMIN_LAST_NAME=Lovelace \
BOOTSTRAP_ADMIN_PASSWORD=change-me-now-123 make run
curl -s localhost:8080/api/v1/auth/login \
  -H 'Content-Type: application/json' \
  -d '{"username":"admin","password":"change-me-now-123"}'

make compose-up runs it with PostgreSQL instead, and make obs-up adds Prometheus, Grafana and Tempo.

What is in it

API

  • OpenAPI 3.1 contract embedded in the binary. Requests that do not match it are rejected before reaching a handler; a test fails when a route and the contract disagree.
  • Errors as RFC 9457 problem responses with a stable code, per-field details and the request ID.
  • Cursor pagination, and optimistic concurrency with ETag and If-Match.

Authentication

  • Short-lived access tokens and rotating refresh tokens. Reusing a refresh token revokes every token of that login.
  • HS256, or EdDSA with the public key published as a JWKS.
  • The caller is reloaded on every request, so deleting a user or changing a role takes effect immediately.

Data

  • One repository implementation for PostgreSQL and SQLite, written in plain SQL.
  • Embedded migrations run by goose, with down migrations and an advisory lock so replicas do not race.
  • Transactions that repositories join through the context.

Operations

  • Health and readiness probes, build information, graceful shutdown that fails readiness first.
  • Prometheus metrics and pprof on a separate admin port; optional OpenTelemetry tracing.
  • Structured logs with slog, security headers, configurable CORS, rate limiting.
  • Static distroless image with its own health check, a Helm chart and a compose stack.

Project

  • Configuration declared once: it validates everything at startup, reads .env files and generates its own reference.
  • A generator for new resources: make resource name=project.
  • End-to-end tests against both databases, linting, vulnerability scanning, and releases with signed images, SBOMs and build provenance.

Layout

api/                OpenAPI contract
cmd/api             the server
cmd/migrate         schema migrations: up, down, status
internal/app        composition root: wiring and routes
internal/auth       tokens, passwords, authentication middleware
internal/users      the users feature: model, rules, storage, endpoints
internal/tasks      the example feature
internal/platform   database, HTTP conventions, middleware, tracing
internal/config     environment configuration
migrations/         SQL migrations per driver
deployments/        compose stack, observability and Helm chart
tools/              init and the resource generator

A feature owns everything about itself, depends only on platform and auth, and is mounted in one place. Removing one is deleting its directory and two lines in internal/app.

Development

make help     # every target
make check    # tidy, vet, lint and tests with the race detector
make dev      # run with hot reload

The website lives in docs/: pip install -r requirements-docs.txt, then make docs-serve.

See CONTRIBUTING.md. The earlier versions are available as the 2.0.1 and 1.0.0 tags; the documentation covers migrating from 2.x.

License

MIT © Antonio Payá González

Frequently Asked Questions

What is go-rest-template?

go-rest-template is a Miscellaneous library for the Go programming language. REST API template with chi, an OpenAPI 3.1 contract, JWT authentication, PostgreSQL or SQLite, and an init tool that removes the features you do not need

How do I install go-rest-template?

Install go-rest-template with the Go module system using `go get antoniopaya22/go-rest-template`. Check the repository for the current installation instructions.

What category does go-rest-template belong to?

go-rest-template is listed under Miscellaneous, specifically Project Layout.

← Back to Project Layout