Building Microservices in Go -Part 2 : Live Reload

Saad Farhan
3 min readNov 7, 2022

--

Okay so in Part 1 we set up our basic project, wrote docker files and created a single end point which returned a simple string. In this part we are going to delve further into building our micro-services. The first thing that we need to do is enable live reloading in our code. Currently if we make any changes to the code and save it, we will have to re-build our binaries manually to verify even the smallest changes. This could be pretty tiresome and will generally slow you down. So the first thing that we are going to do is configure air that allows us to add live reloading to our go apps. So let’s get started

First thing is to update our Dockerfile from this :

To this :

We’ve made two changes to the docker file. Instead of building the Go binary we are downloading and installing the air binary. We also updated the last line to point to air instead of running the Go build.

So now if you go to the main.go file, make changes anywhere in the code and save it you still won’t be able to see the server reload because there’s one additional step that we need to take.

We need to add volumes to our docker-compose.yml so we can mount our virtual directory with the local one. We do this by specifying our local directory first and the working directory of our micro-service. Now air will be able to access the container’s data and reload the server on the go. Let’s test it out

Andd it’s working !!!

Now let’s add a database service to our project as well. In our service directory we are going create a new folder called db, inside it we will create two files, Dockerfile and create.sql. So your directory structure should look like this

Directory Structure

So let’s start writing dockerfile for our database service

This is pretty simple and straightforward. We pull the postgres binary and then pass our create.sql as an entrypoint to that image . Our create.sql is only going to have a single command

create database web_microservice;

This will create a database for us as soon as the database service is up and running. We will also need to update our docker-compose.yml file to add our database service

This is pretty similar to what we did for our blog service except for a few small details. We defined our container name, context, internal / external ports and environment variables for our db service. Other than that we added depends on and links args in our web_blog service to make sure that it only runs if the database service is successfully built.

So let’s connect to our server by opening pgAdmin and adding in a new server. Use the following values to add in the server and use “postgres” as password for your db just as you specified in your docker-compose.yml file.

If everything goes well you will be able to see your database under the server like this :

So we configured live reloading and added a database service in this part. In the next one we are going to be solely focused on databases, how to connect them, define models, configure migrations etc. You can find the source code for this part here. If you’ve any problem with anything, feel free to send me a text on my LinkedIn.

--

--

Saad Farhan

Product Engineer / Flutter / Gopher / Python / Movie Geek