This base project contains a scaffold for building a Flask API with the following features:
- Logging
- Testing
- Caching
- Containerization
- Linting verification
- CI/CD Integration
- Logging: Configured to output logs to both the terminal and a log file.
- Testing: Uses
pytest
for testing, with fixtures and examples. - Caching: Configured with Flask-Caching for efficient data handling.
- Containerization: Ready to be containerized with Docker.
- Linting: Integrated with
flake8
for code style checking. - CI/CD: Setup for continuous integration and deployment on AWS.
-
Set the local Python version:
pyenv local 3.12
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
source ./venv/bin/activate
-
Install the required dependencies:
pip install -r requirements.txt
-
Run the application:
gunicorn --bind 0.0.0.0:8080 app:app
If pytest
does not recognize the src
folder as a module, use the following command to set the PYTHONPATH
:
export PYTHONPATH=$(pwd)
Run the tests with:
pytest
Check the code style with flake8
:
flake8 .
To run the application using Gunicorn:
gunicorn --bind 0.0.0.0:8000 app:app
To build and run the Docker container:
docker build -t template-api:v1 .
docker run -p 8000:8000 --env-file .env template-api:v1
For continuous integration and deployment, we utilize GitHub Actions. Our setup includes refreshing the AWS ECR image and triggering AWS App Runner to refresh its service source.
- AWS_ACCESS_KEY_ID
- AWS_ACCOUNT_ID
- AWS_REGION
- AWS_SECRET_ACCESS_KEY
To update the libraries requirements, run:
pip freeze > requirements.txt