Skip to content

Latest commit

Β 

History

35 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fastapi Boilerplate

A FastAPI-based backend service for the Fastapi Boilerplate platform. This application provides RESTful APIs for managing estates, maintenance activities, bills, and resident records.


πŸš€ Tech Stack

  • Framework: FastAPI
  • Language: Python 3.8+
  • Database: PostgreSQL
  • ORM: SQLAlchemy
  • Migration Tool: Alembic
  • Testing: unittest
  • Authentication: JWT (JSON Web Tokens)

πŸ“¦ Getting Started

Prerequisites

  • Python 3.8 or higher
  • PostgreSQL 12+
  • pip (Python package manager)

Installation

1. Clone the repository

git clone https://github.com/Smash-Tech-Group/fastapi-boilerplate.git
cd fastapi-boilerplate

2. Create and activate virtual environment

# Create virtual environment
python3 -m venv .venv

# Activate virtual environment
# On Linux/Mac:
source .venv/bin/activate

# On Windows:
.venv\Scripts\activate

3. Install dependencies

pip install -r requirements.txt

4. Configure environment variables

Create a .env file by copying the sample:

cp .env.sample .env

Update the .env file with your configuration:

DATABASE_URL=postgresql://user:password@localhost:5432/em_fast_api
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30

5. Set up PostgreSQL database

# Access PostgreSQL as root
sudo -u postgres psql
-- Create database user
CREATE USER user WITH PASSWORD 'your_password';

-- Create database
CREATE DATABASE db_fast_api;

-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE db_fast_api TO user;

-- Exit PostgreSQL
\q

6. Run database migrations

# Apply existing migrations
alembic upgrade head
# Apply existing migrations
alembic revision --autogenerate -m "message"
# Apply existing migrations
alembic upgrade head

7. Seed the database (optional)

python3 seed.py DB - Sheet1.csv

8. Start the server

python main.py

The API will be available at http://localhost:8000

API Documentation:

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

πŸ› οΈ Available Commands

Command Description
python main.py Start the FastAPI development server
alembic revision --autogenerate -m "message" Generate new migration
alembic upgrade head Apply all pending migrations
alembic downgrade -1 Rollback last migration
python3 seed.py Populate database with dummy data
python -m unittest tests/v1/test_*.py Run specific test file

πŸ—„οΈ Database Management

Creating Migrations

When you add new models or modify existing ones:

1. Ensure your model is imported

Import your model in api/v1/models/__init__.py:

from .your_model import YourModel

2. Generate migration

alembic revision --autogenerate -m "add your_table"

3. Apply migration

alembic upgrade head

Migration Troubleshooting

If you encounter this error:

ERROR [alembic.util.messaging] Target database is not up to date.

Solution:

# First, update the database
alembic upgrade head

# Then generate your migration
alembic revision --autogenerate -m "your migration message"

πŸ§ͺ Testing

This project uses Python's unittest framework.

Run specific tests:

# Test login endpoint
python -m unittest tests/v1/test_login.py

# Test signup endpoint
python -m unittest tests/v1/test_signup.py

Important: Always test your endpoints and models before pushing code. s

πŸ“ Project Structure

