go-grpc-gateway-template
Goal
Bringing practices together when building yet another web service.
Example usage
Project contains echo server with PostgreSQL integration. It supports gRPC and HTTP transports
using gRPC to JSON proxy generator.
Proto files generated with buf.
Requirements
The project has been tested on Go 1.17. It uses some new features such as go install
, go:embed
(for third party dependencies like Swagger UI).
Other binary dependencies will be downloaded to the bin
folder.
First steps
Third party dependencies
To download third party dependencies (e.g. buf, proto-gen-go, goose, Swagger UI) run make deps
.
Build project
Run make build
. It will download Swagger UI if it does not exist and build echoapi/main.go
file.
Lint before commit
Run make lint
. It will check *.proto files with buf and *.go files with golangci-lint
Set up dependent services
Run docker compose up
to run PostgreSQL.
Run tests
# Run unit tests
make test
# Run integration tests
# Paste your connection string.
# If you ran docker compose up, you can copy-paste the code below.
export DSN="user=postgres password=postgres database=postgres sslmode=disable"
# run migrations
GOOSE_DRIVER=postgres GOOSE_DBSTRING=$DSN make migrate
# run integration tests
make test-integration
Create migration
GOOSE_DRIVER=postgres GOOSE_DBSTRING=$DSN make migration
The new migration will be added to the migration folder. Do not forget to rename it.
Apply migrations
GOOSE_DRIVER=postgres GOOSE_DBSTRING=$DSN make migrate
Create new service
-
Add a new service to proto/echo/v1/echo.proto
file or add a new proto file to the proto folder;
-
Then run make
- it will generate new *.pb.go files in the pkg folder.;
See generate code with buf for more information.
After fork
- Rename the name of the module in the
go.mod
file;
- Replace the default
go_package_prefix
with the new module name in buf.gen.yaml file;
- Fix badges references in README.md file;
- Change author in LICENSE file;