Coder Social home page Coder Social logo

cc-project's Introduction

Breaking down monoliths ๐Ÿชจโ›๏ธ

This problem statement has two objectives:

  • Get familiar with reading error stack trace and debugging code.
  • Convert a monolith architecture based docker-compose application into a microservices based architecture.

Requirements:

  • docker and docker-compose. Follow the guides based on your operating system.
  • Internet. Pull docker image python:3.8-alpine beforehand to avoid connectivity issues.

Initial directory structure

โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ docs
โ”‚   โ””โ”€โ”€ <documentation related images/files>
โ”œโ”€โ”€ microservices
โ”‚   โ”œโ”€โ”€ Docker-compose.yaml
โ”‚   โ”œโ”€โ”€ landing
โ”‚   โ”‚   โ”œโ”€โ”€ app
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ app.py
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ requirements.txt
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ templates
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ index.html
โ”‚   โ”‚   โ””โ”€โ”€ Dockerfile
โ”‚   โ”‚

Monolith architecture diagram

Build & Run

# under the microservices directory
# NOTE: For any code changes to be reflected, the build command must be rerun, and then up
docker-compose build
# run without the -d flag incase you want to observe the logs
docker-compose up -d

To stop the services in detached mode

docker-compose down

NOTE: It is possible your first build will not be successful and that's alright. Read the stack trace and debug the errors. The resources and links provided within the manual are sufficient to successfully complete the project.

Tasks

  1. Task-0 - Completing the code ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป

    0.0. Add modules to landing/app/requirements.txt

    • The following modules are required by the landing-service:
    • flask
    • flask-restful
    • requests

    0.1. Write the landing/Dockerfile

    • Dockerfiles help building images using a given set of instructions. You can learn about the basics of Dockerfiles here. Use this document to understand the meaning of certain keywords used by Dockerfiles.
    • For this project, you need to write a Dockerfile for a python application. I have provided sample Dockerfiles here and here.
    • Specifications for the Dockerfile:
      • Use the image python:3.8-alpine
      • Copy the contents of ./app/requirements.txt to /app/requirements.txt
      • Set the working directory to /app
      • Run the following command:
        apk add --update \
        && pip install --upgrade pip  \
        && pip install -r requirements.txt \
        && rm -rf /var/cache/apk/*
        
      • Copy the contents of the ./app to /app folder
      • Run the command python app.py
  2. Task-1 - Debugging ๐Ÿ›๐Ÿ”จ

    1.0. Fix the variable type of the incoming values from index.html

    • By default, Python considers all input as string. Due to this, the arithmetic operations do not seem to be working as intended.
    • Fix the type of the two variable values received from index.html

    1.1. Handle the None type exception

    • This bug is observed only after fixing the previous bug.
    • By default, no values are provided by index.html, causing the application to throw exceptions and crash when localhost:5050 is loaded initially.
    • Avoid the crash by handling the exceptions raised in the landing-service app.
  3. Task-2 - Breaking the monolith architecture ๐Ÿชจโ›๏ธ

    2.0. Decouple the arithmetic functions from landing-service.

    • The four arithmetic functions currently reside under landing-service. However, if landing-service were to become unavailable for whatever reason, the four functions would be unavailable as well.
    • Create separate flask applications, Addition, Subtraction, Multiplication and Division. Each application will consist of a class which inherits the Resource class of flask_restful module.
    • Define a GET method within the class with necessary parameters. An example of this class with the method defined can be found here and here
    • Use the add_resource function to add the class as a resource and define the API endpoint. Make sure to also mention the type of the parameters in the endpoint. Example: api.add_resource(<class-name>, '/<int:argument0>/<int:argument1>')
    • Update the Docker-compose.yaml to recognize the newly added flask applications as separate services. The Docker-compose.yaml lets you define the port number and network alias that will be used by landing-service to communicate within the entire architecture - Test the new microservices-based application.

    2.1. Add three more services

    • Proceed to add three more services that perform a certain function. You will also have to make changes to the frontend defined in index.html to make these functions available. Mentioned below are possible functions you could implement but feel free to add your own.
    • gcd: Takes two numbers as arguments and returns their Greatest Common Divisor
    • lcm: Takes two numbers as arguments and returns their Least Common Multiple
    • modulus: Returns the remainder of two numbers after division. Reference
    • exponent: Returns the result of the arithmetic operation ab
    • greater_than: Return True if the first value is greater than the second else ```False``
    • less_than: return True if the first value is less than the second else False
    • equal: return True if the first value is equal to the second else False

Microservices-based architecture diagram

The diagram only shows the services already defined within the microservice architecture for visualization purposes. You still need to add services of your own.

Miscellaneous

  • Directory structure of additional arithmetic microservices you will be adding:
โ”œโ”€โ”€ <name of the service>
โ”‚   โ”œโ”€โ”€ Dockerfile           # same as landing/Dockerfile
โ”‚   โ”œโ”€โ”€ app
โ”‚   โ”‚   โ”œโ”€โ”€ app.py           # TODO: by yourself
โ”‚   โ”‚   โ””โ”€โ”€ requirements.txt # same as landing/app/requirements.txt
โ”‚   โ”‚  

cc-project's People

Contributors

siddhant1309 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.