Skip to content

skatesham/fastapi-bigger-application

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

131 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FastAPI Car Shop ERP

Coverage: 78% Python 3.11+ FastAPI SQLAlchemy Pydantic

Professional REST API ERP system for car shop management built with FastAPI 0.133, SQLAlchemy 2.0, and modern Python patterns.


✨ Features

  • πŸš€ FastAPI 0.133.1 - Latest stable version with modern patterns
  • πŸ—„οΈ SQLAlchemy 2.0.47 - Modern ORM with async support
  • πŸ” Professional Security - JWT with core.security module
  • πŸ“Š Pydantic V2.12.5 - Modern data validation and serialization
  • πŸ—οΈ Clean Architecture - Professional structure with core modules
  • πŸ§ͺ Comprehensive Testing - Tests separated from application code
  • πŸ“ Auto Documentation - Swagger/OpenAPI with /docs
  • πŸ”§ Professional Tooling - Black, isort, mypy, pytest, coverage
  • 🎯 Dependency Injection - FastAPI Annotated patterns
  • πŸ“¦ Modern Packaging - pyproject.toml with professional setup
  • πŸ”’ Security Best Practices - passlib, bcrypt, secure JWT handling

πŸ› οΈ Tech Stack

  • Framework: FastAPI 0.133.1
  • Database: PostgreSQL with SQLAlchemy 2.0.47
  • Configuration: Pydantic Settings V2
  • Authentication: JWT with python-jose[cryptography]
  • Validation: Pydantic V2.12.5 + Pydantic Settings 2.13.1
  • Testing: pytest 9.0.2 + pytest-asyncio 1.3.0 + pytest-cov 7.0.0
  • Code Quality: Black 26.1.0, isort 8.0.0, flake8 7.3.0, mypy 1.19.1
  • Security: passlib[bcrypt] 1.7.4, python-multipart 0.0.6
  • Database Tools: psycopg2-binary 2.9.0, alembic 1.12.0
  • Documentation: Auto-generated Swagger/OpenAPI
  • Architecture: Clean API structure with v1 versioning

πŸ—οΈ Project Structure

fastapi-bigger-application/
β”œβ”€β”€ app/                    # Application source code
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ api/           # API layer (v1)
β”‚   β”‚   β”‚   β”œβ”€β”€ deps.py    # Dependencies injection
β”‚   β”‚   β”‚   β”œβ”€β”€ converters/ # Response converters
β”‚   β”‚   β”‚   └── v1/
β”‚   β”‚   β”‚       └── endpoints/
β”‚   β”‚   β”œβ”€β”€ core/          # Core modules
β”‚   β”‚   β”‚   β”œβ”€β”€ config.py  # Pydantic Settings
β”‚   β”‚   β”‚   β”œβ”€β”€ database.py # Database setup
β”‚   β”‚   β”‚   └── security.py # JWT & auth
β”‚   β”‚   β”œβ”€β”€ domain/        # Business logic
β”‚   β”‚   └── internal/      # Internal utilities
β”‚   └── main.py           # Application entry point
β”œβ”€β”€ tests/                # Test suite (external)
β”œβ”€β”€ Dockerfile            # Docker container configuration
β”œβ”€β”€ docker-compose.yml    # Docker Compose orchestration
β”œβ”€β”€ docker-dev.sh         # Interactive development script
β”œβ”€β”€ .dockerignore         # Docker build exclusions
β”œβ”€β”€ pyproject.toml       # Modern Python packaging
β”œβ”€β”€ requirements.txt     # Dependencies
β”œβ”€β”€ setup.cfg            # Flake8 configuration
β”œβ”€β”€ .coveragerc          # Coverage configuration
└── .env.example        # Environment template

πŸš€ Quick Start

🐳 Docker (Recommended - Automated)

The fastest way to get started is using Docker Compose:

# Clone and start everything automatically
git clone https://github.com/carshop/fastapi-erp.git
cd fastapi-erp
docker-compose up --build

πŸš€ Interactive Development Script

For an enhanced development experience, use our interactive script:

# Run the interactive development script
./docker-dev.sh

The script provides:

  • πŸš€ Auto-start services with health checks
  • πŸ“Š Service status monitoring
  • πŸ“‹ Live logs viewing
  • πŸ›‘ Clean stop/reset options
  • πŸ”— Quick access URLs
  • 🎨 Colored output and status indicators

That's it! πŸŽ‰ The application will be available at:

Services included:

  • FastAPI App (port 8000) - Main application
  • PostgreSQL (port 5432) - Database with persistent data
  • Adminer (port 9000) - Database management interface

Local Development

If you prefer to run locally:

Prerequisites

  • Python 3.11+
  • PostgreSQL 12+
  • Docker & Docker Compose (optional)

Installation

# Clone the repository
git clone https://github.com/carshop/fastapi-erp.git
cd fastapi-erp

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -e ".[dev]"

# Setup environment
cp .env.example .env
# Edit .env with your configuration

# Start the application
uvicorn app.main:app --reload

Environment Configuration

Create .env file based on .env.example:

# Database
DATABASE_URL=postgresql://user:password@localhost/dbname

# Security
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30

