A simple boilerplate application, which uses gRPC to establish communication between fastapi and golang servers
- Go v1.19
- Python v3.8 or higher
- Install the
Protobuf
compiler
$ sudo apt install -y protobuf-compiler
- Install the
gen-go
andgen-go-grpc
plugins
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
- Install the dependencies for the
golang
service
$ go mod tidy
- Install the dependencies for the
fastapi
service
$ pip install -r fastapi/app/requirements.txt
- Run the
golang
service
$ go run main.go -port 8080
- Run the
fastapi
service
$ cd ./fastapi/app && uvicorn main:app --host 127.0.0.1 --port 8000 --reload && cd ../../
Once the servers are running visit, http://127.0.0.1:8000/docs and make a get
request to the /token/verify/
api route. This route makes an internal gRPC call to the golang
service which is responsible for handling the authentication
- Generate
golang
gRPC code
$ protoc --go_out=. --go-grpc_out=. protobufs/auth.proto
- Generate
python
gRPC code
$ cd ./fastapi/app && python -m grpc_tools.protoc -I ../protobufs --python_out=. --grpc_python_out=. ../protobufs/auth.proto && cd ../../
- Import error on
auth_pub2_grpc.py
Open the auth_pub2_grpc.py
file and change the import from
import auth_pb2 as auth__pb2
to
from . import auth_pb2 as auth__pb2
Cheers!