Skip to content

Clean Architecture with Python using SQLAlchemy, PostgreSQL and Flask.

License

Notifications You must be signed in to change notification settings

wesleey/python-clean-architecture

Repository files navigation

Python Clean Architecture

Clean architecture, as defined by Robert C. Martin in his book Clean Architecture: A Craftsman's Guide to Software Structure and Design emphasizes the separation of concerns into distinct layers to promote modularity, testing, and code maintenance.

Installation

  1. Clone the repository to your local environment:
git clone https://github.com/wesleey/python-clean-architecture.git
  1. Navigate to the project directory:
cd python-clean-architecture
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # For Unix/macOS
venv\Scripts\activate     # For Windows
  1. Install dependencies using pip3:
pip3 install -r requirements.txt
  1. Install pre-commit hooks:
pre-commit install

Database Migrations

After installing and configuring the database, you can apply the necessary migrations using the following command:

alembic upgrade head

Usage

Run the In-Memory CLI

python cli_memory_process_handler.py

Run the In-Memory Flask API

python flask_memory_process_handler.py

Run the PostgreSQL Flask API

python flask_process_handler.py

Testing Flask APIs

curl -X POST -H "Content-Type: application/json" -d '{"first_name": "John", "last_name": "Doe", "email": "[email protected]"}' http://localhost:5000/v1/user/

Make sure the Flask server is running before making requests using cURL.

Commands

Docker

Starts containers in the background

docker compose up -d

Stops containers

docker compose down

Show all running containers

docker ps

Open shell in running container

docker exec -it <container_id> bash

Exit the shell

exit

PostgreSQL

Show all users

psql -U db_user -d db_name -c "SELECT * FROM users"

Delete all users

psql -U db_user -d db_name -c "DELETE FROM users"

Delete user by email

psql -U db_user -d db_name -c "DELETE FROM users WHERE email = '<user_email>'"

References

License

This project is licensed under the MIT License.