# shubhankarraj9/url-shortener-project architecture

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

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

## Overview

This is a FastAPI URL-shortening service. Users register and authenticate, create links, and request public redirects; admins can manage users and links. Redirects resolve through a Redis cache with PostgreSQL fallback, while clicks are buffered and flushed exactly once. The repository also documents Traefik ingress and a Prometheus/Grafana/Alertmanager stack. Some infrastructure and route implementations were not sampled, so those relationships are included only where the README documents them.

Main entry points: [`app/main.py`](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/main.py) (Application startup), [`app/routers/auth.py`](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/auth.py) (Authentication routes), [`app/services/links.py`](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/services/links.py) (Link creation and lookup), [`app/services/clicks.py`](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/services/clicks.py) (Click recording and flush)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_edge["Ingress and monitoring"]
  node_traefik["Traefik"]
  node_prometheus["Prometheus"]
  node_grafana["Grafana"]
  node_alertmanager["Alertmanager"]
end

subgraph group_api["API and identity"]
  node_app_main["Application startup<br/>[main.py]"]
  node_auth_router["Authentication routes<br/>[auth.py]"]
  node_user_router["User routes<br/>[users.py]"]
  node_admin_router["Admin routes<br/>[admin.py]"]
  node_access_control["Identity and access<br/>[deps.py]"]
  node_security["Password and JWT<br/>[security.py]"]
  node_tokens["Email tokens<br/>[tokens.py]"]
  node_email["Outbound email<br/>[emailer.py]"]
end

subgraph group_links["Links and redirects"]
  node_link_router["Link management<br/>[links.py]"]
  node_link_service["Link creation and lookup<br/>[links.py]"]
  node_redirect_router["Public redirects<br/>[redirect.py]"]
  node_click_service["Click recording and flush<br/>[clicks.py]"]
end

subgraph group_state["Persistence and telemetry"]
  node_postgres[("PostgreSQL")]
  node_redis[("Redis")]
  node_metrics["Application metrics<br/>[metrics.py]"]
end

node_visitor(("Internet user"))
node_admin_actor(("Administrator"))

node_visitor -->|"sends requests"| node_traefik
node_admin_actor -->|"uses admin UI"| node_traefik
node_traefik -->|"routes API traffic"| node_app_main
node_prometheus -->|"scrapes metrics"| node_app_main
node_prometheus -->|"sends alerts"| node_alertmanager
node_prometheus -->|"supplies dashboards"| node_grafana
node_app_main -->|"registers routes"| node_auth_router
node_app_main -->|"registers routes"| node_user_router
node_app_main -->|"registers routes"| node_admin_router
node_app_main -->|"registers routes"| node_link_router
node_app_main -->|"registers routes"| node_redirect_router
node_auth_router -->|"checks credentials"| node_security
node_auth_router -->|"issues tokens"| node_tokens
node_auth_router -->|"sends email"| node_email
node_auth_router -->|"uses dependencies"| node_access_control
node_user_router -->|"requires identity"| node_access_control
node_admin_router -->|"requires admin"| node_access_control
node_admin_router -->|"invalidates cache"| node_link_service
node_link_router -->|"creates and invalidates"| node_link_service
node_redirect_router -->|"resolves code"| node_link_service
node_redirect_router -->|"records click"| node_click_service
node_app_main -->|"starts flush loop"| node_click_service
node_link_service -->|"reads and writes links"| node_postgres
node_link_service -->|"caches resolutions"| node_redis
node_click_service -->|"buffers click stream"| node_redis
node_click_service -->|"flushes click counts"| node_postgres
node_app_main -->|"initializes database"| node_postgres
node_app_main -.->|"exposes metrics"| node_metrics

