Nerd For Tech
Published in

Nerd For Tech

Build a Microservice App Using gRPC, Python, and Golang (Part 3)

Photo by Minkus on Unsplash

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!

Todo Service

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 todo folder.

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 CreateTodo, GetTodos, UpdateTodo, and DeleteTodo; basically normal CRUD operations. For each procedure, we also defined its Request and Response form. For example, CreateTodoRequest has userID, title, 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 CreateTodoResponse).

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.

Todo Client

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 middleware.pyinside it.

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

If you have any questions or feedback, please feel free to leave a comment or contact me by email, 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.




NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Recommended from Medium

MicroPython Mussings

Pi0 Media Server

The NEW Peerio: A Brand New Architecture. A Brand New Experience.

Choreography Saga Pattern With Spring Boot — Microservice Design Patterns

Pivot Points Trading Strategies — The Full Guide.

Top 10 Python Libraries for Machine Learning

SEEDS World Structure — Part 3

Optimize your AWS Lambda cost with multi-threading in nodeJS

Single dollar note on a table

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Agus Richard

Agus Richard

I am a Software Engineer and Data Science Enthusiast. Love to learn and write. LinkedIn:

More from Medium

How to Connect PostgreSQL Database with Golang

Todo-list Node JS like REST API Architecture with Golang ( gin, gorm, & mysql )

Building a Book Store API in Golang With Gin

Testing Web Server: Part 2