My new other commit
This is the repository for the backend code of the IE Bank web app
This source code works under the following technologies:
C:\sqlite
). Add the C:\sqlite
directory to your PATH environment variable.sudo apt-get install sqlite3
.brew install sqlite3
.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.Ctrl
+Shift
+ `
]) from the Command Palette, which creates a terminal and automatically activates the virtual environment by running its activation script.$ 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
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: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
}
]
}
.py
files. Go to the Debug view, select the 'Python: Flask' configuration, then press F5 or click the green play button.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
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:
source .venv/bin/activate
on mac, ./.venv/Scripts/activate
on windows. Note: that ".venv" is the name of your virtual environment.pip install -r requirements.txt
flask db init
to initialize the db migrations setup. After running this command, a "migrations" folder should be created.flask db migrate -m "<migration-message>"
Learn more:
The file .github/workflows/ie-bank-backend.yml
contains the configuration for the CI/CD pipeline
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 |