A production-ready FastAPI-based backend API framework that follows best practices for building high-performance web APIs.
- Modular Architecture: Organized by functional domains for better maintainability
- Developer Management: Developer registration and information management
- API Key Management: Secure API key generation, distribution, and lifecycle management
- JWT Authentication: JWT-based request authentication mechanism
- Async Support: Full utilization of FastAPI's asynchronous capabilities for better performance
- ORM Integration: Integrated with Tortoise ORM for powerful async database operations
- Environment Configuration: Flexible multi-environment configuration system
- Logging System: Integrated Loguru for elegant logging
- CLI Tools: Complete command-line toolset for managing developers, API keys, and database migrations
- Python 3.8+
- MySQL 5.7+
- Poetry (Python dependency management tool)
- Docker and Docker Compose (optional, for containerized deployment)
# Install project dependencies
poetry install
# Method 1: Use poetry run (recommended for all Poetry versions)
poetry run python -m src
# Method 2: Manually activate virtual environment (if you need to run multiple commands in the venv)
source $(poetry env info --path)/bin/activate
Copy .env.example
to .env
and configure according to your environment:
# Application settings
APP_NAME=fastapi-base-framework
HOST=0.0.0.0
PORT=8000
WORKERS_COUNT=1
RELOAD=True
ENVIRONMENT=development
DEBUG=True
LOG_LEVEL=INFO
# Database settings
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_BASE=fastapi_base
DB_ECHO=False
# JWT settings
SECRET_KEY=replace-with-secure-secret-key
JWT_SECRET=replace-with-secure-jwt-secret
JWT_ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=1440
# Create database
mysql -u root -e "CREATE DATABASE IF NOT EXISTS fastapi_base CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
# Initialize database structure
python scripts/init_db.py
# Or use CLI tools step by step
# Initialize database migration environment
python -m src.cli.main db init
# Generate migration files
python -m src.cli.main db migrate "Initial migration"
# Apply migrations
python -m src.cli.main db upgrade
poetry run python -m src
After starting the service, you can access:
- API Documentation: http://localhost:8000/api/docs
- ReDoc Documentation: http://localhost:8000/api/redoc
docker-compose -f docker-compose.yml -f deploy/docker-compose.dev.yml up --build
docker-compose up -d --build
This project provides a powerful command-line tool for managing developers, API keys, database migrations, and more. The CLI tool is built with Typer, offering a user-friendly command-line interface with detailed help information.
# Show main help and available commands
python -m src.cli
# Or use Poetry
poetry run python -m src.cli
# Show developer command help
python -m src.cli developer --help
# List all developers
python -m src.cli developer list
# Add a new developer
python -m src.cli developer add --name "Developer Name" --email "developer@example.com"
# Shorthand
python -m src.cli developer add -n "Developer Name" -e "developer@example.com"
# Get developer details
python -m src.cli developer get 1
# Update developer information
python -m src.cli developer update 1 --name "New Name" --email "new-email@example.com"
# Delete a developer
python -m src.cli developer delete 1
# Skip confirmation
python -m src.cli developer delete 1 -y
# Show API key command help
python -m src.cli api-key --help
# List all API keys for a developer
python -m src.cli api-key list 1
# Create a new API key for a developer
python -m src.cli api-key create 1
# Revoke an API key
python -m src.cli api-key revoke 123
# Skip confirmation
python -m src.cli api-key revoke 123 -y
This project uses Aerich for database migration management. Below are the detailed usage instructions.
When running the project for the first time, you need to initialize the database structure. We provide two approaches:
-
Using the initialization script (recommended for first-time setup):
python scripts/init_db.py
-
Using CLI tools (for more control):
# Initialize migration environment python -m src.cli.main db init # Generate migration files python -m src.cli.main db migrate "Initial migration" # Apply migrations python -m src.cli.main db upgrade
python -m src.cli.main db history
# Rollback to the previous version
python -m src.cli.main db downgrade
# Or rollback to a specific version
python -m src.cli.main db downgrade "version_name"
python -m src.cli.main db show-models
The aerich.ini
file in the project root defines Aerich's configuration:
[aerich]
tortoise_orm = src.db.config.TORTOISE_CONFIG
location = ./migrations
src_folder = ./
Run tests:
# Start test database
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=test -e MYSQL_DATABASE=test_db -d mysql:8.4
# Run tests
pytest -vvs tests/
Create a .env.prod
file and configure production environment variables:
ENVIRONMENT=production
DEBUG=False
DB_PASSWORD=your_secure_production_password
SECRET_KEY=your_secure_secret_key
JWT_SECRET=your_secure_jwt_secret
# Other production configurations...
docker-compose -f docker-compose.yml -f deploy/docker-compose.prod.yml up -d
gunicorn src.gunicorn_runner:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
- Use strong passwords and keys in production
- Enable HTTPS for secure communication
- Rotate API keys and JWT secrets regularly
- Implement rate limiting to prevent abuse
- Use authentication and authorization for all sensitive operations
This framework follows FastAPI best practices:
- Modular Routing: Organized by functional domains
- Dependency Injection: For authentication and parameter validation
- Pydantic Models: Clear data models for requests and responses
- Async Processing: Leverage async features for better performance
- Auto-generated Documentation: Utilize FastAPI's automatic documentation
Contributions are welcome! Please feel free to submit pull requests, report issues, or suggest improvements. Make sure to follow the project's code style and contribution guidelines.