# Service Configuration
SERVICE_NAME=fastapi-car-shop-erp
SERVICE_VERSION=1.0.0
SERVICE_DESCRIPTION=Professional ERP system for car shop management
SERVICE_AUTHOR=Your Name

# Application
DEBUG=false
ENVIRONMENT=production

# CORS
ALLOWED_HOSTS=localhost,127.0.0.1,yourdomain.com

Note: All service information (name, version, description, author) is centralized in the configuration and automatically used by health/info endpoints.


πŸ“š API Documentation

Once running, access:

πŸ§ͺ Testing

# Run all tests
pytest

# Run with coverage
pytest --cov=app --cov-report=html

# Run specific test
pytest tests/test_jwt.py -v

πŸ”§ Development

Code Quality

# Format code
black app/ tests/
isort app/ tests/

# Lint
flake8 app/ tests/

# Type checking
mypy app/

# Security check
bandit -r app/

Database Migrations

# Create migration
alembic revision --autogenerate -m "Description"

# Apply migrations
alembic upgrade head

# Downgrade
alembic downgrade -1

πŸ“¦ Deployment

🐳 Docker (Recommended)

Quick Start - One Command Setup

# Clone and run everything automatically
git clone https://github.com/carshop/fastapi-erp.git
cd fastapi-erp
docker-compose up --build

Docker Services

The docker-compose.yml includes three services:

  1. api - FastAPI Application

    • Port: 8000
    • Auto-reload with volume mounting
    • Health checks enabled
    • Depends on database
  2. db - PostgreSQL Database

    • Port: 5432
    • Persistent data volume
    • Health checks for startup order
    • Credentials: skatesham:skatesham-github
  3. adminer - Database Admin Interface

    • Port: 9000
    • Web-based database management
    • Connect to db service

Docker Commands

# Start all services
docker-compose up --build

# Start in background
docker-compose up -d --build

# View logs
docker-compose logs -f api

# Stop services
docker-compose down

# Stop and remove volumes
docker-compose down -v

# Rebuild specific service
docker-compose up --build api

# Access running container
docker-compose exec api bash

Environment Variables

The Docker setup automatically configures:

  • DATABASE_URL=postgresql://skatesham:skatesham-github@db:5432/skatesham
  • DEBUG=true (development mode)
  • ENVIRONMENT=development

Production

# Install production dependencies
pip install -e .

# Run with Gunicorn (recommended for production)
gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker

πŸ“Š API Endpoints

System & Health Monitoring

  • GET /api/v1/system/health - Complete health check with database connectivity and pool status
  • GET /api/v1/system/health/live - Kubernetes liveness probe (simple alive check)
  • GET /api/v1/system/health/ready - Kubernetes readiness probe (database connectivity)
  • GET /api/v1/system/info - Detailed service information and configuration
  • GET /api/v1/ - API root endpoint with navigation links

Authentication

  • POST /api/v1/auth/login/ - User login with OAuth2PasswordRequestForm
  • POST /api/v1/auth/register/ - User registration with password hashing
  • GET /api/v1/auth/me/ - Get current authenticated user info

Resources

  • GET /api/v1/buyers/ - List buyers
  • POST /api/v1/buyers/ - Create buyer
  • GET /api/v1/cars/ - List cars
  • POST /api/v1/cars/ - Create car
  • GET /api/v1/sales/ - List sales
  • POST /api/v1/sales/ - Create sale

Health Check Examples

Basic Health Check

curl http://localhost:8000/api/v1/system/health

Response:

{
  "status": "healthy",
  "timestamp": "2024-01-01T00:00:00Z",
  "version": "1.0.0",
  "service": "fastapi-car-shop-erp",
  "checks": {
    "database": {
      "status": "healthy",
      "message": "Database connection successful"
    },
    "database_pool": {
      "status": "healthy",
      "pool_size": 5,
      "checked_in": 5,
      "checked_out": 0,
      "overflow": 0
    }
  }
}

Service Information

curl http://localhost:8000/api/v1/system/info

Response:

{
  "service": {
    "name": "fastapi-car-shop-erp",
    "version": "1.0.0",
    "description": "Professional ERP system for car shop management",
    "author": "Sham Vinicius Fiorin"
  },
  "technology": {
    "framework": "FastAPI",
    "database": "PostgreSQL",
    "orm": "SQLAlchemy",
    "authentication": "JWT OAuth2"
  },
  "environment": {
    "debug": false,
    "database_configured": true,
    "secret_key_configured": true,
    "environment": "development"
  }
}

Kubernetes Integration

The health endpoints are designed for Kubernetes orchestration:

# Kubernetes Deployment Example
apiVersion: apps/v1
kind: Deployment
metadata:
  name: fastapi-erp
spec:
  template:
    spec:
      containers:
      - name: app
        image: fastapi-erp:latest
        ports:
        - containerPort: 8000
        livenessProbe:
          httpGet:
            path: /api/v1/system/health/live
            port: 8000
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /api/v1/system/health/ready
            port: 8000
          initialDelaySeconds: 5
          periodSeconds: 5

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests and code quality checks
  5. Submit a pull request

Source Documentation


πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


Created by

Sham Vinicius Fiorin

Built with ❀️ using FastAPI and modern Python patterns


About

Example of FastAPI bigger application

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages