โครงสร้างมาตรฐานที่แนะนำสำหรับ FastAPI

โครงสร้างมาตรฐานที่แนะนำสำหรับ FastAPI

 

1. โครงสร้างพื้นฐาน (Small Project)

my-fastapi-project/
├── main.py          # Entry point
├── requirements.txt # Dependencies
├── .env            # Environment variables
└── README.md       # Documentation

2. โครงสร้างขนาดกลาง (Medium Project)

my-fastapi-project/
├── app/
│   ├── __init__.py
│   ├── main.py          # FastAPI app instance
│   ├── dependencies.py  # Global dependencies
│   ├── routers/         # API endpoints
│   │   ├── __init__.py
│   │   ├── users.py
│   │   └── items.py
│   ├── models/          # Pydantic models
│   │   ├── __init__.py
│   │   ├── user.py
│   │   └── item.py
│   └── database.py      # Database connection
├── requirements.txt
├── .env
└── README.md

3. โครงสร้างขนาดใหญ่ (Large Project) - คล้ายกับที่คุณใช้

my-fastapi-project/
├── app/
│   ├── __init__.py
│   ├── main.py
│   ├── core/            # Core configurations
│   │   ├── __init__.py
│   │   ├── config.py    # Settings & configurations
│   │   ├── security.py  # Authentication & security
│   │   └── database.py  # Database setup
│   ├── api/             # API layer
│   │   ├── __init__.py
│   │   ├── deps.py      # Dependencies
│   │   └── v1/          # API version 1
│   │       ├── __init__.py
│   │       ├── api.py   # Main API router
│   │       └── endpoints/
│   │           ├── __init__.py
│   │           ├── users.py
│   │           └── items.py
│   ├── models/          # Database models (SQLAlchemy)
│   │   ├── __init__.py
│   │   ├── user.py
│   │   └── item.py
│   ├── schemas/         # Pydantic schemas
│   │   ├── __init__.py
│   │   ├── user.py
│   │   └── item.py
│   ├── services/        # Business logic
│   │   ├── __init__.py
│   │   ├── user_service.py
│   │   └── item_service.py
│   ├── utils/           # Utility functions
│   │   ├── __init__.py
│   │   └── helpers.py
│   └── tests/           # Test files
│       ├── __init__.py
│       ├── test_users.py
│       └── test_items.py
├── alembic/             # Database migrations
├── requirements.txt
├── .env
├── .env.example
├── .gitignore
├── docker-compose.yml
├── Dockerfile
└── README.md

การแยกหน้าที่ของแต่ละส่วน

1. core/ - การตั้งค่าหลัก

  • config.py: การตั้งค่า environment variables, database URLs
  • database.py: การเชื่อมต่อฐานข้อมูล
  • security.py: การจัดการ authentication, JWT tokens

2. api/ - API Layer

  • routers/: กลุ่มของ endpoints ตามหน้าที่
  • deps.py: Dependencies ที่ใช้ร่วมกัน
  • API versioning (v1/, v2/)

3. models/ - Database Models

  • SQLAlchemy models สำหรับฐานข้อมูล
  • Table definitions และ relationships

4. schemas/ - Pydantic Schemas

  • Request/Response models
  • Data validation และ serialization

5. services/ - Business Logic

  • ตmantik ทางธุรกิจ
  • การประมวลผลข้อมูล
  • การเชื่อมต่อกับ external APIs

เปรียบเทียบกับโปรเจคของคุณ

โปรเจคของคุณมีโครงสร้างที่ดีแล้ว:

server/api/
├── main.py          ✅ Entry point
├── core/
│   ├── config.py    ✅ Configuration
│   └── database.py  ✅ Database setup
├── routers/
│   └── chat.py      ✅ API endpoints
├── schemas/
│   └── chat.py      ✅ Pydantic schemas
└── services/
    ├── providers.py ✅ Business logic
    └── rag_service.py ✅ RAG service


ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

SetUp Theme Monokai On Visual Studio

"zsh: command not found: docker"

คู่มือ Install IIS On Windows Server 2016