Code Monkey home page Code Monkey logo

flask-mongodb-app's Introduction

GitHub issues GitHub forks GitHub stars GitHub license Docker Image CI

Python-Flask-MongoDB Application

*This project(with documentation) was created back in a while in 10 days for an assignment. Right now I'm not maintaining this project anymore. 🙏

Heads-up before you use/look into the code⚠️

  • I didn't know anything about MonogoDB or Flask framework at time I started the project.
  • I'm not a web devloper.

Deploy on Kubernetes

$ git clone https://github.com/veerendra2/python-flask-mongodb-app.git
$ cd python-flask-mongodb-app/
$ kubectl create -f kubernetes_deployment.yml

Docker Compose

$ git clone https://github.com/veerendra2/python-flask-mongodb-app.git
$ cd python-flask-mongodb-app/
$ docker-compose up -d

Vagrant

$ git clone https://github.com/veerendra2/python-flask-mongodb-app.git
$ cd python-flask-mongodb-app/
$ vagrant up

Introduction

A simple application that stores and retrieve json data via HTTP API. The endpoints list given below

Name HTTP Method Endpoint Description
List GET /configs Lists available configuration in the server
Create POST /configs Create/stores configuration in the server
Get GET /configs/<config_name> Fetches configuration details for a specific configuration
Update PUT/PATCH /configs/<config_name> Updates configuration for specific configuration
Delete DELETE /configs/<config_name> Delete a configuration
Get GET /search?name=<config_name>&data.<key>.=<value> Search configuration

Configuration(Json Data) Schema Structure

[
 {
   "name": <unique_name>,
   "data": [
      {
        <key>: <value>
        <ke2>:<value>
      }
    ]
 }
]

Schema Example

[
  {
    "name": "Mckinney",
    "data": [
      {
        "id": 4608,
        "app_name": "cupidatat",
        "isActive": true
      },
      {
        "id": 2678,
        "app_name": "tempor",
        "isActive": true
      },
      {
        "id": 4114,
        "app_name": "adipisicing",
        "isActive": true
      }
    ]
  }
]

Design

Backend

As we can see, we want to develop application that stores JSON data. We definitely can’t store data in files which we all knew "File System Storage" lacks ACID properties especially when we are serving data to multiple user. So, the best way to store data is storing it in DBMS.

That brings up another question, what kind of database should solve our problem. If we look at our goal again, we want to store a JSON (configuration) which the size of JSON document is unknown or don’t want to put constraint on users to send only certain number of keys values pairs. But, if we choose to use traditional RDBMS programs, we have to define schema/table in DB. So, clearly we don’t want use SQL DBMS software like MySQL, Progress (NOTE: Recent versions of these software can supports NoSQL). One another problem or extra work in coding is we have to code to get keys-values and write SQL to inject in tables.

By observing above problems, "NoSQL" got my attention which covers all drawbacks of RDBMS. I chose, "MongoDB" as a backend to our application to store JSON. MongoDB is popular NoSQL DBMS, supports JSON document storage, define constraints on data, support indexing and "schema less", etc.

Frontend

An API that stores/fetches data from/in DB. I chose Flask to implement API. Flask is a Python framework designed to easy to use and light weight. After reviewing pro and cons of difference technologies to design backend and frontend, I chose below technologies/DB/framework with respective to their roles.

Role Framework
Frontend Flask
Backend MongoDB

Implementation

The API was implemented in Python 2.7 flask framework which communicates to MongoDB to store/fetch data accordingly with help of official python module from MongoDB “pymongo”.

The API has capability of identify HTTP methods and performs actions according to it. The API validates the user input and sends response back to user as HTTP status code and text message. If the user’s input is valid, the API store JSON document in DB (or fetch data from DB).

In order to avoid malicious requests for “Query” endpoint which API might struck if user pass malicious urls, the application does regular expression on input query string and expected query string.

Deployment

Our goal is to deploy application on Kubernetes cluster. Before that, we should do some tweaking to our application. As flask docs says "Flask’s built-in server is not suitable for production as it doesn’t scale well". So, we should deploy flask application to WSGI server. We will use fully reliable HTTP server Apache to serve our application to end user with help of “mod_wsgi” which is an Apache module that implements a WSGI compliant interface for hosting Python based web applications on top of the Apache web server.

Building Docker images

Before deploying our application on kubernetes, we need to build docker images and push docker image to hosting repository. Since we already identified frontend and backend, we can build a docker image for frontend contains Apache webserver + flask application (HTTP API) and another for mongoDB. We will use quay.io to store our docker images (private repo).

Deployment on Kubernetes

Once docker image push to registry is complete. We can deploy application on K8s by using deployment manifest files that I provided along with this doc.

  1. Since we are going to deploy on “minikube” which is only for development. We are not using PV, PVC for MongoDB (PV and PVC are highly recommended in production). I have defined “EmptyDir” config which persistence of data is lifetime of pod.
  2. As we can see in manifest files, we are mounting/injecting configuration via “secrets”/environmental variables.

*Check tests directory for more info.

flask-mongodb-app's People

Contributors

veerendra2 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

flask-mongodb-app's Issues

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.