click node_traefik "https://github.com/shubhankarraj9/url-shortener-project/tree/main/traefik"
click node_prometheus "https://github.com/shubhankarraj9/url-shortener-project/tree/main/monitoring/prometheus"
click node_grafana "https://github.com/shubhankarraj9/url-shortener-project/tree/main/monitoring/grafana"
click node_alertmanager "https://github.com/shubhankarraj9/url-shortener-project/tree/main/monitoring/alertmanager"
click node_app_main "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/main.py"
click node_auth_router "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/auth.py"
click node_user_router "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/users.py"
click node_admin_router "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/admin.py"
click node_access_control "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/deps.py"
click node_security "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/security.py"
click node_tokens "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/services/tokens.py"
click node_email "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/emailer.py"
click node_link_router "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/links.py"
click node_link_service "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/services/links.py"
click node_redirect_router "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/redirect.py"
click node_click_service "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/services/clicks.py"
click node_metrics "https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/metrics.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_traefik,node_prometheus,node_grafana,node_alertmanager,node_visitor toneBlue
class node_app_main,node_auth_router,node_user_router,node_admin_router,node_access_control,node_security,node_tokens,node_email toneAmber
class node_link_router,node_link_service,node_redirect_router,node_click_service toneMint
class node_postgres,node_redis,node_metrics toneRose
class node_admin_actor toneIndigo
```

## Components

### Ingress and monitoring

- [Traefik](https://github.com/shubhankarraj9/url-shortener-project/tree/main/traefik): `traefik`
- [Prometheus](https://github.com/shubhankarraj9/url-shortener-project/tree/main/monitoring/prometheus): `monitoring/prometheus`
- [Grafana](https://github.com/shubhankarraj9/url-shortener-project/tree/main/monitoring/grafana): `monitoring/grafana`
- [Alertmanager](https://github.com/shubhankarraj9/url-shortener-project/tree/main/monitoring/alertmanager): `monitoring/alertmanager`

### API and identity

- [Application startup](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/main.py): `app/main.py`
- [Authentication routes](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/auth.py): `app/routers/auth.py`
- [User routes](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/users.py): `app/routers/users.py`
- [Admin routes](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/admin.py): `app/routers/admin.py`
- [Identity and access](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/deps.py): `app/deps.py`
- [Password and JWT](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/security.py): `app/security.py`
- [Email tokens](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/services/tokens.py): `app/services/tokens.py`
- [Outbound email](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/emailer.py): `app/emailer.py`

### Links and redirects

- [Link management](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/links.py): `app/routers/links.py`
- [Link creation and lookup](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/services/links.py): `app/services/links.py`
- [Public redirects](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/routers/redirect.py): `app/routers/redirect.py`
- [Click recording and flush](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/services/clicks.py): `app/services/clicks.py`

### Persistence and telemetry

- **PostgreSQL**
- **Redis**
- [Application metrics](https://github.com/shubhankarraj9/url-shortener-project/blob/main/app/metrics.py): `app/metrics.py`

### Other components

- **Internet user**
- **Administrator**

## Connections

- Internet user → Traefik: sends requests (evidence: `README.md`)
- Administrator → Traefik: uses admin UI (evidence: `README.md`)
- Traefik → Application startup: routes API traffic (evidence: `README.md`)
- Prometheus → Application startup: scrapes metrics (evidence: `README.md`)
- Prometheus → Alertmanager: sends alerts (evidence: `README.md`)
- Prometheus → Grafana: supplies dashboards (evidence: `README.md`)
- Application startup → Authentication routes: registers routes (evidence: `app/main.py`)
- Application startup → User routes: registers routes (evidence: `app/main.py`)
- Application startup → Admin routes: registers routes (evidence: `app/main.py`)
- Application startup → Link management: registers routes (evidence: `app/main.py`)
- Application startup → Public redirects: registers routes (evidence: `app/main.py`)
- Authentication routes → Password and JWT: checks credentials (evidence: `app/routers/auth.py`)
- Authentication routes → Email tokens: issues tokens (evidence: `app/routers/auth.py`)
- Authentication routes → Outbound email: sends email (evidence: `app/routers/auth.py`)
- Authentication routes → Identity and access: uses dependencies (evidence: `app/routers/auth.py`)
- User routes → Identity and access: requires identity (evidence: `app/routers/users.py`)
- Admin routes → Identity and access: requires admin (evidence: `app/routers/admin.py`)
- Admin routes → Link creation and lookup: invalidates cache (evidence: `app/routers/admin.py`)
- Link management → Link creation and lookup: creates and invalidates (evidence: `app/routers/links.py`)
- Public redirects → Link creation and lookup: resolves code (evidence: `app/routers/redirect.py`)
- Public redirects → Click recording and flush: records click (evidence: `app/routers/redirect.py`)
- Application startup → Click recording and flush: starts flush loop (evidence: `app/main.py`)
- Link creation and lookup → PostgreSQL: reads and writes links (evidence: `app/services/links.py`)
- Link creation and lookup → Redis: caches resolutions (evidence: `app/services/links.py`)
- Click recording and flush → Redis: buffers click stream (evidence: `app/services/clicks.py`)
- Click recording and flush → PostgreSQL: flushes click counts (evidence: `app/services/clicks.py`)
- Application startup → PostgreSQL: initializes database (evidence: `app/main.py`)
- Application startup → Application metrics: exposes metrics
