λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸš€ Backend

[FastAPI] Best Practices - Project Structure

by dev.py 2025. 2. 14.

Project Structure

FastAPI ν”„λ‘œμ νŠΈ ꡬ쑰 섀계 ν• λ•Œ μ°Έκ³ ν•˜κΈ° μ’‹λ‹€.

Netflaix/Dipatchλ₯Ό μ°Έμ‘°ν•˜μ—¬ μˆ˜μ •ν•œ μžλ£Œμ΄λ‹€.

fastapi-project
β”œβ”€β”€ alembic/
β”œβ”€β”€ app # 원본은 src μ΄μ§€λ§Œ 본인은 app μ„ ν˜Έν•¨.
β”‚   β”œβ”€β”€ auth
β”‚   β”‚   β”œβ”€β”€ router.py
β”‚   β”‚   β”œβ”€β”€ schemas.py  # pydantic models
β”‚   β”‚   β”œβ”€β”€ models.py  # db models
β”‚   β”‚   β”œβ”€β”€ dependencies.py
β”‚   β”‚   β”œβ”€β”€ config.py  # local configs
β”‚   β”‚   β”œβ”€β”€ constants.py
β”‚   β”‚   β”œβ”€β”€ exceptions.py
β”‚   β”‚   β”œβ”€β”€ service.py
β”‚   β”‚   └── utils.py
β”‚   β”œβ”€β”€ aws
β”‚   β”‚   β”œβ”€β”€ client.py  # client model for external service communication
β”‚   β”‚   β”œβ”€β”€ schemas.py
β”‚   β”‚   β”œβ”€β”€ config.py
β”‚   β”‚   β”œβ”€β”€ constants.py
β”‚   β”‚   β”œβ”€β”€ exceptions.py
β”‚   β”‚   └── utils.py
β”‚   └── posts
β”‚   β”‚   β”œβ”€β”€ router.py
β”‚   β”‚   β”œβ”€β”€ schemas.py
β”‚   β”‚   β”œβ”€β”€ models.py
β”‚   β”‚   β”œβ”€β”€ dependencies.py
β”‚   β”‚   β”œβ”€β”€ constants.py
β”‚   β”‚   β”œβ”€β”€ exceptions.py
β”‚   β”‚   β”œβ”€β”€ service.py
β”‚   β”‚   └── utils.py
β”‚   β”œβ”€β”€ config.py  # global configs
β”‚   β”œβ”€β”€ models.py  # global models
β”‚   β”œβ”€β”€ exceptions.py  # global exceptions
β”‚   β”œβ”€β”€ pagination.py  # global module e.g. pagination
β”‚   β”œβ”€β”€ database.py  # db connection related stuff
β”‚   └── main.py
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ auth
β”‚   β”œβ”€β”€ aws
β”‚   └── posts
β”œβ”€β”€ templates/
β”‚   └── index.html
β”œβ”€β”€ requirements
β”‚   β”œβ”€β”€ base.txt
β”‚   β”œβ”€β”€ dev.txt
β”‚   └── prod.txt
β”œβ”€β”€ .env
β”œβ”€β”€ .gitignore
β”œβ”€β”€ logging.ini
└── alembic.ini

ν”„λ‘œμ νŠΈκ°€ ν™•μž₯될 것을 μ—Όλ‘ν•˜μ—¬, 도메인 λ³„λ‘œ ꡬ쑰λ₯Ό κ°€μ Έκ°„λ‹€ ( crud, router, models 둜 λ‚˜λˆ„λŠ” 것이 μ•„λ‹Œ)

 

1. λͺ¨λ“  도메인 디렉토리λ₯Ό app/ μ•ˆμ— μ €μž₯ν•œλ‹€.

  • app/- μ•±μ˜ μ΅œμƒμœ„ λ””λ ‰ν† λ¦¬λ‘œ, 일반적인 λͺ¨λΈ, μ„€μ •, μƒμˆ˜ 등을 ν¬ν•¨ν•œλ‹€.
  • app/main.py- FastAPI 앱을 μ΄ˆκΈ°ν™”ν•˜λŠ” ν”„λ‘œμ νŠΈμ˜ 루트

 

2. 각 νŒ¨ν‚€μ§€μ—λŠ” λΌμš°ν„°, μŠ€ν‚€λ§ˆ, λͺ¨λΈ 등이 μžˆλ‹€.

  • router.py- λͺ¨λ“  μ—”λ“œν¬μΈνŠΈλ₯Ό κ°–μΆ˜ 각 λͺ¨λ“ˆμ˜ 핡심
  • schemas.py- pydantic λͺ¨λΈμš©
  • models.py- db λͺ¨λΈμš©
  • service.py- λͺ¨λ“ˆλ³„ λΉ„μ¦ˆλ‹ˆμŠ€ 둜직
  • dependencies.py- λΌμš°ν„° 쒅속성
  • constants.py- λͺ¨λ“ˆλ³„ μƒμˆ˜ 및 였λ₯˜ μ½”λ“œ
  • config.py- 예: ν™˜κ²½ λ³€μˆ˜
  • utils.py- λΉ„μ¦ˆλ‹ˆμŠ€ 둜직이 μ•„λ‹Œ κΈ°λŠ₯, 예: 응닡 μ •κ·œν™”, 데이터 보강 λ“±
  • exceptions.py- λͺ¨λ“ˆλ³„ μ˜ˆμ™Έ, 예 PostNotFound:InvalidUserData

 

3. λ‹€λ₯Έ νŒ¨ν‚€μ§€μ—μ„œ (μ„œλΉ„μŠ€, μƒμˆ˜) 등이 ν•„μš”ν•œ 경우 λͺ…μ‹œμ μœΌλ‘œ λͺ¨λ“ˆ μ΄λ¦„μœΌλ‘œ κ°€μ Έμ˜¨λ‹€.

from src.auth import constants as auth_constants
from src.notifications import service as notification_service
from src.posts.constants import ErrorCode as PostsErrorCode  # PostsErrorCode둜 κ°€μ Έμ˜¨λ‹€.

 

좜처

 

GitHub - zhanymkanov/fastapi-best-practices: FastAPI Best Practices and Conventions we used at our startup

FastAPI Best Practices and Conventions we used at our startup - zhanymkanov/fastapi-best-practices

github.com