This repo is supposed to help with learning how to handle grpc/proto/grpc-gateway/openapi and more via a setup that can generate multiple different protobufs for different services inside your monorepo at the same time and allowing the owner to code using grpc and still expose a Json http 1.1 TCP transport layer if the client needs it.
GoExampleGRPC requires a golang to be installed correctly and it also requires protoc to be installed to work.
We have made this easy by running make install_proto to install the protoc required packages and then make install_buf
to install the buf package more info.
Buf will be responsible for generating the auto generated code (structs and interfaces) based on the idl folder files. All the configurations for buf live in gen file or the work file. This is where the output location for the generated files live and where it should look for input too + all the plugins that you want to use in your code generation.
In this case we are generating grpc, grpc-gateway and openapi specs from the single proto location by using the specified plugins.
On important notice: Google files located in the idl folder were manually copied over and are required for grpc-gateway to work. This was available internally in the grpc golang package (in the third_party package), but now have to be downloaded and added manually.
If you choose to generate the idl_gen .pb.go files - you need to run make clean_proto which will delete all the generated
files (golang, as an example) and then running make proto generates these based on buf.
After the golang files have been generated successfully, the next step is to simply go into your own service and add to the go.mod file, this:
// Mapping to generated idl files (grpc)
replace github.com/copito/example_grpc/idl_gen/go => ../../idl_gen/go/and run:
go mod tidyThe first part: github.com/copito/example_grpc/idl_gen/go should match the module name in the idl_gen module
module github.com/copito/example_grpc/idl_gen/goYour project directory should follow a specific structure:
idl/
|-- avro/
|-- proto/
| |-- google/
| |-- service1/
| | | --hello.proto
idl_gen/
|-- go/
| |-- service1/
| | | --hello.pb.go
| | | --hello_grpc.pb.go
| | | --hello.pb.gw.go
openapi/
services/
|-- service2/
|-- service1/
| |-- main.go
| |-- README.md
| |-- ...
-- README.md
-- MakefileContributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License - see the LICENSE file for details. Feel free to customize it further based on your project's specifics!