# nxd010/url-shortener architecture

> Architecture diagram of the GitHub repository nxd010/url-shortener, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nxd010/url-shortener
- Repository: https://github.com/nxd010/url-shortener

## Overview

URL Shortener is a FastAPI service for registration/login, URL creation, and short-code redirects. Its principal flow is HTTP routing through business services into repositories, with links read through Redis caching and persisted in PostgreSQL; redirect handling also records clicks asynchronously. The graph preserves authentication, expiration/status validation, Base62 code generation, and click tracking. Relationships are limited to calls and integrations visible in excerpts, the source index, or README; unsampled modules are represented only where their role or connection is documented.

Main entry points: [`app/services/link_service.py`](https://github.com/nxd010/url-shortener/blob/main/app/services/link_service.py) (Link Service), [`app/routers/redirect.py`](https://github.com/nxd010/url-shortener/blob/main/app/routers/redirect.py) (Redirect Route), [`app/repositories/link_repository.py`](https://github.com/nxd010/url-shortener/blob/main/app/repositories/link_repository.py) (Link Repository), [`app/main.py`](https://github.com/nxd010/url-shortener/blob/main/app/main.py) (FastAPI App)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_app["FastAPI App<br/>[main.py]"]
  node_auth_router["Auth Routes<br/>[auth.py]"]
  node_link_router["Link Routes<br/>[links.py]"]
  node_redirect_router["Redirect Route<br/>[redirect.py]"]
  node_link_schema["Link Schemas<br/>[link.py]"]
  node_user_schema["User Schemas<br/>[user.py]"]
end

subgraph group_domain["Domain Services"]
  node_link_service["Link Service<br/>[link_service.py]"]
  node_user_service["User Service<br/>[user_service.py]"]
  node_click_service["Click Service<br/>[click_service.py]"]
  node_base62["Base62 Encoder<br/>[base62.py]"]
  node_exceptions["Link Errors<br/>[exceptions.py]"]
end

subgraph group_data["Persistence"]
  node_link_repo["Link Repository<br/>[link_repository.py]"]
  node_user_repo["User Repository<br/>[user_repository.py]"]
  node_click_repo["Click Repository"]
  node_link_model["Link Model<br/>[link.py]"]
  node_user_model["User Model<br/>[user.py]"]
  node_click_model["Click Model<br/>[click.py]"]
  node_model_base["ORM Base<br/>[base.py]"]
  node_database["DB Sessions<br/>[database.py]"]
end

subgraph group_security["Authentication"]
  node_security["JWT And Passwords<br/>[security.py]"]
  node_config["App Configuration<br/>[config.py]"]
end

node_client(("API Client"))
node_postgres[("PostgreSQL")]
node_redis_cache[("Redis Cache")]

node_client -->|"sends requests"| node_app
node_app -->|"registers routes"| node_auth_router
node_app -->|"registers routes"| node_link_router
node_app -->|"registers routes"| node_redirect_router
node_auth_router -->|"calls"| node_user_service
node_auth_router -->|"accepts and returns"| node_user_schema
node_auth_router -->|"creates token"| node_security
node_link_router -->|"calls"| node_link_service
node_link_router -->|"uses contracts"| node_link_schema
node_redirect_router -->|"validates links"| node_link_service
node_redirect_router -->|"schedules tracking"| node_click_service
node_redirect_router -->|"constructs repository"| node_link_repo
node_redirect_router -->|"constructs repository"| node_click_repo
node_link_service -->|"reads and writes"| node_link_repo
node_link_service -->|"encodes IDs"| node_base62
node_link_service -->|"accepts input"| node_link_schema
node_link_service -->|"raises status errors"| node_exceptions
node_user_service -->|"reads and writes"| node_user_repo
node_user_service -->|"accepts input"| node_user_schema
node_user_service -->|"hashes and verifies"| node_security
node_click_service -->|"persists clicks"| node_click_repo
node_link_repo -->|"queries and persists"| node_link_model
node_link_repo -->|"reads and caches"| node_redis_cache
node_user_repo -->|"queries and persists"| node_user_model
node_click_repo -->|"persists"| node_click_model
node_link_repo -->|"uses session"| node_database
node_user_repo -->|"uses session"| node_database
node_click_repo -->|"uses session"| node_database
node_database -->|"opens sessions"| node_postgres
node_link_model -->|"inherits"| node_model_base
node_user_model -->|"inherits"| node_model_base
node_click_model -->|"inherits"| node_model_base
node_security -->|"loads signing key"| node_config

click node_app "https://github.com/nxd010/url-shortener/blob/main/app/main.py"
click node_auth_router "https://github.com/nxd010/url-shortener/blob/main/app/routers/auth.py"
click node_link_router "https://github.com/nxd010/url-shortener/blob/main/app/routers/links.py"
click node_redirect_router "https://github.com/nxd010/url-shortener/blob/main/app/routers/redirect.py"
click node_link_schema "https://github.com/nxd010/url-shortener/blob/main/app/schemas/link.py"
click node_user_schema "https://github.com/nxd010/url-shortener/blob/main/app/schemas/user.py"
click node_link_service "https://github.com/nxd010/url-shortener/blob/main/app/services/link_service.py"
click node_user_service "https://github.com/nxd010/url-shortener/blob/main/app/services/user_service.py"
click node_click_service "https://github.com/nxd010/url-shortener/blob/main/app/services/click_service.py"
click node_base62 "https://github.com/nxd010/url-shortener/blob/main/app/utils/base62.py"
click node_exceptions "https://github.com/nxd010/url-shortener/blob/main/app/exceptions.py"
click node_link_repo "https://github.com/nxd010/url-shortener/blob/main/app/repositories/link_repository.py"
click node_user_repo "https://github.com/nxd010/url-shortener/blob/main/app/repositories/user_repository.py"
click node_click_repo "https://github.com/nxd010/url-shortener/blob/main/app/repositories/click_repository.py"
click node_link_model "https://github.com/nxd010/url-shortener/blob/main/app/models/link.py"
click node_user_model "https://github.com/nxd010/url-shortener/blob/main/app/models/user.py"
click node_click_model "https://github.com/nxd010/url-shortener/blob/main/app/models/click.py"
click node_model_base "https://github.com/nxd010/url-shortener/blob/main/app/models/base.py"
click node_database "https://github.com/nxd010/url-shortener/blob/main/app/database.py"
click node_security "https://github.com/nxd010/url-shortener/blob/main/app/security.py"
click node_config "https://github.com/nxd010/url-shortener/blob/main/app/config.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_app,node_auth_router,node_link_router,node_redirect_router,node_link_schema,node_user_schema,node_client toneBlue
class node_link_service,node_user_service,node_click_service,node_base62,node_exceptions,node_postgres,node_redis_cache toneAmber
class node_link_repo,node_user_repo,node_click_repo,node_link_model,node_user_model,node_click_model,node_model_base,node_database toneMint
class node_security,node_config toneRose
```

## Components

### HTTP API

- [FastAPI App](https://github.com/nxd010/url-shortener/blob/main/app/main.py): `app/main.py`
- [Auth Routes](https://github.com/nxd010/url-shortener/blob/main/app/routers/auth.py): `app/routers/auth.py`
- [Link Routes](https://github.com/nxd010/url-shortener/blob/main/app/routers/links.py): `app/routers/links.py`
- [Redirect Route](https://github.com/nxd010/url-shortener/blob/main/app/routers/redirect.py): `app/routers/redirect.py`
- [Link Schemas](https://github.com/nxd010/url-shortener/blob/main/app/schemas/link.py): `app/schemas/link.py`
- [User Schemas](https://github.com/nxd010/url-shortener/blob/main/app/schemas/user.py): `app/schemas/user.py`

### Domain Services

- [Link Service](https://github.com/nxd010/url-shortener/blob/main/app/services/link_service.py): `app/services/link_service.py`
- [User Service](https://github.com/nxd010/url-shortener/blob/main/app/services/user_service.py): `app/services/user_service.py`
- [Click Service](https://github.com/nxd010/url-shortener/blob/main/app/services/click_service.py): `app/services/click_service.py`
- [Base62 Encoder](https://github.com/nxd010/url-shortener/blob/main/app/utils/base62.py): `app/utils/base62.py`
- [Link Errors](https://github.com/nxd010/url-shortener/blob/main/app/exceptions.py): `app/exceptions.py`

### Persistence

- [Link Repository](https://github.com/nxd010/url-shortener/blob/main/app/repositories/link_repository.py): `app/repositories/link_repository.py`
- [User Repository](https://github.com/nxd010/url-shortener/blob/main/app/repositories/user_repository.py): `app/repositories/user_repository.py`
- [Click Repository](https://github.com/nxd010/url-shortener/blob/main/app/repositories/click_repository.py): `app/repositories/click_repository.py`
- [Link Model](https://github.com/nxd010/url-shortener/blob/main/app/models/link.py): `app/models/link.py`
- [User Model](https://github.com/nxd010/url-shortener/blob/main/app/models/user.py): `app/models/user.py`
- [Click Model](https://github.com/nxd010/url-shortener/blob/main/app/models/click.py): `app/models/click.py`
- [ORM Base](https://github.com/nxd010/url-shortener/blob/main/app/models/base.py): `app/models/base.py`
- [DB Sessions](https://github.com/nxd010/url-shortener/blob/main/app/database.py): `app/database.py`

### Authentication

- [JWT And Passwords](https://github.com/nxd010/url-shortener/blob/main/app/security.py): `app/security.py`
- [App Configuration](https://github.com/nxd010/url-shortener/blob/main/app/config.py): `app/config.py`

### Other components

- **API Client**
- **PostgreSQL**
- **Redis Cache**

## Connections

- API Client → FastAPI App: sends requests (evidence: `README.md`)
- FastAPI App → Auth Routes: registers routes (evidence: `app/main.py`)
- FastAPI App → Link Routes: registers routes (evidence: `app/main.py`)
- FastAPI App → Redirect Route: registers routes (evidence: `app/main.py`)
- Auth Routes → User Service: calls (evidence: `app/routers/auth.py`)
- Auth Routes → User Schemas: accepts and returns (evidence: `app/routers/auth.py`)
- Auth Routes → JWT And Passwords: creates token (evidence: `app/routers/auth.py`)
- Link Routes → Link Service: calls (evidence: `app/routers/links.py`)
- Link Routes → Link Schemas: uses contracts (evidence: `app/routers/links.py`)
- Redirect Route → Link Service: validates links (evidence: `app/routers/redirect.py`)
- Redirect Route → Click Service: schedules tracking (evidence: `app/routers/redirect.py`)
- Redirect Route → Link Repository: constructs repository (evidence: `app/routers/redirect.py`)
- Redirect Route → Click Repository: constructs repository (evidence: `app/routers/redirect.py`)
- Link Service → Link Repository: reads and writes (evidence: `app/services/link_service.py`)
- Link Service → Base62 Encoder: encodes IDs (evidence: `app/services/link_service.py`)
- Link Service → Link Schemas: accepts input (evidence: `app/services/link_service.py`)
- Link Service → Link Errors: raises status errors (evidence: `app/services/link_service.py`)
- User Service → User Repository: reads and writes (evidence: `app/services/user_service.py`)
- User Service → User Schemas: accepts input (evidence: `app/services/user_service.py`)
- User Service → JWT And Passwords: hashes and verifies (evidence: `app/services/user_service.py`)
- Click Service → Click Repository: persists clicks (evidence: `app/services/click_service.py`)
- Link Repository → Link Model: queries and persists (evidence: `app/repositories/link_repository.py`)
- Link Repository → Redis Cache: reads and caches (evidence: `app/repositories/link_repository.py`)
- User Repository → User Model: queries and persists (evidence: `app/repositories/user_repository.py`)
- Click Repository → Click Model: persists (evidence: `app/repositories/click_repository.py`)
- Link Repository → DB Sessions: uses session (evidence: `app/repositories/link_repository.py`)
- User Repository → DB Sessions: uses session (evidence: `app/repositories/user_repository.py`)
- Click Repository → DB Sessions: uses session (evidence: `app/repositories/click_repository.py`)
- DB Sessions → PostgreSQL: opens sessions (evidence: `app/database.py`)
- Link Model → ORM Base: inherits (evidence: `app/models/link.py`)
- User Model → ORM Base: inherits (evidence: `app/models/user.py`)
- Click Model → ORM Base: inherits (evidence: `app/models/click.py`)
- JWT And Passwords → App Configuration: loads signing key (evidence: `app/security.py`)
