Hello, I am Faustin of Money Forward.
If you visited our sponsor booth at Go Conference 2023,
then you may remember that Andy live coded grpcake
,
a command-line client for testing gRPC services during development.
If you couldn't make it to the session, or if you just want a quick recap,
I will tell you about it in this post.
Motivation Behind grpcake
As a CLI tool, you can use grpcake
from the embedded terminal of that IDE you love so much,
eliminating the need for those disruptive switches to a different GUI. Written in Golang,
it also offers seamless integration with gRPC projects also written in Go. The difference
with other gRPC clients written in Golang is its user interface.
The Basics of grpcake
Grpcake
is designed to make the process of testing gRPC services from the command line
simple. Instead of having to pass JSON strings, grpcake
reads parameters as
regular command line arguments.
Let's consider an example from the gRPC go quickstart,
the Greeter
Service:
// The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; }
For the server
to support reflection, we modify the code by importing
google.golang.org/grpc/reflection
and adding reflection.Register(s)
in the function main
.
After launching the server, we're ready to test the Greeter
service using grpcake
.
grpcake --url localhost:50051 --grpc-method helloworld.Greeter/SayHello name="Go Con 2023"
The response is something like this:
2023/06/19 08:42:26 request json body: {"name":"Go Con 2023"} 2023/06/19 08:42:26 Response: { "message": "Hello Go Con 2023" }
Grpcake
also supports raw JSON arguments for request parameters, so you can pass
in complex JSON objects as parameters by using :=
instead of =
. Consider a
hypothetical gRPC service Person
, with a method information
taking a
parameter person
shaped as:
{ "person": { "name": string, "age": number, "address": { "city": string, "prefecture": string, "zip": number } } }
To test this service, you can run a command like this
grpcake --url SERVER_URL --grpc-method Person/information person.address:={"prefecture":"Kanagawa"} person.age:=2023
Under the Hood
Grpcake
utilizes various packages from google.golang.org/protobuf
to dynamically manipulate protobuf messages, bufbuild/protocompile
for parsing protobuf files, jhump/protoreflect
for querying servers that support reflection, and tidwall/sjson
for converting command-line arguments into JSON for the gRPC request.
Thoughts on live coding
Our original plan for the conference was to guide participants through building the tool during a hands-on workshop. However, we quickly realized that ensuring an engaging experience for all participants in a virtual setting would be quite a challenge. So, we opted for the live coding session instead, which we hope was as engaging and educational. You can code along with Andy, and rebuild the tool for yourself by watching the video.
We hope this tutorial inspires gRPC services developers to explore what Golang has to offer, making our lives easier when writing gRPC services.
Thanks
I would like to thank the organizers of the Go Conference for giving us a platform to learn together. Also special thanks to my colleagues Acha, Uji and Mikito for supporting the workshop from preparation to execution.