# vanthao03596/clean-architecture-fastapi architecture

> Architecture diagram of the GitHub repository vanthao03596/clean-architecture-fastapi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vanthao03596/clean-architecture-fastapi
- Repository: https://github.com/vanthao03596/clean-architecture-fastapi

## Overview

This is a Clean Architecture FastAPI service for user management and JWT authentication. Clients call versioned HTTP routes; user requests flow through application use cases to transactional repositories, while authentication handles credentials, token validation, refresh rotation, and revocation. The composition root wires application services to infrastructure implementations. The README also documents async PostgreSQL persistence, dependency injection, and API error handling. Some implementation files were not excerpted, so edges reflect only shown calls, indexed references, or documented architecture.

Main entry points: [`app/presentation/dependencies.py`](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/presentation/dependencies.py) (Dependency wiring), [`app/application/services/auth_service.py`](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/services/auth_service.py) (Auth use cases), [`app/application/services/user_service.py`](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/services/user_service.py) (User use cases), [`app/main.py`](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/main.py) (FastAPI app)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_presentation["HTTP presentation"]
  node_main["FastAPI app<br/>[main.py]"]
  node_auth_routes["Auth routes<br/>[auth.py]"]
  node_user_routes["User routes<br/>[users.py]"]
  node_error_handling["Error handling"]
  node_composition["Dependency wiring<br/>[dependencies.py]"]
end

subgraph group_application["Use cases"]
  node_auth_service["Auth use cases<br/>[auth_service.py]"]
  node_user_service["User use cases<br/>[user_service.py]"]
  node_auth_dto["Auth DTOs<br/>[auth_dto.py]"]
  node_user_dto["User DTOs<br/>[user_dto.py]"]
  node_app_errors["Application errors<br/>[exceptions.py]"]
end

subgraph group_domain["Business contracts"]
  node_user_entity["User entity<br/>[user.py]"]
  node_user_repository["User repository contract<br/>[user_repository.py]"]
  node_uow_contract["Transaction contract<br/>[unit_of_work.py]"]
  node_token_contract["Token repository contract"]
  node_token_service_contract["Token service contract<br/>[token_service.py]"]
end

subgraph group_infrastructure["Infrastructure"]
  node_settings["Settings<br/>[settings.py]"]
  node_database[("Async database<br/>[database.py]")]
  node_user_model["User ORM model<br/>[user_model.py]"]
  node_user_repo_impl["User repository"]
  node_uow_impl["Transaction manager"]
  node_token_repo_impl["Token storage"]
  node_jwt_service["JWT service"]
end

node_client(("API client"))

node_client -->|"sends HTTP"| node_main
node_main -->|"registers routes"| node_auth_routes
node_main -->|"registers routes"| node_user_routes
node_main -->|"registers handlers"| node_error_handling
node_auth_routes -->|"invokes"| node_auth_service
node_user_routes -->|"invokes"| node_user_service
node_composition -->|"constructs"| node_auth_service
node_composition -->|"constructs"| node_user_service
node_composition -->|"creates sessions"| node_database
node_composition -->|"loads config"| node_settings
node_composition -->|"constructs"| node_uow_impl
node_composition -->|"constructs"| node_token_repo_impl
node_composition -->|"constructs"| node_jwt_service
node_auth_service -->|"maps user data"| node_user_dto
node_auth_service -->|"uses token DTOs"| node_auth_dto
node_auth_service -->|"uses transactions"| node_uow_contract
node_auth_service -->|"tracks tokens"| node_token_contract
node_auth_service -->|"validates tokens"| node_token_service_contract
node_user_service -->|"maps user data"| node_user_dto
node_user_service -->|"uses transactions"| node_uow_contract
node_user_service -->|"creates and updates"| node_user_entity
node_user_entity -->|"repository contract uses"| node_user_repository
node_uow_contract -->|"exposes"| node_user_repository
node_user_repo_impl -->|"implements"| node_user_repository
node_user_repo_impl -->|"persists as"| node_user_model
node_user_model -->|"uses ORM base"| node_database
node_uow_impl -->|"implements"| node_uow_contract
node_uow_impl -->|"provides repository"| node_user_repo_impl
node_token_repo_impl -->|"implements"| node_token_contract
node_jwt_service -->|"implements"| node_token_service_contract
node_jwt_service -->|"tracks revocation"| node_token_repo_impl

