How vdraha-dev/social-media-app works
Purpose and Entry Points
This is an async FastAPI monolith for social-media identity/access management: registration, JWT login, logout via token invalidation, and current-user lookup. The HTTP application is created in app/main.py; API composition and identity endpoints reside in app/identity/presentation/router.py. It runs under Uvicorn directly or through Dockerfile and compose.yaml.
Bounded Contexts and Layering
The repository is organized as DDD-style bounded contexts. Identity is the implemented authentication context; Profiles is a parallel/emerging context with the same domain/application/infrastructure/presentation split. Shared code supplies cross-context primitives and infrastructure.
• Identity: app/identity/domain/entities.py, app/identity/application/use_cases.py, app/identity/infrastructure/repository.py
• Profiles: app/profiles/domain/entities.py, app/profiles/application/use_cases.py, app/profiles/infrastructure/repositories.py
• Shared: app/shared/domain/base_entity.py, app/shared/infrastructure/database.py
Request and Control Flow
Requests enter FastAPI routers, where presentation dependencies resolve authentication and application services. Router handlers map HTTP/Pydantic inputs to DTOs and invoke use cases; exception handlers translate domain/application failures into HTTP responses.
• HTTP boundary: app/identity/presentation/router.py, app/identity/presentation/dependencies.py, app/identity/presentation/exception_handlers.py
• Application orchestration: app/identity/application/dto.py, app/identity/application/use_cases.py
• Cross-cutting HTTP errors: app/shared/presentation/exception_handlers.py
Domain Model and Events
Business invariants belong in entities and immutable value objects, insulated from FastAPI and SQLAlchemy. Identity emits domain events; the shared in-memory event bus enables decoupled side effects within the process. Domain repository abstractions prevent use cases from depending on persistence implementation.
• Domain: app/identity/domain/entities.py, app/identity/domain/value_objects.py, app/identity/domain/events.py
• Shared event mechanism: app/shared/domain/event_bus.py
• Profile contracts: app/profiles/domain/repositories.py
Persistence and Transactions
PostgreSQL is accessed asynchronously through SQLAlchemy 2.x and asyncpg. Shared database infrastructure owns session/transaction setup; context-specific SQLAlchemy models and repositories map between persistence records and domain objects. The Unit of Work boundary is exercised by application use cases/repositories rather than exposed to presentation.
• Database boundary: app/shared/infrastructure/database.py
• Identity persistence: app/identity/infrastructure/models.py, app/identity/infrastructure/repository.py
• Profile persistence: app/profiles/infrastructure/models.py, app/profiles/infrastructure/repositories.py
Authentication and Security
Identity infrastructure owns password hashing and JWT issuance/validation, keeping cryptographic details outside domain logic. Presentation dependencies use these services to authenticate protected requests; logout persists or otherwise records token-blacklisting state through identity persistence.
• Security adapter: app/identity/infrastructure/security.py
• Auth wiring: app/identity/presentation/dependencies.py
• Identity storage models: app/identity/infrastructure/models.py
Runtime and Schema Operations
Configuration is environment-driven, including async runtime and synchronous migration database URLs plus JWT secret. PostgreSQL 16 is the material external service. Alembic maintains schema history, including identity tables and token-expiry fixes; Compose provides application, migration, test, debug, and database service topology.
• Settings: app/shared/config.py
• Migration environment/history: alembic/env.py, alembic/versions/bea8099fe81f_init.py, alembic/versions/f8640513a824_add_identity_tables.py
• Deployment topology: compose.yaml, Dockerfile
Main entry points: router.py Identity HTTP router · use_cases.py Identity use cases · compose.yaml Container & Compose · main.py FastAPI application
Components 20
Runtime
- Container & Compose · Runs the API, migrations, and PostgreSQL topology.
compose.yaml - FastAPI application
app/main.py - PostgreSQL 16
- Alembic migrations · Maintains identity schema history.
alembic/env.py
Identity context
- Identity HTTP router · Registration, login, logout, and current-user endpoints.
app/identity/presentation/router.py - Auth dependencies · Resolves authenticated users and application services.
app/identity/presentation/dependencies.py - Identity exception handlers
app/identity/presentation/exception_handlers.py - Identity use cases · Orchestrates authentication and transaction work.
app/identity/application/use_cases.py - Identity DTOs
app/identity/application/dto.py - Identity domain model · Entities and value objects enforce identity invariants.
app/identity/domain/entities.py - Identity domain events
app/identity/domain/events.py - JWT & password security · Hashes passwords and issues or validates JWTs.
app/identity/infrastructure/security.py - Identity repository · Maps identity domain objects to storage records.
app/identity/infrastructure/repository.py - Identity SQLAlchemy models · Includes identity and token-blacklist storage.
app/identity/infrastructure/models.py
Profiles context
Parallel emerging bounded context.
- Profile use cases
app/profiles/application/use_cases.py - Profile repository contracts
app/profiles/domain/repositories.py - Profile repository
app/profiles/infrastructure/repositories.py
Shared platform
- Async database boundary · Owns SQLAlchemy async sessions and transactions.
app/shared/infrastructure/database.py - In-memory event bus · Delivers domain-event side effects within the process.
app/shared/domain/event_bus.py - Shared HTTP error handlers
app/shared/presentation/exception_handlers.py
Connections 22
- Container & Compose → FastAPI application: runs
- Container & Compose → PostgreSQL 16: provisions
- Container & Compose → Alembic migrations: runs migrations
- Alembic migrations → PostgreSQL 16: updates schema
- FastAPI application → Identity HTTP router: mounts
- FastAPI application → Shared HTTP error handlers: registers
- Identity HTTP router → Auth dependencies: uses
- Identity HTTP router → Identity DTOs: maps requests to
- Identity HTTP router → Identity use cases: invokes
- Identity HTTP router → Identity exception handlers: translates failures
- Auth dependencies → JWT & password security: authenticates with
- Identity use cases → Identity domain model: applies invariants
- Identity use cases → Identity repository: persists through
- Identity use cases → JWT & password security: uses
- Identity domain model → Identity domain events: emits
- Identity domain events → In-memory event bus: publishes via
- Identity repository → Identity SQLAlchemy models: maps
- Identity repository → Async database boundary: uses sessions
- Async database boundary → PostgreSQL 16: connects to
- Profile use cases → Profile repository contracts: depends on
- Profile use cases → Profile repository: uses implementation
- Profile repository → Async database boundary: uses sessions