Merged
Conversation
…refactor middleware and add gzip compression 4.
…ctices Project structure: - Reorganize from backend/fastapi/ to src/backend/, frontend/ to src/frontend/ - Move tests to root tests/ directory with shared conftest.py - Remove dead files (constants.py, authorization.py, rate_limiter.py) Package management: - Replace requirements.txt with pyproject.toml + uv - Pin Python 3.12 via .python-version - Drop unused deps (trio, databases), add uvloop Environment & security: - Replace tracked .env with .env.example template - Add .env, .cursor/, .claude/ to .gitignore - Fix hardcoded SECRET_KEY in SessionMiddleware - Auth reads from pydantic-settings instead of os.getenv() FastAPI best practices (from Kludex/fastapi-tips): - Convert all endpoints and CRUD to async (Tips #2, #9) - Pure ASGI DocProtectMiddleware replacing BaseHTTPMiddleware (Tip #8) - Typed lifespan state yielding session factory (Tip #6) - Async DB init and proper engine disposal on shutdown - SQLAlchemy 2.0: select(), DeclarativeBase, Mapped, async_sessionmaker - Portable UUID column (String(36)) for SQLite + PostgreSQL compat Testing: - All-async test suite with httpx.AsyncClient + pytest-anyio (Tips #5, #10) - conftest.py handles DB lifecycle since ASGITransport skips lifespan - Consolidated test_api_sync.py + test_api_async.py into test_api.py Deployment: - Dockerfile updated for Python 3.12 + uv - Docker build and test run verified in container (7/7 pass) Co-Authored-By: Claude Opus 4.6 <[email protected]>
Replace String(36) with SQLAlchemy 2.0 native Uuid type which maps to PostgreSQL UUID and SQLite CHAR(32) automatically. Fixes startup crash when deploying against an existing PostgreSQL database that has UUID columns from the previous schema. Co-Authored-By: Claude Opus 4.6 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optimized Version: Performance, Structure, and Best Practices Overhaul
Summary
This branch transforms the FastAPI fullstack template from a basic prototype into a production-ready, fully async application with modern Python tooling and FastAPI best practices.
Commits included
3a9cc72[FEAT] Add locust file to benchmark performance6c1f6fa[EHN] orjson, lifespan thread pool, middleware refactor, gzip compression7e5aa79Merge resolved keeping our changesbf667bc[EHN] Add locust in dependency7af624c[REFACTOR] Restructure project, migrate to uv, apply FastAPI best practicesWhat Changed
1. Performance Optimizations (early commits)
2. Project Restructure
src/layout:backend/fastapi/→src/backend/,frontend/→src/frontend/— removes redundant nesting, follows standard Python packaging conventionstests/directory with sharedconftest.pyfor DB lifecycle managementconstants.py,authorization.py,rate_limiter.py, root__init__.py3. Package Management:
requirements.txt→uvpyproject.tomlwith separated core and dev dependencies ([project.optional-dependencies])uv.lockfor reproducible installs.python-versiontrio,databases(SQLAlchemy async handles everything natively)4. Environment & Security
.env→.env.example: Secrets no longer tracked in git.gitignoreupdated: Covers.env,.env.*,.cursor/,.claude/,.DS_StoreSECRET_KEYfixed: Session middleware reads frompydantic-settingsinstead of"your_secret_key"global_settingsinstead of scatteredos.getenv()calls5. FastAPI Best Practices
Integrations from Kludex/fastapi-tips and FastAPI---Strawberry:
async def(no thread pool waste)DocProtectMiddleware(noBaseHTTPMiddlewareoverhead)AppState(TypedDict)with session factoryuvloopadded as platform-conditional dependencyhttpx.AsyncClient+pytest-anyioconn.run_sync(Base.metadata.create_all)select(),DeclarativeBase,Mapped,async_sessionmakerString(36)for SQLite + PostgreSQL compat/messages/asyncendpoint (all endpoints are now async)6. Testing
test_api_sync.py+test_api_async.py→ singletest_api.pyconftest.py: Shared fixtures handle DB creation/teardown sinceASGITransportdoes not trigger FastAPI lifespan events7. Deployment
uv(replaces pip + requirements.txt)docker build+docker run ... pytest— 7/7 passBefore / After
backend/fastapi/(redundant)src/backend/(clean)requirements.txt(no pinning)uv+pyproject.toml+ lockfilesession.query()select()BaseHTTPMiddlewarewrapper"your_secret_key"SECRET_KEYtrio,databasesconftest.pylifecycle.envin git.env.exampletemplate,.envgitignoredTest Plan
uv run pytest tests/ -v— 7/7 pass (0 warnings)uv run ruff check— all checks passeddocker build -t fullstack-fastapi .— builds successfullydocker run --rm fullstack-fastapi sh -c "uv sync --extra dev && uv run pytest tests/ -v"— 7/7 pass in containeruv run python -m src.backend.main --mode devand verify/docs,/login, CRUD via Swaggeruv run locustagainst running server