IE-BestBank / BestBank-be

0 stars 0 forks source link

IE Bank backend

My new other commit

Overview

This is the repository for the backend code of the IE Bank web app

IE Bank app logical view

Requirements

This source code works under the following technologies:

Recommended tutorials

Configure your local environment

Install Prerequisites

Set up your local environment with VSCode

  1. Create the virtual environment. In VS Code, open the Command Palette (View > Command Palette or [Ctrl+Shift+P]). Then select the Python: Create Environment command to create a virtual environment in your workspace. Select venv and then the Python environment you want to use to create it.

Python - Create Environment on VSCode

  1. Activate your virtual environment. After your virtual environment creation has been completed, run Terminal: Create New Terminal [Ctrl+Shift+ `]) from the Command Palette, which creates a terminal and automatically activates the virtual environment by running its activation script.
  2. Install Flask in the virtual environment. In the VS Code Terminal, run the following command:
$ python -m pip install flask

You now have a self-contained environment ready for writing Flask code. VS Code activates the environment automatically when you use Terminal: Create New Terminal.

Run and debug the backend locally

  1. Debug the app locally. In VS Code, switch to the Run and Debug view (using the left-side activity bar or Ctrl + Shift + D). You may see the message "To customize Run and Debug create a launch.json file". VS Code can create that for you if you click on the create a launch.json file link:

VS Code create launch file

  1. Set and run debug configuration for Flask. Select the link and VS Code will prompt for a debug configuration. Select Python and the Flask from the dropdown and VS Code will populate a new launch.json file in the .vscode folder with a Flask run configuration. The launch.json file contains a number of debugging configurations, each of which is a separate JSON object within the configuration array. Edit the .vscode/launch.json configuration file with the snippet below and save (Ctrl+S).
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "IE Bank Backend",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "app.py",
                "FLASK_DEBUG": "1",
                "ENV": "local",
                "DBUSER":"",
                "DBPASS":"",
                "DBHOST":"",
                "DBNAME":""
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true,
            "justMyCode": true
        }
    ]
}
  1. Run and Debug your application locally. Set a breakpoint in any of the .py files. Go to the Debug view, select the 'Python: Flask' configuration, then press F5 or click the green play button.

Configuration variables

Learn more: Flask configuration handling

When running the application in VSCode, the .vscode/launch.json file well set environment variables as configured in the env section:

 "env": {
                "FLASK_APP": "app.py",
                "FLASK_DEBUG": "1",
                "ENV": "local"
            },

This python app will read the environment variables in the iebank_api\__init__.py file. This file will load different variables depending on the value of the ENV variable read in the running machine.

# Select environment based on the ENV environment variable
if os.getenv('ENV') == 'local':
    print("Running in local mode")
    app.config.from_object('config.LocalConfig')
elif os.getenv('ENV') == 'dev':
    print("Running in development mode")
    app.config.from_object('config.DevelopmentConfig')
elif os.getenv('ENV') == 'ghci':
    print("Running in github mode")
    app.config.from_object('config.GithubCIConfig')
else:
    print("Running in production mode")
    app.config.from_object('config.ProductionConfig')

The configuration that will be loaded is defined in the config.py file.

class LocalConfig(Config):
    basedir = os.path.abspath(os.path.dirname(__file__))
    SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir,'.db', 'local.db')
    DEBUG = True

class GithubCIConfig(Config):
    SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db'
    DEBUG = True

class DevelopmentConfig(Config):
    SQLALCHEMY_DATABASE_URI = 'postgresql://{dbuser}:{dbpass}@{dbhost}/{dbname}'.format(
    dbuser=os.getenv('DBUSER'),
    dbpass=os.getenv('DBPASS'),
    dbhost=os.getenv('DBHOST'),
    dbname=os.getenv('DBNAME')
    )
    DEBUG = True

Database Migrations

If you have ran the backend before on your computer, most likely you will need to update the database schema instead of creating a new one. To handle these migrations in an efficient way, we set up migrations using Flask-Migrate to take care of this without running sql queries directly. To run the migrations:

  1. Start your virtual environenmt: source .venv/bin/activate on mac, ./.venv/Scripts/activate on windows. Note: that ".venv" is the name of your virtual environment.
  2. Install the requirements: pip install -r requirements.txt
  3. Run flask db init to initialize the db migrations setup. After running this command, a "migrations" folder should be created.
  4. Now, anytime the database schema is updated and needs a migration, run flask db migrate -m "<migration-message>"

Continuos Delivery

Learn more:

The file .github/workflows/ie-bank-backend.yml contains the configuration for the CI/CD pipeline

GitHub secrets

The workflow uses the following GitHub secrets:

Secret name Description Learn more
AZURE_CREDENTIALS Azure credentials to authenticate to Azure via Service Principal Use the Azure login action with a service principal secret