Build a Microservice App Using gRPC, Python, and Golang (Part 3)
In this article, we will continue building our app and focus on the todo CRUD operations. Since the implementations are not quite different from the auth service, hopefully, this article would be shorter. Let’s jump into it!
Likewise the auth service, first we have to do some set up before moving on to write a proto file.
Create a database called grpc_todo and a table called todo. You can create this table using this query.
CREATE TABLE public.todos (id serial NOT NULL,title varchar(64) NOT NULL,description varchar(256) NULL,user_id int4 NULL,CONSTRAINT todos_pkey PRIMARY KEY (id));
Doing some setup. Now, the database.
The configurations are particularly the same as the auth service, the difference is the name of the database. It’s named
grpc_todo instead of
grpc_auth. Next, define the model of our Todo.
Todo model has 4 fields, ID, Title, Description and UserID. Also, we defined the tag for each field. Continue, define repository (for now only the template).
Nothing special about the above code, because it’s just a template for a
repository file (duh!?). We will come back later to implement the details. Now, create the usecase.
Still a usual thing, we had done this in the previous article. Next, this is the new thing, defining todo proto file. Create a file named
todo.proto and put it inside
todo working directory (root directory of todo service).
After that, generate the gRPC code using this command and we’ll have two go files inside
protoc --go_out=./todo --go_opt=paths=source_relative \--go-grpc_out=./todo --go-grpc_opt=paths=source_relative \./todo.proto
From the proto file above, we defined four procedures, which are
DeleteTodo; basically normal CRUD operations. For each procedure, we also defined its
Response form. For example,
descriptions fields. So when a client wants to call the procedure remotely, he has to give the correct form of request, and then the server will respond appropriately (gives us the
After this, let’s define the server part.
Our todo server file is still bare bone, so we will come back later. The last thing is the main file.
At this point, you can run
go run main.go command, but it won’t work properly since we haven’t created the client and establish the connection between this server and the client. No need to worry, let’s defineit now.
Now, make sure that you are in the correct working directory of main service. After that copy the same
todo.proto from the
todo service into
main service and generate the gRPC code in Python using this command.
python -m grpc_tools.protoc -I. --python_out=./todo --grpc_python_out=./todo ./todo.proto
After running this command, we have two generated files. Next, let’s define the client operations.
TodoClient has four methods (excluding the constructor) that we will use in todo view functions. Four of them have their own responsibility, for example,
create_todo method will invoke a remote procedure in todo service when we want to create a todo.
Now, let’s use the client and implement them inside views file.
Note that there is a decorator on top of each view functions. The job of this decorator, just like its name, is to make sure only logged-in user can access this function. To do that, we have to define a middleware. Now, create a folder named middleware and a file named
Our todo service and its connection to main service is almost done. As the final step, you can test this service using Postman. Is everything works properly? If it is, then good, we are doing a good job here.
In the next article, we will migrate to use templates engine so our application is no longer using REST framework. You can find the code for this article here https://github.com/agusrichard/python-golang-grpc/tree/part3.
If you have any questions or feedback, please feel free to leave a comment or contact me by email, email@example.com. Also, if you think this article help you, don’t hesitate to give this article a clap.
Thank you for reading and see you in the next article.