This project is a Library Management System that consists of two independent APIs:
- Frontend API: For user-facing operations like user enrollment, browsing books, and borrowing.
- Backend/Admin API: For administrative operations like adding/removing books, managing users, and handling borrowing information.
The system uses Flask for the web framework, MongoDB for data storage, Redis for handling events, and follows a microservices architecture with separate services for frontend and backend logic.
- Features
- Architecture
- Technologies
- Setup Instructions
- API Endpoints
- Running Unit Tests
- Event-Driven Approach
- License
- User Enrollment: Users can register in the library using their email, firstname and lastname.
- Browse Books:
- Users can list all available books.
- Users can get a single book by its ID.
- Users can filter books
- by publishers e.g Wiley, Apress, Manning
- by category e.g fiction, technology, science
- Borrow Books: Users can borrow available books by ID specifying how long they want it.
- Add Books: Admins can add books to the catalogue.
- Remove Books: Admins can remove books from the catalogue.
- List Users:
- Admins can list all registered users
- Admins can list all users with borrowed books and the books they have borrowed.
- List Unavailable Books: Admins can see books that are currently borrowed.
- User Enrollment Event: Publishes events when a user is enrolled.
- Book Borrowed Event: Publishes events when a book is borrowed.
- Book Added Event: Publishes events when a book is added.
- Book Removed Event: Publishes events when a book is removed.
The project is organized into the following components:
-
Frontend API:
- Handles user-facing functionalities.
- Manages user enrollment and book browsing.
- Interacts with Redis to communicate user events to the backend.
-
Backend API:
- Handles admin functionalities like adding/removing books and managing borrowed books.
- Publishes events to Redis for synchronizing with the frontend system.
-
MongoDB:
- Stores data related to users, books, and borrowing records.
-
Redis:
- Used for event-driven communication between the two APIs (frontend and backend).
- Python 3.x
- Flask: Lightweight web framework.
- MongoDB: NoSQL database to store user, book, and borrow data.
- Redis: Message broker to manage communication between microservices.
- Pytest: Testing framework to ensure code quality.
- Docker: For containerization of the API.
- Python 3.x
- MongoDB installed locally or via cloud (e.g., MongoDB Atlas)
- Redis installed locally or via cloud (e.g., Redis Cloud)
git clone https://github.com/ifatoki/libra.git
cd libra
You will need two separate virtual environments for the frontend and backend APIs.
# For Frontend API
cd frontend-api
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
# For Backend API
cd ../backend-api
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Create a .env
file in both the frontend and backend directories with the following variables:
# MongoDB connection string
MONGO_URI=mongodb://localhost:27017/library
# Redis connection
REDIS_HOST=localhost
REDIS_PORT=6379
To run the services locally:
cd frontend
source venv/bin/activate
flask run --host=0.0.0.0 --port=5000
cd backend
source venv/bin/activate
flask run --host=0.0.0.0 --port=5001
The Frontend API will run on port 5000
and the Backend API on port 5001
.
You can view details of the endpoints here https://documenter.getpostman.com/view/2602351/2sAXqtaLrS
The project uses pytest
to run unit tests. Ensure that pytest
is installed in your virtual environments.
cd frontend-api
source .venv/bin/activate
pytest
cd backend-api
source .venv/bin/activate
pytest
The system utilizes an event-driven architecture powered by Redis. Events such as user enrollment, book addition, book deletion, and book borrowing trigger notifications and updates across the microservices.
- User Enrollment: Publishes a
user_enrolled
event. - Book Added: Publishes a
book_added
event. - Book Removed: Publishes a
book_removed
event. - Book Borrowed: Publishes a
book_borrowed
event.
The Frontend API publishes events to a Redis channel (e.g., frontend_events
), and the Backend API subscribes to these events to keep the systems in sync.
This project is licensed under the MIT License - see the LICENSE file for details.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Make sure to update tests as appropriate.
- Author: Itunuloluwa Fatoki
- GitHub: ifatoki