A boilerplate Go repo that comes with:
- Dependency Injection using uber-go/fx (:tv: video)
- DB Migration Tool using golang-migrate (:tv: video)
- ORM using sqlboiler (:tv: video)
- Env Var Configs using go-envconfig
- gRPC using connect-go
- Protobufs, compiled, formatted, linted, and more with Buf
- Command-Line Interface using cobra
These libraries do a lot of heavy lifting in terms of boilerplate.
For example:
- The fx framework manages dependency injection and application life-cycle
for you. (See
example_test.go
). - sqlboiler makes DB CRUD really simple. (See
foo.go
). Raw queries are supported as an escape hatch.
Directory | Description |
---|---|
./cmd |
CLI for making gRPC requests |
./idl |
Protobufs (Interface Definition Language) |
./internal/app |
App dependency injection / initialization |
./internal/idl |
Auto-generated protobufs |
./internal/models |
Auto-generated ORM / models |
./internal/service |
Service layer / Business logic |
./internal/service/db |
Data layer |
./schema |
SQL migration scripts |
# Step 1: Start containers in detached (background) mode
docker-compose up -d
# Step 2: Create the database schema
make migrate-up
# Step 3: Start app
go run main.go
Finally, hit the API (using HTTPie)
# Create a new Foo
http POST \
http://localhost:8081/coop.drivers.foo.v1beta1.FooService/CreateFoo \
name="Kevin"
# Get existing Foo
http POST \
http://localhost:8081/coop.drivers.foo.v1beta1.FooService/GetFoo \
id="cb4c4rnrirfucgsert7g"
Or with curl:
curl -X POST http://localhost:8081/coop.drivers.foo.v1beta1.FooService/CreateFoo \
-H "Content-Type: application/json" \
-d '{"name": "Kevin"}'
curl -X POST http://localhost:8081/coop.drivers.foo.v1beta1.FooService/GetFoo \
-H "Content-Type: application/json" \
-d '{"id": "cb4c4rnrirfucgsert7g"}'
make migrate-up
or you can run:
docker run -v $(pwd)/schema:/migrations \
--network host \
--rm \
migrate/migrate \
-path=/migrations/ \
-database postgres://postgres:postgres@localhost:5432/foo\?sslmode=disable \
up
This will run all migrations in ./schema
.
To create a new migration called create-new-table
, run:
docker run -v $(pwd)/schema:/migrations \
--network host \
--rm \
migrate/migrate \
-path=/migrations/ \
create \
-dir /migrations \
-ext sql \
create-new-table
This will create a new up and down migration in ./schema
.
We have a sqlboiler command that introspects the DB and generates ORM models.
make gen-models
Configuration comes from sqlboiler.toml
See metrics in Prometheus here.