fastapi-boilerplate/
β”œβ”€β”€ alembic/                     # Database migrations
β”‚   β”œβ”€β”€ versions/                # Migration version files
β”‚   └── env.py                   # Alembic environment configuration
β”œβ”€β”€ api/
β”‚   β”œβ”€β”€ core/                    # Core application components
β”‚   β”‚   β”œβ”€β”€ base/                # Base classes and models
β”‚   β”‚   β”œβ”€β”€ dependencies/        # Dependency injection
β”‚   β”‚   └── responses.py         # Standard API responses
β”‚   β”œβ”€β”€ db/                      # Database configuration
β”‚   β”‚   └── database.py          # Database connection and session
β”‚   β”œβ”€β”€ loggers/                 # Logging configuration
β”‚   β”œβ”€β”€ utils/                   # Utility functions and helpers
β”‚   β”‚   β”œβ”€β”€ config.py            # Application configuration
β”‚   β”‚   β”œβ”€β”€ constants.py         # Application constants
β”‚   β”‚   β”œβ”€β”€ db_validators.py    # Database validation utilities
β”‚   β”‚   β”œβ”€β”€ files.py             # File handling utilities
β”‚   β”‚   β”œβ”€β”€ helpers.py           # General helper functions
β”‚   β”‚   β”œβ”€β”€ json_validator.py   # JSON validation
β”‚   β”‚   β”œβ”€β”€ log_streamer.py     # Log streaming utilities
β”‚   β”‚   β”œβ”€β”€ mime_types.py        # MIME type definitions
β”‚   β”‚   β”œβ”€β”€ minio_service.py    # MinIO object storage service
β”‚   β”‚   β”œβ”€β”€ pagination.py        # Pagination utilities
β”‚   β”‚   β”œβ”€β”€ rate_limiter.py     # Rate limiting middleware
β”‚   β”‚   β”œβ”€β”€ settings.py          # Application settings
β”‚   β”‚   β”œβ”€β”€ success_response.py # Success response formatters
β”‚   β”‚   β”œβ”€β”€ tweet_service.py    # Tweet/social media service
β”‚   β”‚   └── urllib_request.py   # HTTP request utilities
β”‚   └── v1/                      # API version 1
β”‚       β”œβ”€β”€ models/              # SQLAlchemy ORM models
β”‚       β”‚   └── __init__.py      # Import all models here
β”‚       β”œβ”€β”€ routes/              # API route handlers
β”‚       β”‚   └── __init__.py      # Router configuration
β”‚       β”œβ”€β”€ schemas/             # Pydantic request/response schemas
β”‚       └── services/            # Business logic layer
β”œβ”€β”€ logs/                        # Application logs
β”œβ”€β”€ media/                       # Media files
β”‚   └── uploads/                 # User uploaded files
β”œβ”€β”€ node_modules/                # Node.js dependencies (if any)
β”œβ”€β”€ qa_tests/                    # QA test suite
β”œβ”€β”€ tests/                       # Unit and integration tests
β”‚   β”œβ”€β”€ v1/                      # Version 1 API tests
β”‚   β”‚   β”œβ”€β”€ test_login.py
β”‚   β”‚   └── test_signup.py
β”‚   β”œβ”€β”€ conftest.py              # Pytest configuration and fixtures
β”‚   β”œβ”€β”€ database.py              # Test database setup
β”‚   └── run_all_test.py          # Test runner script
β”œβ”€β”€ tmp/                         # Temporary files
β”œβ”€β”€ venv/                        # Virtual environment (git-ignored)
β”œβ”€β”€ .env                         # Environment variables (git-ignored)
β”œβ”€β”€ .env.sample                  # Environment variables template
β”œβ”€β”€ alembic.ini                  # Alembic configuration
β”œβ”€β”€ CountryPricingTable.py       # Country pricing utilities
β”œβ”€β”€ LICENSE                      # Apache 2.0 License
β”œβ”€β”€ main.py                      # Application entry point
β”œβ”€β”€ package.json                 # Node.js package configuration
β”œβ”€β”€ package-lock.json            # Node.js dependency lock
β”œβ”€β”€ README.md                    # Project documentation
β”œβ”€β”€ release.config.cjs           # Release configuration
β”œβ”€β”€ requirements.txt             # Python dependencies
β”œβ”€β”€ setup.py                     # Package setup configuration
└── update_api_status.py         # API status update script

πŸ”€ Adding New Features

Adding New Models

1. Create your model file in api/v1/models/your_model.py

2. Import it in api/v1/models/__init__.py:

from .your_model import YourModel

3. Generate and apply migration:

alembic revision --autogenerate -m "add your_model"
alembic upgrade head

Adding New Routes

1. Check existing route files in api/v1/routes/

If a related file exists, add your route there. Otherwise, create a new file.

2. Create route file (e.g., api/v1/routes/yourRoute.py):

from fastapi import APIRouter

router = APIRouter(
    prefix="/estates",  # Don't include /api/v1
    tags=["yourRoute"]
)

