Skip to content
anonychunPublic

About

Golang without bootstrapping fatigue

Resources

Stars

54 stars

Watchers

1 watching

Forks

Repository files navigation

Bibit

Bibit is a Go project template for backend services. One command gives you a working HTTP server, gRPC server, background worker, PostgreSQL setup, migrations, code generators, tests, and observability, so you can start on business logic right away.

The code comes with a small sign-up/sign-in feature (user and user_session). That feature exists to show how each layer is written. Read it, copy its patterns, then delete it. See Starter kit.

Contents

Requirements

  • Go 1.27+
  • PostgreSQL 18+ (the schema uses uuidv7())
  • goreman for ./bin/dev: go install github.com/mattn/goreman@latest
  • protoc, only if you write gRPC services

The scripts in bin/ install the other tools (air, mockery, nilaway, protoc plugins) the first time you run them.

Create a project

wget -qO- https://raw.githubusercontent.com/anonychun/bibit/refs/heads/main/new.sh | bash -s github.com/anonychun/billing-api

The script clones Bibit, replaces the module path github.com/anonychun/bibit with the name you pass, renames the # Bibit heading in README.md and AGENTS.md, copies .env.sample to .env, and writes the result to a directory named after the last path segment (billing-api in the example).

Then:

cd billing-api
# fill in .env
./bin/db setup
./bin/dev

Commands and conventions

AGENTS.md lists every bin/ command and the rules for each layer: component shape, entity, migration, repository, usecase, errors, handlers, middleware, jobs, file storage, tests, and commits. Coding agents read it automatically, and it is the reference for people too.

Project layout

cmd/
  db/             database CLI
  generate/       code generators
  server/         HTTP + gRPC servers
  worker/         background job worker
internal/
  api/            response envelope, API errors, validation errors
  bootstrap/      dependency injector and process lifecycle
  client/         clients for other systems (River job queue)
  config/         environment configuration
  consts/         shared constants and API errors
  current/        request-scoped values on context.Context
  db/             database connections, migrator, seeder
  dto/            response shapes shared across usecases
  entity/         database models
  job/            background jobs, one package per job
  lib/            small helpers
  middleware/     HTTP middleware, one package per middleware
  o11y/           logging, metrics, tracing
  repository/     data access, one package per table
  server/         HTTP server, gRPC server, routes
  storage/        file storage (S3)
  usecase/        business logic and handlers, one package per feature
  validation/     struct validation
  worker/         worker process and job registration
migrations/       SQL and Go migrations (embedded in the binary)
proto/            protobuf definitions
pkg/pb/           generated protobuf code
public/           static files served at /

Observability

Every command calls o11y.Setup at startup:

  • Logs go to stdout as JSON through log/slog. Use slog.Info(...) and friends anywhere.
  • Metrics use OpenTelemetry with a Prometheus exporter, served at GET /metrics. The Echo OpenTelemetry middleware records HTTP metrics.
  • Traces use OpenTelemetry, are sampled at 100%, and go to OTLP_ENDPOINT over gRPC. The service name is the Go module path.

Deployment

The Dockerfile builds the db, server, and worker binaries into a Debian slim image. On start, bin/docker-entrypoint runs db create, db migrate, and db seed, then starts both processes from Procfile with goreman. To run one process per container, override the command:

docker run --env-file .env my-image /app/bin/server start
docker run --env-file .env my-image /app/bin/worker start

Starter kit

These files exist to demonstrate the patterns. Delete them, or rewrite them for your domain, once you have your own feature in place:

  • internal/entity/user.go, internal/entity/user_session.go
  • internal/repository/user/, internal/repository/user_session/
  • internal/usecase/api/v1/app/auth/
  • internal/middleware/auth/
  • internal/job/hello/, and its registration in internal/worker/worker.go
  • internal/consts/cookie.go and the auth errors in internal/consts/error.go
  • The auth handler field in internal/server/http_server.go and the /api/v1/app routes in internal/server/route.go
  • The users and user_sessions tables in migrations/20010114000000_init.sql
  • current.User in internal/current/current.go, if you drop the auth middleware

Keep the rest: health (backs /up and the gRPC health service), the root repository package (transactions), attachment, storage, and the infrastructure packages.

About

Golang without bootstrapping fatigue

Resources

Stars

54 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages