Design of the gRPC service using the gRPC-Gateway. The service allows the classic design of gprc services, i.e. client-server, to use REST queries thanks to the implemented grpc-gateway.
The first installation step is to install the Go language on our system. Follow the link to the official Go website where we can download the language to our system.
To create the grpc service, we need to install two packages. After installing Go first, we can use the following commands in the terminal
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
Update your PATH
so that the protoc
compiler can find the plugins:
export PATH="$PATH:$(go env GOPATH)/bin"
After opening the project, type the following command in the terminal
go mod tidy
And then we install the mandatory packages to generate the files responsible for the grpc-gateway
go install \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
The last step, to be sure, is to use the command found in the Makefile
make clean
and command
make create
The first command will delete all files generated previously and then the create command will generate new ones.
To launch the project, we need to enter the server folder and then run the main.go
file
cd serwer/
go run main.go
The server will listen on the port :8081
and process endpoints thanks to the multiplexer
Once the server is up and listening in the second terminal window, we can send the first requests from the ROOT
path.
First command to generate authorization token
curl http://localhost:8081/auth
We save the generated token and then use the command
curl -H "Authorization: Token your_token" http://localhost:8081/some_path
Using the token generated by the first command will allow us to familiarise ourselves with the operation of the programme
To run a validation test on the generated token at endpoint /auth
, we use the command
go test -run Auth
There is only one check function in the test file due to the handling of all errors in the other one: /allPath