Skip to content

kevinmichaelchen/api-go-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

460fd25 · Dec 4, 2022

History

25 Commits
Oct 6, 2022
Jul 8, 2022
Dec 4, 2022
Dec 4, 2022
Dec 4, 2022
Oct 17, 2022
Jul 8, 2022
Jul 9, 2022
Jul 9, 2022
Dec 4, 2022
Dec 4, 2022
Dec 4, 2022
Oct 6, 2022
Jul 8, 2022
Dec 4, 2022
Dec 4, 2022
Jul 8, 2022
Jul 8, 2022

Repository files navigation

api-go-template

Lines Of Code

A boilerplate Go repo that comes with:

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.

Project structure

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

Getting started

# 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"}'

Database

Run Migrations

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.

Create New Migration

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.

Auto-generate ORM DB Models

We have a sqlboiler command that introspects the DB and generates ORM models.

make gen-models

Configuration comes from sqlboiler.toml

Telemetry

Traces

See traces in Jaeger here

Metrics

See metrics in Prometheus here.