3.8 KiB
3.8 KiB
Directory Structure
How backend code is organized in this project.
Overview
Backend code is centered in backend/src and follows a route-module layout under v1/ with shared infrastructure in core/.
- HTTP entry:
backend/src/app.pyandbackend/src/v1/router.py - Feature modules:
backend/src/v1/<domain>/ - Cross-cutting infrastructure:
backend/src/core/ - ORM models:
backend/src/models/ - Shared/domain schemas:
backend/src/schemas/ - Global/base service providers:
backend/src/services/ - Migrations:
backend/alembic/ - Tests:
backend/tests/{unit,integration,e2e}
Directory Layout
backend/
├── src/
│ ├── app.py
│ ├── core/
│ │ ├── config/
│ │ ├── db/
│ │ ├── http/
│ │ ├── logging/
│ │ └── taskiq/
│ ├── models/
│ ├── schemas/
│ ├── services/
│ └── v1/
│ ├── router.py
│ ├── auth/
│ ├── users/
│ ├── todo/
│ ├── schedule_items/
│ └── ...
├── alembic/
│ ├── env.py
│ └── versions/
└── tests/
├── unit/
├── integration/
└── e2e/
Module Organization
Observed feature-module pattern (evidence from multiple v1/* modules):
router.pycontains FastAPI routes and request/response wiringdependencies.pycontains DI providersservice.pycontains business logic, authz checks, transaction boundariesrepository.pycontains query/CRUD logic onlyschemas.pycontains API DTOs (Pydantic)
Real examples:
backend/src/v1/todo/{router.py,dependencies.py,service.py,repository.py,schemas.py}backend/src/v1/schedule_items/{router.py,dependencies.py,service.py,repository.py,schemas.py}backend/src/v1/friendships/{router.py,dependencies.py,service.py,repository.py,schemas.py}
Cross-module composition examples:
backend/src/v1/router.pyincludes all domain routers under/api/v1backend/src/app.pymounts the v1 router and global exception handlersbackend/src/v1/todo/dependencies.pycomposes DB session + user dependency + repositories intoTodoService
Naming Conventions
Conventions observed in current codebase:
- Package and file names use snake_case (
schedule_items,inbox_messages,registration_bootstrap.py) - Route package names are domain nouns (
todo,users,auth,agent) - Common file names are stable across modules:
router.pyservice.pyrepository.pyschemas.pydependencies.py
- Logger names usually follow module path semantics, e.g.:
get_logger("v1.todo.service")get_logger("v1.schedule_items.repository")get_logger("api.app")
Anti-patterns / Forbidden Patterns
Grounded by repository rules and existing structure:
- Do not place backend feature code under
apps/**or non-backend folders; backend runtime code stays inbackend/src/**. - Do not bypass module layering by putting SQL and transaction commits directly in router files.
- Current positive references:
backend/src/v1/todo/router.py(routing only),backend/src/v1/todo/service.py(commit/rollback),backend/src/v1/todo/repository.py(query layer).
- Current positive references:
- Do not introduce ad-hoc folder layouts for new domains when
v1/<domain>/{router,service,repository,schemas,dependencies}already exists.
Uncertainties (documented, not invented)
- There is no explicit convention doc yet for when logic should go to
backend/src/services/**versusbackend/src/v1/**; both exist and are currently used for different scopes. backend/src/v1/analytics/includestasks.pyand static web content usage frombackend/src/app.py(v1/analytics/web), which is slightly different from CRUD-oriented modules.