Getting an automatically generated gRPC service definition is super cool, but we still need to register it to a concrete gRPC server, that listens on some TCP port for traffic and is able to respond to RPC calls.
We decided not to generate this part automatically because it typically involves some team/org specific behavior such as wiring in different middlewares. This may change in the future. In the meantime, this section describes how to create a simple gRPC server that will serve our service code.
Create a new file
cmd/server/main.go and write:
Notice that we added an import of
github.com/mattn/go-sqlite3, so we need to add it to our module:
Next, let's run the server, while we write a client that will communicate with it:
Let's create a simple client that makes some calls to our server. Create a new file named
cmd/client/main.go and write:
Our client creates a connection to port 5000, where our server is listening, then issues a
request to create a new user, and then issues a second
Get request to retrieve it from the database.
Let's run our client code:
Observe the output:
Hooray! We have successfully created a real gRPC client to talk to our real gRPC server! In the next sections, we will see how the ent/gRPC integration deals with more advanced ent schema definitions.