Welcome to FastBook, a robust REST API designed for managing a modern book collection and review system. Built with Python, FastAPI, and PostgreSQL, this backend service provides a fast, scalable, and efficient foundation for a book management application. It follows modern API design principles with comprehensive authentication, asynchronous operations, and clean separation of concerns, making it highly maintainable and performant.
- 🔐 JWT Authentication — Secure endpoints using JSON Web Tokens (JWT) with refresh token support, ensuring that only authenticated users can access protected resources.
- 🏗️ Clean Architecture — Organized into distinct layers (Routes, Services, Models) for a clear separation of concerns, making the codebase easy to understand, test, and scale.
- 📦 Full CRUD Operations — Comprehensive Create, Read, Update, and Delete functionality for all core entities:
- Users: Complete user management with secure password hashing, email verification, and role-based access control.
- Books: Manage book catalog with detailed information and user associations.
- Reviews: Allow users to rate and review books with comment functionality.
- Tags: Organize books with tagging system for better categorization.
- 📧 Email System — Integrated email functionality with Celery background tasks for:
- Account verification emails
- Password reset notifications
- Welcome messages
- 🔄 Background Tasks — Asynchronous email processing using Celery and Redis for improved performance.
- 🛡️ Request Validation — Built-in validation using Pydantic models to ensure data integrity and type safety.
- 🐘 PostgreSQL Integration — Utilizes PostgreSQL with SQLModel (SQLAlchemy) for robust and reliable async data storage.
- 🚀 High Performance — Built on FastAPI for automatic API documentation, async support, and blazing-fast performance.
- 🔍 Token Blacklisting — Redis-based token blacklisting for secure logout functionality.
- 📊 Database Migrations — Alembic integration for database schema versioning and migrations.
- ⚙️ Centralized Configuration — Manages all environment-specific settings securely through environment variables.
- User registers by sending their details to the
/signup
endpoint and receives an email verification link. - User verifies email by clicking the verification link and can then authenticate via
/login
to receive JWT tokens. - The client includes the JWT as a Bearer Token in the
Authorization
header for all subsequent requests to protected endpoints. - JWT Middleware intercepts and validates tokens, checking against Redis blacklist for revoked tokens.
- The Routes layer receives requests, validates data using Pydantic schemas, and calls appropriate Service layer methods.
- The Service layer executes core business logic, handles exceptions, and coordinates with the Models layer.
- The Models layer manages database interactions using SQLModel and async SQLAlchemy sessions.
- Background tasks handle email sending asynchronously via Celery workers.
- A structured JSON response with comprehensive error handling is returned to the client.
- 🐍 Python 3.12+
- ⚡ FastAPI (Modern Web Framework)
- 🐘 PostgreSQL (Database)
- 🔗 SQLModel (ORM with SQLAlchemy)
- 🔐 python-jose (JWT Implementation)
- 🛡️ passlib (Password Hashing with bcrypt)
- ✅ Pydantic (Data Validation)
- 📧 fastapi-mail (Email Integration)
- 🔄 Celery (Background Task Processing)
- 🗄️ Redis (Token Blacklisting & Celery Broker)
- 🗃️ Alembic (Database Migrations)
- 🧪 pytest (Testing Framework)
- 🌐 Python Backend : View Code
Follow these steps to get FastBook up and running on your local machine.
- Python (version 3.12 or higher)
- PostgreSQL
- Redis (for token blacklisting and Celery)
- A tool to interact with your database (e.g., TablePlus, DBeaver, or pgAdmin)
-
Clone the repository:
git clone https://github.com/LouisFernando1204/fastbook-backend.git cd fastbook-backend
-
Create and activate virtual environment:
python -m venv env source env/bin/activate # On Windows: env\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
- Create a
.env
file in the root directory. - Add the following configuration variables:
# Database Configuration DATABASE_URL="postgresql+asyncpg://username@localhost:5432/fastblog_db" # JWT Configuration JWT_SECRET="your_super_secret_jwt_key_here" JWT_ALGORITHM="HS256" # Redis Configuration REDIS_HORT="localhost" REDIS_PORT=6379 REDIS_URL="redis://localhost:6379/0" # Email Configuration MAIL_USERNAME=[email protected] MAIL_PASSWORD=your_app_password MAIL_SERVER=smtp.gmail.com MAIL_PORT=587 MAIL_FROM=[email protected] MAIL_FROM_NAME=FastBook Backend # Application Configuration DOMAIN=localhost:8000 # PostgreSQL Configuration (Optional) POSTGRES_USER= POSTGRES_PASSWORD= POSTGRES_DB=
- Create a
-
Set up the database:
- Start your PostgreSQL server.
- Create a new database named
fastbook_db
(or as specified in your DATABASE_URL). - Run database migrations:
alembic upgrade head
-
Start Redis server:
redis-server
-
Start Celery worker (in a separate terminal):
celery -A src.celery_tasks worker --loglevel=info
-
Run the application:
fastapi dev src/
The server should now be running on
http://localhost:8000
. -
Access API Documentation:
- Swagger UI:
http://localhost:8000/docs
- ReDoc:
http://localhost:8000/redoc
- Swagger UI:
POST /api/v1/auth/signup
- Register new userPOST /api/v1/auth/login
- User loginGET /api/v1/auth/verify/{token}
- Verify emailPOST /api/v1/auth/refresh-token
- Refresh access tokenPOST /api/v1/auth/logout
- User logoutGET /api/v1/auth/me
- Get current user profilePOST /api/v1/auth/password-reset-request
- Request password resetPOST /api/v1/auth/password-reset-confirm/{token}
- Confirm password reset
GET /api/v1/books/
- Get all booksPOST /api/v1/books/
- Create new bookGET /api/v1/books/{book_uid}
- Get book by IDPATCH /api/v1/books/{book_uid}
- Update bookDELETE /api/v1/books/{book_uid}
- Delete book
POST /api/v1/books/{book_uid}/reviews
- Add review to bookDELETE /api/v1/books/{book_uid}/reviews/{review_uid}
- Delete review
GET /api/v1/tags/
- Get all tagsPOST /api/v1/tags/
- Create new tagPOST /api/v1/books/{book_uid}/tags
- Add tags to bookDELETE /api/v1/books/{book_uid}/tags/{tag_uid}
- Remove tag from book
pytest src/tests/ -v
- 🧑💻 Louis Fernando : @LouisFernando1204