Hello World REST API deployed with Python Flask and SQLAlchemy as ORM over SQLite database.
- Python 3.8.x
- Flask web/REST framework
- marshmallow-sqlalchemy, an integration of marshmallow serialization library and SQLAlchemy ORM with
- Psycopg as driver for PostgreSQL database
- Gunicorn WSGI HTTP Server
Use your PostgreSQL instance or simply use Docker locally (specific internal network for the database instance):
docker network create db_net;
docker container run --name pg \
-e POSTGRES_PASSWORD=pass \
-d -p 5432:5432 \
--network db_net \
-v ${PWD}/data:/var/lib/postgresql/data \
postgres:12.2
Create app database using Docker psql
, connecting to PostgreSQL previous
instance:
docker container run \
--network db_net \
-it postgres:12.2 \
psql -h pg -U postgres -d postgres -c 'CREATE DATABASE fshw;'
Create virtual env:
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip wheel setuptools
Install fshw
:
deactivate; source .venv/bin/activate
pip install .
- With premade script:
deactivate; source .venv/bin/activate
DATABASE_URL='postgresql+psycopg2://postgres:pass@localhost/fshw' run_fshw
- With gunicorn binary:
deactivate; source .venv/bin/activate
DATABASE_URL='postgresql+psycopg2://postgres:pass@localhost/fshw' \
gunicorn fshw.fshw:app
- With flask binary:
deactivate; source .venv/bin/activate
DATABASE_URL='postgresql+psycopg2://postgres:pass@localhost/fshw' \
FLASK_APP=fshw.fshw \
flask run
- With flask module:
deactivate; source .venv/bin/activate
DATABASE_URL='postgresql+psycopg2://postgres:pass@localhost/fshw' \
FLASK_APP=fshw.fshw \
python -m flask run
This will create a local server listening on default port (8000 for gunicorn, 5000 for Flask). Test it with:
- For gunicorn (previous options 1 and 2)
curl -i -X GET http://localhost:8000/hello
- For Flask (previous options 3 and 4)
curl -i -X GET http://localhost:5000/hello