Project template for Flask API.
Medium post: A useful template to help you spin up your next project and start building.
- Go live with a Continuous Deployment Pipeline using GitHub and Render's Infrastructure as Code. See sample IaC file here.
- Works with SQLAlchemy / Postgres DB out of the box.
- Uses mypy for static type checks.
There is a sample reference package generated from our cookiecutter which you can refer to in here: https://github.com/ardydedase/flask-postgres-api.
Deploy the Sample Repo built from this cookiecutter to Render.com:
- Check the deployed API:
<API URL>/healthcheck
- Ensure that you have deleted your resources from Render when you're done.
-
Install cookiecutter
pip install cookiecutter
-
Cookie cut the template.
cookiecutter git@github.com:ardydedase/cookiecutter-flask-postgres-api.git
-
Enter the values accordingly. Pick your project name.
-
Change the working directory to the generated folder, same name as the project slug.
cd <project_slug>
Use docker-compose
docker-compose up
Bring up the Postgres DB container
docker-compose up -d db
Install requirements.
mypy
takes some time to install
pip install -r requirements.txt
brew install postgresql
. Stackoverflow
Initialise environment variables. The .env
is used in docker-compose.yml
.
export FLASK_APP="src/main.py"
export POSTGRES_URL="127.0.0.1:5432"
export POSTGRES_DB="mydb"
export POSTGRES_USER="postgres"
export POSTGRES_PASSWORD="example"
Run migrations
chmod +x run-migrations.sh
./run-migrations.sh
Run flask
# initialise environment variables
flask run
py.test -vv
For production.
cd src && gunicorn main:app
Build command
pip install -r requirements.txt && ./run-migrations.sh
Run the app
cd src && gunicorn app:app