A production-ready FastAPI application designed for experimenting with modern DevOps tools and practices. This project serves as a comprehensive learning and testing platform for DevOps methodologies in a Python environment, featuring authentication, database integration, API versioning, and containerization.
Project 4 Tutorial: Watch on YouTube
- DevOps Foundation: Establish a robust base for testing and implementing DevOps practices
- Tool Integration: Seamlessly integrate various DevOps tools (Docker, Terraform, CI/CD)
- Learning Platform: Foster hands-on learning for DevOps within a Python/FastAPI environment
- Best Practices: Demonstrate modern software development and deployment patterns
- RESTful API: Built with FastAPI for high performance and automatic OpenAPI documentation
- JWT Authentication: Secure OAuth2 with JWT tokens for user authentication
- CRUD Operations: Complete Create, Read, Update operations for items resource
- API Versioning: Structured v1 API endpoints for backward compatibility
- Database Integration: PostgreSQL database with SQLAlchemy ORM
- CORS Support: Configured Cross-Origin Resource Sharing for frontend integration
- Containerization: Docker and Docker Compose configurations
- Infrastructure as Code: Terraform configuration for cloud deployment
- Testing Suite: Comprehensive pytest-based test coverage
- Code Quality: Integrated linting (flake8, pylint) and formatting (autopep8)
- Environment Configuration: Environment variable based configuration management
POST /token- Login with username/password to receive JWT access tokenGET /users/me/- Get current authenticated user profile
GET /items/- List items with pagination (skip, limit parameters)POST /items/- Create a new itemPATCH /items/{id}- Update an existing item
GET /docs- Interactive Swagger UI API documentation (auto-generated)GET /redoc- ReDoc API documentation (auto-generated)
api-python-project-devops-fast-api21/
βββ app/
β βββ config/ # Configuration files (database, settings)
β βββ models/ # Database models and Pydantic schemas
β βββ repositories/ # Data access layer (repository pattern)
β βββ routers/ # API route handlers
β β βββ v1/ # Version 1 API endpoints
β βββ security/ # Security utilities
β βββ services/ # Business logic layer
βββ tests/ # Test suite
β βββ test_auth.py # Authentication tests
β βββ test_items.py # Items endpoint tests
βββ main.py # Application entry point
βββ call_db.py # Database connection utilities
βββ Dockerfile # Docker image configuration
βββ docker-compose.yml # Multi-container Docker setup
βββ main.tf # Terraform infrastructure configuration
βββ requirements.txt # Python dependencies
βββ README.md # This file
- FastAPI (0.109.0) - Modern, fast web framework for building APIs
- Uvicorn (0.25.0) - ASGI server for running FastAPI
- Pydantic (2.5.3) - Data validation using Python type annotations
- SQLAlchemy (2.0.25) - SQL toolkit and ORM
- PostgreSQL - Primary database (via psycopg2-binary)
- python-jose (3.3.0) - JWT token creation and validation
- passlib (1.7.4) & bcrypt (4.1.2) - Password hashing
- cryptography (41.0.7) - Cryptographic recipes
- pytest (7.4.4) - Testing framework
- pytest-html (4.1.1) - HTML test reports
- flake8 (7.0.0) - Code linting
- pylint (3.0.3) - Code analysis
- autopep8 (2.0.4) - Code formatting
- Docker & Docker Compose - Containerization
- Terraform - Infrastructure as Code
- Gunicorn (20.1.0) - WSGI HTTP server for production
Ensure you have the following installed:
- Python 3.x (3.9 or higher recommended)
- pip (Python package manager)
- Docker and Docker Compose (for containerized deployment)
- PostgreSQL (if running locally without Docker)
-
Clone the repository:
git clone https://github.com/maxiplux/api-python-project-devops-fast-api.git cd api-python-project-devops-fast-api21 -
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
Create a
.envfile or export the following variables:DB_USERNAME=postgres DB_PASSWORD=postgres DB_HOST=localhost DB_NAME=postgres SECRET_KEY=your-secret-key-here ACCESS_TOKEN_EXPIRE_MINUTES=30
Run the FastAPI application with hot-reload:
uvicorn main:app --reloadThe API will be available at:
- API: http://localhost:8000
- Interactive Docs: http://localhost:8000/docs
- Alternative Docs: http://localhost:8000/redoc
Build and run the Docker container:
docker build -t fastapi-devops .
docker run -p 8000:8000 \
-e DB_USERNAME=your_username \
-e DB_PASSWORD=your_password \
-e DB_HOST=your_host \
-e DB_NAME=your_db_name \
fastapi-devopsRun the complete stack with PostgreSQL database:
docker-compose up -dThe application will be available at:
- API: http://localhost:8080
- PostgreSQL: localhost:5432
To stop the services:
docker-compose downRun the test suite using pytest:
# Run all tests
pytest
# Run with coverage report
pytest --cov=app
# Run with HTML report
pytest --html=report.html
# Run specific test file
pytest tests/test_auth.py-
Obtain an access token:
curl -X POST "http://localhost:8000/token" \ -H "Content-Type: application/json" \ -d '{"username":"testuser","password":"testpass"}'
-
Use the token for authenticated requests:
curl -X GET "http://localhost:8000/items/" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN"
| Variable | Description | Default | Required |
|---|---|---|---|
DB_USERNAME |
PostgreSQL username | postgres | Yes |
DB_PASSWORD |
PostgreSQL password | postgres | Yes |
DB_HOST |
Database host | localhost | Yes |
DB_NAME |
Database name | postgres | Yes |
SECRET_KEY |
JWT secret key | - | Yes |
ACCESS_TOKEN_EXPIRE_MINUTES |
Token expiration time | 30 | No |
- Verify PostgreSQL is running:
docker-compose ps - Check environment variables are correctly set
- Ensure the database exists and user has proper permissions
# Check what's using port 8000
netstat -ano | findstr :8000 # Windows
lsof -i :8000 # Linux/Mac
# Kill the process or use a different port
uvicorn main:app --reload --port 8001# Reinstall dependencies
pip install -r requirements.txt --force-reinstallContributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Follow PEP 8 style guidelines
- Write tests for new features
- Update documentation as needed
- Ensure all tests pass before submitting PR
This project is licensed under the MIT License - see the LICENSE file for details.
Maintainer: maxiplux
Email: maxiplux@gmail.com
GitHub: @maxiplux
β If you find this project helpful, please consider giving it a star!