# FastAPI-Template
**Repository Path**: msy_study/FastAPI-Template
## Basic Information
- **Project Name**: FastAPI-Template
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2025-09-09
- **Last Updated**: 2026-01-12
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# đ Enterprise-Grade FastAPI Backend Template
**A production-ready FastAPI backend template with clean architecture, built-in RBAC, and enterprise features - ready to use out of the box**
[](https://www.python.org/downloads/)
[](https://fastapi.tiangolo.com/)
[](https://opensource.org/licenses/MIT)
[](https://github.com/JiayuXu0/FastAPI-Template/stargazers)
[](https://github.com/astral-sh/uv)
[](#)
[](#)
[](https://www.docker.com/)
[įŽäŊ䏿](README.md) | **English**
â Like this project? Give it a star! â
[đ Quick Start](#-quick-start) âĸ [đī¸ Architecture](#-architecture) âĸ [đ Features](#-features) âĸ [đ Website](http://fastapi.infyai.cn/) âĸ [đ¤ Contributing](CONTRIBUTING.md)
---
## đ Why Choose This Template?
| đ¯ **Enterprise Ready** | ⥠**Developer Friendly** | đĄī¸ **Secure by Default** | đ **High Performance** |
|:---:|:---:|:---:|:---:|
| Clean 3-layer architecture
Production tested | 5-minute setup
Zero configuration hassle | RBAC, JWT, Rate limiting
Security best practices | Async/await throughout
Redis caching built-in |
## ⨠Features
### đ Authentication & Authorization
- **JWT Authentication** - Secure token-based auth with refresh tokens
- **RBAC System** - Role-based access control with fine-grained permissions
- **User Management** - Complete user CRUD with profile management
- **Rate Limiting** - Built-in protection against brute force attacks
### đī¸ Architecture
- **3-Layer Design** - Clean separation: API â Service â Repository â Model
- **Async Support** - Full async/await for high performance
- **Type Safety** - Complete type annotations with Pydantic
- **Dependency Injection** - FastAPI's powerful DI system
### đĄī¸ Security
- **Password Policies** - Enforced strong passwords (8+ chars with letters & numbers)
- **Login Throttling** - 5 attempts per minute with smart lockout
- **JWT Security** - Short-lived access tokens (4h) + refresh tokens (7d)
- **File Security** - Type validation, size limits, malware detection
- **CORS & Headers** - Proper CORS setup and security headers
### đ Data Management
- **Menu System** - Dynamic menu configuration with hierarchy
- **Department Management** - Organizational structure support
- **File Management** - Secure upload/download with S3 compatibility
- **Audit Logging** - Complete activity tracking
### ⥠Performance
- **Redis Caching** - Built-in caching layer with decorators
- **Connection Pooling** - Optimized database connections
- **Async Architecture** - Non-blocking I/O throughout
- **Background Tasks** - APScheduler integration
## đ Quick Start
### Prerequisites
- Python 3.11+
- UV package manager
- PostgreSQL (optional, SQLite for development)
- Redis (optional, for caching)
### Installation
```bash
# Install UV package manager
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone the repository
git clone https://github.com/JiayuXu0/FastAPI-Template.git
cd FastAPI-Template
# Install dependencies
uv sync
# Copy environment file
cp .env.example .env
# Initialize database
uv run aerich init-db
# Start development server
uv run uvicorn src:app --reload --host 0.0.0.0 --port 8000
```
### Access the Application
- **đ Website**: http://fastapi.infyai.cn/
- **API Documentation**: http://localhost:8000/docs
- **Alternative Docs**: http://localhost:8000/redoc
- **Health Check**: http://localhost:8000/api/v1/base/health
### Default Credentials
```
Username: admin
Password: abcd1234
```
â ī¸ **Change these immediately in production!**
## đī¸ Architecture
```
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â API Layer â
â FastAPI Routes - Input validation, Response formatting â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â Service Layer â
â Business Logic - Permissions, Validation, Cross-cutting â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â Repository Layer â
â Data Access - CRUD operations, Query building â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â Model Layer â
â Tortoise ORM - Database models, Relations â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
```
## đ Project Structure
```
src/
âââ api/v1/ # API routes and endpoints
â âââ base/ # Auth, health checks
â âââ users/ # User management
â âââ roles/ # Role management
â âââ ...
âââ services/ # Business logic layer
âââ repositories/ # Data access layer
âââ models/ # Database models
âââ schemas/ # Pydantic schemas
âââ core/ # Core functionality
â âââ dependency.py # FastAPI dependencies
â âââ middlewares.py # Custom middlewares
â âââ exceptions.py # Exception handlers
âââ utils/ # Utility functions
âââ settings/ # Configuration
```
## đ ī¸ Technology Stack
| Component | Technology | Version |
|-----------|------------|---------|
| Language | Python | 3.11+ |
| Framework | FastAPI | 0.100+ |
| ORM | Tortoise ORM | 0.20+ |
| Database | PostgreSQL/SQLite | Latest |
| Cache | Redis | 4.5+ |
| Package Manager | UV | Latest |
| Authentication | PyJWT | 2.8+ |
| Validation | Pydantic | 2.0+ |
## đ§ Configuration
### Environment Variables
Create a `.env` file with these key settings:
```bash
# Security (Required - Generate new keys!)
SECRET_KEY=your-secret-key-here # Generate: openssl rand -hex 32
SWAGGER_UI_PASSWORD=strong-password-here
# Database
DB_ENGINE=sqlite # or postgres for production
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=your-db-password
DB_NAME=your-db-name
# Application
DEBUG=True # Set to False in production
APP_ENV=development # or production
CORS_ORIGINS=http://localhost:3000,http://localhost:8080
```
## đ Deployment
### Docker Deployment
```bash
# Build the image
docker build -t fastapi-template .
# Run the container
docker run -d -p 8000:8000 --env-file .env fastapi-template
```
### Production Checklist
- [ ] Set `DEBUG=False`
- [ ] Generate strong `SECRET_KEY`
- [ ] Use PostgreSQL instead of SQLite
- [ ] Configure proper `CORS_ORIGINS`
- [ ] Enable HTTPS with SSL certificates
- [ ] Set up monitoring and logging
- [ ] Configure backup strategy
- [ ] Review and tighten security settings
## đ Documentation
- [Development Guide](CLAUDE.md) - Detailed development instructions
- [API Reference](http://localhost:8000/docs) - Interactive API documentation
- [Contributing Guide](CONTRIBUTING.md) - How to contribute
## đ¤ Contributing
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request
## đ Project Stats



## đ Success Stories
This template has been used to build:
- đĸ Enterprise management systems with 100k+ users
- đ E-commerce backends handling high traffic
- đą Mobile app APIs with real-time features
- đ¯ Multi-tenant SaaS platforms
## đ License
This project is licensed under the [MIT License](LICENSE) - see the LICENSE file for details.
## đ Acknowledgments
- [FastAPI](https://fastapi.tiangolo.com/) - The amazing web framework
- [Tortoise ORM](https://tortoise.github.io/) - The async ORM
- [UV](https://github.com/astral-sh/uv) - The blazing fast Python package manager
---
**Built with â¤ī¸ for the developer community**
If this project helps you, please consider giving it a â!
[Report Bug](../../issues) âĸ [Request Feature](../../issues) âĸ [Discussions](../../discussions)