davidh83110 / flask-app

Flask App and Helm Chart

Home Page:https://davidh83110.github.io/flask-app/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Flask Application

badge-python.svg badge-ghaction.svg security: bandit

This is a Flak RESTfulAPI. Please run the application and vivsit /swagger for API documentations.

🐳 Docker Registry is here -- davidh83110/flask-app

Table of Content

Data Migration

The /history API will require some history data in place so that it can return to user, so we have scripts/migrations.py here for helping to inject initial data. The script will be executed when we run scripts/entrypoint.sh in Docker environment.

If you are not in Docker environment, please run it manually before start the application.

cd scripts
python3 migrations.py

How to Run

This Page will only focus on Flask application, if you need any information about Helm or run in Kubernetes, please visit deploy/README.md.

Quick Run

Docker Compose

docker-compose.png

docker-compose up --build -d
  • Hardcode the password in the docker-compose.yaml, it can also use docker-compose secret from file.

Kind and Helm Chart

kind.png kind-finish.png

cd ./deploy
sh ./startup.sh

Prerequisites

  • Python >= 3.10
  • Pipenv
  • Docker
  • Redis Server

Setup Environment

  • Install Pipenv
    python -m pip install --upgrade pip
    pip install pipenv --no-cache-dir
    
  • Install Dependencies
    pipenv install --system --deploy --ignore-pipfile
    
  • Start Redis Locally
     docker run -p 6379:6379 -itd redis:alpine                  
    

Start Flask Application

In Gunicorn without Docker

gunicorn --bind 0.0.0.0:3000 \
            --access-logfile flask-app-gunicorn.log \
            --error-logfile flask-app-gunicron-error.log \
            --log-level debug \
            wsgi:app

In Gunicorn with Docker

  • Build Docker Image
     export APP_VERSION=0.0.1
     docker build --build-arg APP_VERSION=$APP_VERSION --no-cache -t flask-app .
    
  • Run Docker Container
    docker run -p 3000:3000 -it -e REDIS_HOST='172.17.0.1' flask-app
    
    • 172.17.0.1 is Docker default host network, I am assuming you have a Redis running in Docker.

Running in local with Kind and Helm Chart

please visit deploy/README.md.

Check Result

After the applciation is started, you can always vivist the pages on browser -- http://localhost:3000 .

Development

Run under Pipenv Shell

pipenv install --deploy
pipenv shell
python3 main.py

or

pipenv install --deploy
pipenv run python3 main.py

Install PYPI packages with Pipenv

pipenv install flask ...
pipenv update

Run Unittest

pipenv shell
pytest

CI/CD

I use Github Actions to implement a simple CI/CD pipeline, which includes unittest, SAST evulation, docker build and push, and helm package and upload.

  • Please visit ci-build.yaml.
  • SAST tool - bandit
  • We could possibly optimize the pipeline to be faster and comment some messages on the Pull Request, but I just ignore those things since it is a demo only.

The artifacts of this CI pipeline are --

Versioning

I use a VERSION file to control the Application version, which will be the APP_VERSION when we build the Docker Image. But in real environment, I think we can do it with tags triggering, auto increament, or integrate with Jira/Github Project.

Logging

logging.png The access and error logs of Gunicorn/Flask will be shown as JSON format, it is better for most of the log streamer like Filebeat.

About

Flask App and Helm Chart

https://davidh83110.github.io/flask-app/


Languages

Language:Python 70.9%Language:Shell 14.5%Language:Smarty 11.0%Language:Dockerfile 3.6%