A simple User RESTful API developed using Flask(python) and PostgresSQL, using a clean architecture.
Python3.10
Poetry1.2.1
PostgresSQL >= 14
Use the script setup_dev_env.sh to setup a virtual environment and activate it on your working terminal, then it will install dependencies using poetry and setup the pre-commit pipeline that will check code integrity and format using pylint, isort and black.
To use the script run
source setup_dev_env.sh
copy the .env.dist to .env
cp .env.dist .env
and add the postgressql uri
Once dependencies installed and PostgresSQL database running you can run the app using in normal mode
flask run
or in debug mode
flask --debug run
When using this method the app will be accessible using http://localhost:5000
If you do not want to bother with dependencies and database issues you can build and run the app directly using docker
docker compose up --build
When using this method the app will be accessible using http://localhost:8080 (nginx http server)
PS: If you are in production environment use the docker compose methode after setting the variable inside .env to "production" so that it uses a dedicated WSGI server(gunicorn) best suited for production deployement.
Tests !!!!!!!!!!
Even though I have designed this API to be easy to test, I think some modification maybe needed to implement unit tests.
Add roles for user. Add creation and update date in model to keep history of changes. Add a JWT token creation and verification to restrict access to the api.
You can run the postgres container independently if you don't wont to install it locally
If you run the app using docker you need to use the name of the postgres container as the host
You may encounter sometime problems when running postgres due to some malfunctioning that keeps the port 5432 in use you can run these commands to tackle this issues.
sudo lsof -i:5432 # copy the process id
sudo kill -9 process_id
After installing:
Initialize a database storage area on disk :
sudo mkdir /usr/local/pgsql/data
sudo chown -v postgres /usr/local/pgsql
add these lines as sudoer to /var/lib/postgresql/.bash_profile to be able to use postgres command line in any user terminal:
PATH=/usr/local/pgsql/bin:$PATH
export PATH
PATH=/usr/lib/postgresql/{version}/bin:$PATH
export PATH
then run
sudo su - postgres
initdb -D /usr/local/pgsql/data
Running Server:
from your user
sudo su - postgres -c 'pg_ctl start -D /usr/local/pgsql/data -l logfile'
from postgres user:
pg_ctl start -D /usr/local/pgsql/data -l logfile
Shutting down the server :
from your user
sudo su - postgres -c 'pg_ctl stop -D /usr/local/pgsql/data'
#or
sudo kill -INT `sudo head -1 /usr/local/pgsql/data/postmaster.pid`
from postgres user:
pg_ctl -D stop
#or
kill -INT `head -1 /usr/local/pgsql/data/postmaster.pid`