Project Refactor

Part 2, Lesson 2



In this lesson, we'll break up the base project structure into multiple projects to maintain a clear separation between each service...


Before we break up the mono project, it's important to note that you can manage a microservice architecture in a single project (with a single git repo). You could add each individual service to a "services" directory as a separate directory, for example. There are pros and cons to each approach - mono repo vs multiple repo. Do your research.

With that, create two new projects:

  1. flask-microservices-main
  2. flask-microservices-client

For each, init a new git repo and add a .gitignore file.

The main project will house the Docker Compose files, Nginx config, and any admin scripts. Essentially, you'll manage all services from this project. Meanwhile, we'll add React to the client.

Steps:

  1. Test current structure
  2. Refactor
  3. Test new structure
  4. Update production

Test current structure

Run the tests:

$ docker-compose run users-service python manage.py test

All should pass. With tests in place, we can refactor with confidence.

Refactor

Bring down the containers and remove the images:

$ docker-compose down

Then remove the containers:

$ docker-compose rm

Do the same for production. Change the active host and point the Docker client at it, and then bring down the containers and remove them along with the images:

$ docker-machine env aws
$ eval $(docker-machine env aws)
$ docker-compose -f docker-compose-prod.yml down
$ docker-compose -f docker-compose-prod.yml rm

Switch the active host back to dev:

$ docker-machine env dev
$ eval $(docker-machine env dev)

Move the following files and folders from flask-microservices-users to the flask-microservices-main project:

docker-compose-prod.yml
docker-compose.yml
nginx/Dockerfile
nginx/flask.conf

Back in the flask-microservices-users project, commit your code and push up the changes.

Then update the build commands for users-db and users-service to point to the git repo within both Docker Compose files:

  1. users-db

    build: https://github.com/realpython/flask-microservices-users.git#master:project/db
    

    master:project/db uses the Dockerfile found in "project/db" in the master branch.

  2. users-service:

    build: https://github.com/realpython/flask-microservices-users.git
    

Since this changes the "build context" from the local machine to a git repo, we need to remove the volume from users-service. Once removed, build the images and spin up the containers:

$ docker-compose up -d --build

While this is spinning up think about why we had to remove the volume. What is the "build context"? Turn to Google for help.

Test new structure

Once up, create and seed the db and run the tests:

$ docker-compose run users-service python manage.py recreate_db
$ docker-compose run users-service python manage.py seed_db
$ docker-compose run users-service python manage.py test

Grab the IP, from docker-machine ip dev, and make sure the app works in the browser.

Update production

First, change the active host and point the Docker client at it:

$ docker-machine env aws
$ eval $(docker-machine env aws)

Bring up the containers:

$ docker-compose -f docker-compose-prod.yml up -d --build

Just like before, create and seed the db and run the tests:

$ docker-compose -f docker-compose-prod.yml run users-service python manage.py recreate_db
$ docker-compose -f docker-compose-prod.yml run users-service python manage.py seed_db
$ docker-compose -f docker-compose-prod.yml run users-service python manage.py test

Test in the browser as well. Commit your code and push up to GitHub.


Project Refactor

In this lesson, we'll break up the base project structure into multiple projects to maintain a clear separation between each service...


Before we break up the mono project, it's important to note that you can manage a microservice architecture in a single project (with a single git repo). You could add each individual service to a "services" directory as a separate directory, for example. There are pros and cons to each approach - mono repo vs multiple repo. Do your research.

With that, create two new projects:

  1. flask-microservices-main
  2. flask-microservices-client

For each, init a new git repo and add a .gitignore file.

The main project will house the Docker Compose files, Nginx config, and any admin scripts. Essentially, you'll manage all services from this project. Meanwhile, we'll add React to the client.

Steps:

  1. Test current structure
  2. Refactor
  3. Test new structure
  4. Update production

Test current structure

Run the tests:

$ docker-compose run users-service python manage.py test

All should pass. With tests in place, we can refactor with confidence.

Refactor

Bring down the containers and remove the images:

$ docker-compose down

Then remove the containers:

$ docker-compose rm

Do the same for production. Change the active host and point the Docker client at it, and then bring down the containers and remove them along with the images:

$ docker-machine env aws
$ eval $(docker-machine env aws)
$ docker-compose -f docker-compose-prod.yml down
$ docker-compose -f docker-compose-prod.yml rm

Switch the active host back to dev:

$ docker-machine env dev
$ eval $(docker-machine env dev)

Move the following files and folders from flask-microservices-users to the flask-microservices-main project:

docker-compose-prod.yml
docker-compose.yml
nginx/Dockerfile
nginx/flask.conf

Back in the flask-microservices-users project, commit your code and push up the changes.

Then update the build commands for users-db and users-service to point to the git repo within both Docker Compose files:

  1. users-db

    build: https://github.com/realpython/flask-microservices-users.git#master:project/db
    

    master:project/db uses the Dockerfile found in "project/db" in the master branch.

  2. users-service:

    build: https://github.com/realpython/flask-microservices-users.git
    

Since this changes the "build context" from the local machine to a git repo, we need to remove the volume from users-service. Once removed, build the images and spin up the containers:

$ docker-compose up -d --build

While this is spinning up think about why we had to remove the volume. What is the "build context"? Turn to Google for help.

Test new structure

Once up, create and seed the db and run the tests:

$ docker-compose run users-service python manage.py recreate_db
$ docker-compose run users-service python manage.py seed_db
$ docker-compose run users-service python manage.py test

Grab the IP, from docker-machine ip dev, and make sure the app works in the browser.

Update production

First, change the active host and point the Docker client at it:

$ docker-machine env aws
$ eval $(docker-machine env aws)

Bring up the containers:

$ docker-compose -f docker-compose-prod.yml up -d --build

Just like before, create and seed the db and run the tests:

$ docker-compose -f docker-compose-prod.yml run users-service python manage.py recreate_db
$ docker-compose -f docker-compose-prod.yml run users-service python manage.py seed_db
$ docker-compose -f docker-compose-prod.yml run users-service python manage.py test

Test in the browser as well. Commit your code and push up to GitHub.