click node_main "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/main.py"
click node_auth_routes "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/presentation/api/v1/auth.py"
click node_user_routes "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/presentation/api/v1/users.py"
click node_error_handling "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/presentation/exception_handlers.py"
click node_auth_service "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/services/auth_service.py"
click node_user_service "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/services/user_service.py"
click node_auth_dto "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/dtos/auth_dto.py"
click node_user_dto "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/dtos/user_dto.py"
click node_app_errors "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/exceptions/exceptions.py"
click node_user_entity "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/domain/entities/user.py"
click node_user_repository "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/domain/repositories/user_repository.py"
click node_uow_contract "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/domain/repositories/unit_of_work.py"
click node_token_contract "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/domain/repositories/token_repository.py"
click node_token_service_contract "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/domain/services/token_service.py"
click node_composition "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/presentation/dependencies.py"
click node_settings "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/config/settings.py"
click node_database "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/persistence/database.py"
click node_user_model "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/persistence/models/user_model.py"
click node_user_repo_impl "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/repositories/user_repository_impl.py"
click node_uow_impl "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/repositories/unit_of_work_impl.py"
click node_token_repo_impl "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/repositories/token_repository_impl.py"
click node_jwt_service "https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/security/jwt_token_service.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_auth_routes,node_user_routes,node_error_handling,node_composition,node_client toneBlue
class node_auth_service,node_user_service,node_auth_dto,node_user_dto,node_app_errors toneAmber
class node_user_entity,node_user_repository,node_uow_contract,node_token_contract,node_token_service_contract toneMint
class node_settings,node_database,node_user_model,node_user_repo_impl,node_uow_impl,node_token_repo_impl,node_jwt_service toneRose
```

## Components

### HTTP presentation

- [FastAPI app](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/main.py): `app/main.py`
- [Auth routes](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/presentation/api/v1/auth.py): `app/presentation/api/v1/auth.py`
- [User routes](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/presentation/api/v1/users.py): `app/presentation/api/v1/users.py`
- [Error handling](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/presentation/exception_handlers.py): `app/presentation/exception_handlers.py`
- [Dependency wiring](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/presentation/dependencies.py): `app/presentation/dependencies.py`

### Use cases

- [Auth use cases](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/services/auth_service.py): `app/application/services/auth_service.py`
- [User use cases](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/services/user_service.py): `app/application/services/user_service.py`
- [Auth DTOs](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/dtos/auth_dto.py): `app/application/dtos/auth_dto.py`
- [User DTOs](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/dtos/user_dto.py): `app/application/dtos/user_dto.py`
- [Application errors](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/application/exceptions/exceptions.py): `app/application/exceptions/exceptions.py`

### Business contracts

- [User entity](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/domain/entities/user.py): `app/domain/entities/user.py`
- [User repository contract](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/domain/repositories/user_repository.py): `app/domain/repositories/user_repository.py`
- [Transaction contract](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/domain/repositories/unit_of_work.py): `app/domain/repositories/unit_of_work.py`
- [Token repository contract](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/domain/repositories/token_repository.py): `app/domain/repositories/token_repository.py`
- [Token service contract](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/domain/services/token_service.py): `app/domain/services/token_service.py`

### Infrastructure

- [Settings](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/config/settings.py): `app/infrastructure/config/settings.py`
- [Async database](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/persistence/database.py): `app/infrastructure/persistence/database.py`
- [User ORM model](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/persistence/models/user_model.py): `app/infrastructure/persistence/models/user_model.py`
- [User repository](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/repositories/user_repository_impl.py): `app/infrastructure/repositories/user_repository_impl.py`
- [Transaction manager](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/repositories/unit_of_work_impl.py): `app/infrastructure/repositories/unit_of_work_impl.py`
- [Token storage](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/repositories/token_repository_impl.py): `app/infrastructure/repositories/token_repository_impl.py`
- [JWT service](https://github.com/vanthao03596/clean-architecture-fastapi/blob/main/app/infrastructure/security/jwt_token_service.py): `app/infrastructure/security/jwt_token_service.py`

### Other components

- **API client**

## Connections

- API client → FastAPI app: sends HTTP
- FastAPI app → Auth routes: registers routes (evidence: `app/main.py`)
- FastAPI app → User routes: registers routes (evidence: `app/main.py`)
- FastAPI app → Error handling: registers handlers (evidence: `app/main.py`)
- Auth routes → Auth use cases: invokes (evidence: `app/presentation/api/v1/auth.py`)
- User routes → User use cases: invokes (evidence: `app/presentation/api/v1/users.py`)
- Dependency wiring → Auth use cases: constructs (evidence: `app/presentation/dependencies.py`)
- Dependency wiring → User use cases: constructs (evidence: `app/presentation/dependencies.py`)
- Dependency wiring → Async database: creates sessions (evidence: `app/presentation/dependencies.py`)
- Dependency wiring → Settings: loads config (evidence: `app/presentation/dependencies.py`)
- Dependency wiring → Transaction manager: constructs (evidence: `app/presentation/dependencies.py`)
- Dependency wiring → Token storage: constructs (evidence: `app/presentation/dependencies.py`)
- Dependency wiring → JWT service: constructs (evidence: `app/presentation/dependencies.py`)
- Auth use cases → User DTOs: maps user data (evidence: `app/application/services/auth_service.py`)
- Auth use cases → Auth DTOs: uses token DTOs (evidence: `app/application/services/auth_service.py`)
- Auth use cases → Transaction contract: uses transactions (evidence: `app/application/services/auth_service.py`)
- Auth use cases → Token repository contract: tracks tokens (evidence: `app/application/services/auth_service.py`)
- Auth use cases → Token service contract: validates tokens (evidence: `app/application/services/auth_service.py`)
- User use cases → User DTOs: maps user data (evidence: `app/application/services/user_service.py`)
- User use cases → Transaction contract: uses transactions (evidence: `app/application/services/user_service.py`)
- User use cases → User entity: creates and updates (evidence: `app/application/services/user_service.py`)
- User entity → User repository contract: repository contract uses (evidence: `app/domain/repositories/user_repository.py`)
- Transaction contract → User repository contract: exposes (evidence: `app/domain/repositories/unit_of_work.py`)
- User repository → User repository contract: implements (evidence: `app/infrastructure/repositories/user_repository_impl.py`)
- User repository → User ORM model: persists as (evidence: `app/infrastructure/repositories/user_repository_impl.py`)
- User ORM model → Async database: uses ORM base (evidence: `app/infrastructure/persistence/models/user_model.py`)
- Transaction manager → Transaction contract: implements (evidence: `app/infrastructure/repositories/unit_of_work_impl.py`)
- Transaction manager → User repository: provides repository (evidence: `app/infrastructure/repositories/unit_of_work_impl.py`)
- Token storage → Token repository contract: implements (evidence: `app/infrastructure/repositories/token_repository_impl.py`)
- JWT service → Token service contract: implements (evidence: `app/infrastructure/security/jwt_token_service.py`)
- JWT service → Token storage: tracks revocation (evidence: `app/infrastructure/security/jwt_token_service.py`)
