This project is a RESTful API built with FastAPI for managing a book collection. It provides comprehensive CRUD (Create, Read, Update, Delete) operations for books with proper error handling, input validation, and documentation. The project includes a Continuous Integration (CI) and Continuous Deployment (CD) pipeline and is served using Nginx as a reverse proxy.
- 📚 Book management (CRUD operations)
- ✅ Input validation using Pydantic models
- 🔍 Enum-based genre classification
- 🧪 Complete test coverage
- 📝 API documentation (auto-generated by FastAPI)
- 🔒 CORS middleware enabled
- 🚀 CI pipeline for automated testing
- 🛠️ CD pipeline for automated deployment
- 🔧 Served using Nginx as a reverse proxy
fastapi-book-project/
fastapi-book-project/
├── api/
│ ├── db/
│ │ ├── __init__.py
│ │ └── schemas.py # Data models and in-memory database
│ ├── routes/
│ │ ├── __init__.py
│ │ └── books.py # Book route handlers
│ └── router.py # API router configuration
├── core/
│ ├── __init__.py
│ └── config.py # Application settings
├── tests/
│ ├── __init__.py
│ └── test_books.py # API endpoint tests
├── .github/
│ └── workflows/
│ ├── ci.yml # Continuous Integration workflow
│ └── cd.yml # Continuous Deployment workflow
├── nginx/
│ └── nginx.conf # Nginx configuration file
├── main.py # Application entry point
├── requirements.txt # Project dependencies
└── README.md
- Python 3.12
- FastAPI
- Pydantic
- pytest
- uvicorn
- GitHub Actions (for CI/CD)
- Nginx
- Clone the repository:
git clone https://github.com/LivingHopeDev/fastapi-book-project.git
cd fastapi-book-project- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Start the server:
uvicorn main:app --reload- Access the API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
live url
GET /api/v1/books/- Get all booksGET /api/v1/books/{book_id}- Get a specific bookPOST /api/v1/books/- Create a new bookPUT /api/v1/books/{book_id}- Update a bookDELETE /api/v1/books/{book_id}- Delete a book
GET /healthcheck- Check API status
{
"id": 1,
"title": "Book Title",
"author": "Author Name",
"publication_year": 2024,
"genre": "Fantasy"
}Available genres:
- Science Fiction
- Fantasy
- Horror
- Mystery
- Romance
- Thriller
-
File: .github/workflows/ci.yml
-
Trigger: Runs on pull requests to the main branch.
-
Actions:
-
Set up Python environment.
-
Install dependencies.
-
Run pytest to execute all tests.
-
Fail the workflow if any tests fail.
pytest-
File: .github/workflows/cd.yml
-
Trigger: Runs on merging a pull request to the main branch.
-
Actions:
-
Deploy the FastAPI application with the latest changes.
-
Restart the server to apply updates.
- Install Nginx (if not installed):
sudo apt update
sudo apt install nginx
- Configure Nginx:
Create or update the nginx/nginx.conf file with the following:
server {
listen 80;
server_name your_domain_or_ip;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
- Enable the Configuration and Restart Nginx:
sudo ln -s /etc/nginx/sites-available/nginx.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
The API includes proper error handling for:
- Non-existent books
- Invalid book IDs
- Invalid genre types
- Malformed requests
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit changes (
git commit -m 'Add AmazingFeature') - Push to branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For support, please open an issue in the GitHub repository.