@router.get("/")
async def get_estates():
    return {"message": "List of estates"}

3. Register the router in api/v1/routes/__init__.py:

from .estates import router as estates_router

api_version_one.include_router(estates_router)

Note: Don't include the base prefix /api/v1 in your router, as it's already included in the api_version_one router.


πŸ‘₯ Contributing

We follow the Git Flow workflow for branch management and collaboration.

Branch Structure

  • main - Production-ready code
  • develop - Integration branch for features
  • feature/* - New features
  • hotfix/* - Urgent production fixes
  • release/* - Release preparation

Git Flow Workflow

1. Start a new feature

# Create and switch to a new feature branch from develop
git checkout develop
git pull origin develop
git checkout -b feature/your-feature-name

2. Work on your feature

  • Write clean, maintainable code
  • Add tests for new functionality
  • Test endpoints before committing
  • Follow the coding guidelines below

3. Run tests

python -m unittest discover tests/

4. Commit your changes

# Use conventional commit messages
git add .
git commit -m "feat: add Fastapi Boilerplate endpoints"

Commit message conventions:

  • feat: - New feature
  • fix: - Bug fix
  • docs: - Documentation changes
  • style: - Code style changes (formatting, etc.)
  • refactor: - Code refactoring
  • test: - Adding or updating tests
  • chore: - Maintenance tasks

5. Push migrations and create Pull Request

# Push your feature branch (including migrations)
git push origin feature/your-feature-name

Then create a Pull Request from feature/your-feature-name β†’ develop

6. After PR approval and merge

# Delete the local feature branch
git checkout develop
git pull origin develop
git branch -d feature/your-feature-name

Hotfix Workflow

For urgent production fixes:

# Create hotfix branch from main
git checkout main
git pull origin main
git checkout -b hotfix/fix-critical-bug

# Make your fix and test thoroughly
python -m unittest discover tests/

# Commit and push
git commit -m "fix: resolve critical authentication bug"
git push origin hotfix/fix-critical-bug

Create PR to both main and develop

Release Workflow

When preparing a release:

# Create release branch from develop
git checkout develop
git pull origin develop
git checkout -b release/v1.2.0

# Update version numbers, changelog, etc.
# Test thoroughly

# Merge to main
git checkout main
git merge release/v1.2.0
git tag -a v1.2.0 -m "Release version 1.2.0"
git push origin main --tags

# Merge back to develop
git checkout develop
git merge release/v1.2.0
git push origin develop

# Delete release branch
git branch -d release/v1.2.0

βœ… Coding Guidelines

General Principles

  • Follow PEP 8 style guide for Python code
  • Write descriptive variable and function names
  • Add docstrings to all functions and classes
  • Keep functions small and focused
  • Write tests for all new endpoints and services

Best Practices

  • Always test endpoints before pushing
  • Include Alembic migrations in your commits
  • Use Pydantic schemas for request/response validation
  • Implement proper error handling with appropriate HTTP status codes
  • Use dependency injection for database sessions
  • Keep business logic in service layer, not in routes

Code Structure

  • Models: SQLAlchemy ORM models (api/v1/models/)
  • Schemas: Pydantic models for validation (api/v1/schemas/)
  • Routes: API endpoints (api/v1/routes/)
  • Services: Business logic (api/v1/services/)
  • Tests: Unit tests (tests/v1/)

πŸ”§ Configuration Files

File Purpose
main.py Application entry point and FastAPI configuration
alembic.ini Alembic migration configuration
requirements.txt Python package dependencies
.env Environment variables (git-ignored)
.env.sample Template for required environment variables
seed.py Database seeding script

πŸ“š Additional Resources


πŸ“„ License

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


⚠️ Important Reminders

  • βœ… Always test your endpoints before pushing
  • βœ… Include Alembic migrations in your commits
  • βœ… Update .env.sample when adding new environment variables
  • βœ… Import new models in api/v1/models/__init__.py
  • βœ… Follow the Git Flow workflow for all contributions
  • βœ… Run alembic upgrade head before generating new migrations

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages