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.protofile linting and provide additional intellisense.
- Click
Use this templatebutton and create your repository. - Clone your repository to desired directory on the local machine.
(usinggit cloneor git tools in your IDE). - Run
make initcommand in your project root.
This will install required protoc binaries to your$GOPATH/bindirectory. - Change
awesomeProjectingo.modfile 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.yamlfile and changeawesomeProjectto your module name in thego_package_prefix.defaultfield.
- Write your first
.protofile in/api/proto/{proto_package}directory.
It may look like/api/proto/v1/helloworld.proto
or/api/proto/helloworld/v1/helloworld.proto. - Run
make gencommand.
This will generate API specification (api/spec/helloworld.swagger.jsonfile).
And.pb.gofiles in/internal/api/helloworld/v1or/internal/api/v1directory
corresponding to your.protopackage name. - Write your handlers, use cases and repositories, set up gRPC and/or HTTP servers.
Note: for more help, you can find aREADME.mdfile in each directory of this repository.
- To test or run your project run
make testormake runcommands respectfully. - Buf makes it easier to import third party
.protopackages without manually copying them to your/third_partydirectory.
To manage your dependencies go to/api/proto/buf.yamlfile and make changes indepsfield.
Note: try to keepdepsin/api/proto/buf.yamlandgo_package_prefix.exceptinbuf.gen.yamlin sync to avoid incorrectGoimport paths.
After all the above, runmake updatecommand.
If you have buf extension installed, updated dependencies will be available in the intellisense. - To learn more about buf tool, visit introduction to buf.