This is a template repository, which can be used to create backend projects in Go
programming language.
It uses proto
programming language to create an API definition, which is used to generate stubs for gRPC and REST endpoints as well as openAPI.v2 JSON specification.
Think of end product as gRPC service with REST proxy (which is created with minimal effort using grpc-gateway).
Also buf is used to manage proto dependencies without manually downloading files and placing them in your project.
This template has some pre-imported proto libraries (i.e. googleapis/googleapis, envoyproxy/protoc-gen-validate and grpc-ecosystem/grpc-gateway)
- go-example - simple to understand project that uses this template, which will introduce you to the technologies I use
- Buf extension in your IDE of choice.
This will include real-time.proto
file linting and provide additional intellisense.
- Click
Use this template
button and create your repository. - Clone your repository to desired directory on the local machine.
(usinggit clone
or git tools in your IDE). - Run
make init
command in your project root.
This will install required protoc binaries to your$GOPATH/bin
directory. - Change
awesomeProject
ingo.mod
file to the name of your go module.
Recommended prefixing it withgithub.com/{your_username}/
and naming it the same as your repository.
Example:module github.com/grimerssy/go-template
. - Go to
buf.gen.yaml
file and changeawesomeProject
to your module name in thego_package_prefix.default
field.
- Write your first
.proto
file in/api/proto/{proto_package}
directory.
It may look like/api/proto/v1/helloworld.proto
or/api/proto/helloworld/v1/helloworld.proto
. - Run
make gen
command.
This will generate API specification (api/spec/helloworld.swagger.json
file).
And.pb.go
files in/internal/api/helloworld/v1
or/internal/api/v1
directory
corresponding to your.proto
package name. - Write your handlers, use cases and repositories, set up gRPC and/or HTTP servers.
Note: for more help, you can find aREADME.md
file in each directory of this repository.
- To test or run your project run
make test
ormake run
commands respectfully. - Buf makes it easier to import third party
.proto
packages without manually copying them to your/third_party
directory.
To manage your dependencies go to/api/proto/buf.yaml
file and make changes indeps
field.
Note: try to keepdeps
in/api/proto/buf.yaml
andgo_package_prefix.except
inbuf.gen.yaml
in sync to avoid incorrectGo
import paths.
After all the above, runmake update
command.
If you have buf extension installed, updated dependencies will be available in the intellisense. - To learn more about buf tool, visit introduction to buf.