# ankush-hegde/url-shortener architecture

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

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

## Overview

This repository implements a URL-shortening HTTP service. A client submits a long URL; the service validates it, reuses or creates a short-code mapping, and returns a short URL. On a short-code request, the service checks Redis, falls back to MongoDB on a miss or cache error, and returns a redirect response. The README also identifies the OpenAPI/Swagger interface and database caching as product capabilities. The graph follows shown wiring; generated-controller internals and some documentation asset serving are not expanded beyond the excerpts.

Main entry points: [`cmd/url_shortener/main.go`](https://github.com/ankush-hegde/url-shortener/blob/main/cmd/url_shortener/main.go) (HTTP server setup), [`internal/pkg/server/service/app_url_shorten_service_impl.go`](https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/server/service/app_url_shorten_service_impl.go) (Shortener service), [`internal/app/resolveshorturl/resolve_short_url.go`](https://github.com/ankush-hegde/url-shortener/blob/main/internal/app/resolveshorturl/resolve_short_url.go) (Resolve short code), [`internal/pkg/database/mongodb/db.go`](https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/database/mongodb/db.go) (MongoDB adapter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_http["HTTP interface"]
  node_server["HTTP server setup<br/>[main.go]"]
  node_middleware["Request middleware<br/>[middleware.go]"]
  node_routes["Generated API routes<br/>[routers.go]"]
  node_controller["API controller<br/>[api_default.go]"]
  node_service["Shortener service"]
  node_metrics["Prometheus endpoint<br/>[main.go]"]
end

subgraph group_domain["Short URL workflows"]
  node_create["Create short URL"]
  node_resolve["Resolve short code"]
  node_config["Runtime configuration<br/>[config.go]"]
end

subgraph group_storage["Mapping storage"]
  node_mongo_adapter["MongoDB adapter<br/>[db.go]"]
  node_mongo[("MongoDB mappings")]
  node_redis_adapter["Redis cache adapter<br/>[redis.go]"]
  node_redis[("Redis cache")]
end

subgraph group_docs["API documentation"]
  node_api_spec["OpenAPI specification"]
  node_swagger["Swagger UI setup"]
end

node_client(("HTTP client"))

node_client -->|"sends requests"| node_server
node_server -->|"registers controllers"| node_routes
node_server -->|"installs middleware"| node_middleware
node_routes -.->|"routes to handlers"| node_controller
node_controller -.->|"dispatches API calls"| node_service
node_service -->|"creates mappings"| node_create
node_service -->|"resolves codes"| node_resolve
node_server -->|"loads settings"| node_config
node_create -->|"queries and writes"| node_mongo_adapter
node_resolve -->|"checks and caches"| node_redis_adapter
node_resolve -->|"loads on miss"| node_mongo_adapter
node_mongo_adapter -->|"reads and writes"| node_mongo
node_redis_adapter -->|"reads and writes"| node_redis
node_server -->|"registers endpoint"| node_metrics
node_server -->|"serves UI assets"| node_swagger
node_swagger -->|"loads specification"| node_api_spec

click node_server "https://github.com/ankush-hegde/url-shortener/blob/main/cmd/url_shortener/main.go"
click node_middleware "https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/server/middleware/middleware.go"
click node_routes "https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/server/gen/routers.go"
click node_controller "https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/server/gen/api_default.go"
click node_service "https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/server/service/app_url_shorten_service_impl.go"
click node_create "https://github.com/ankush-hegde/url-shortener/blob/main/internal/app/createshorturl/create_short_url.go"
click node_resolve "https://github.com/ankush-hegde/url-shortener/blob/main/internal/app/resolveshorturl/resolve_short_url.go"
click node_config "https://github.com/ankush-hegde/url-shortener/blob/main/config/config.go"
click node_mongo_adapter "https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/database/mongodb/db.go"
click node_redis_adapter "https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/redis/redis.go"
click node_api_spec "https://github.com/ankush-hegde/url-shortener/blob/main/api/url-shortener/url-shortener.openapi.yaml"
click node_swagger "https://github.com/ankush-hegde/url-shortener/blob/main/api/url-shortener/swagger-initializer.js"
click node_metrics "https://github.com/ankush-hegde/url-shortener/blob/main/cmd/url_shortener/main.go"

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_server,node_middleware,node_routes,node_controller,node_service,node_metrics,node_client toneBlue
class node_create,node_resolve,node_config toneAmber
class node_mongo_adapter,node_mongo,node_redis_adapter,node_redis toneMint
class node_api_spec,node_swagger toneRose
```

## Components

### HTTP interface

- [HTTP server setup](https://github.com/ankush-hegde/url-shortener/blob/main/cmd/url_shortener/main.go): `cmd/url_shortener/main.go`
- [Request middleware](https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/server/middleware/middleware.go): `internal/pkg/server/middleware/middleware.go`
- [Generated API routes](https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/server/gen/routers.go): `internal/pkg/server/gen/routers.go`
- [API controller](https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/server/gen/api_default.go): `internal/pkg/server/gen/api_default.go`
- [Shortener service](https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/server/service/app_url_shorten_service_impl.go): `internal/pkg/server/service/app_url_shorten_service_impl.go`
- [Prometheus endpoint](https://github.com/ankush-hegde/url-shortener/blob/main/cmd/url_shortener/main.go): `cmd/url_shortener/main.go`

### Short URL workflows

- [Create short URL](https://github.com/ankush-hegde/url-shortener/blob/main/internal/app/createshorturl/create_short_url.go): `internal/app/createshorturl/create_short_url.go`
- [Resolve short code](https://github.com/ankush-hegde/url-shortener/blob/main/internal/app/resolveshorturl/resolve_short_url.go): `internal/app/resolveshorturl/resolve_short_url.go`
- [Runtime configuration](https://github.com/ankush-hegde/url-shortener/blob/main/config/config.go): `config/config.go`

### Mapping storage

- [MongoDB adapter](https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/database/mongodb/db.go): `internal/pkg/database/mongodb/db.go`
- **MongoDB mappings**
- [Redis cache adapter](https://github.com/ankush-hegde/url-shortener/blob/main/internal/pkg/redis/redis.go): `internal/pkg/redis/redis.go`
- **Redis cache**

### API documentation

- [OpenAPI specification](https://github.com/ankush-hegde/url-shortener/blob/main/api/url-shortener/url-shortener.openapi.yaml): `api/url-shortener/url-shortener.openapi.yaml`
- [Swagger UI setup](https://github.com/ankush-hegde/url-shortener/blob/main/api/url-shortener/swagger-initializer.js): `api/url-shortener/swagger-initializer.js`

### Other components

- **HTTP client**

## Connections

- HTTP client → HTTP server setup: sends requests (evidence: `cmd/url_shortener/main.go`)
- HTTP server setup → Generated API routes: registers controllers (evidence: `cmd/url_shortener/main.go`)
- HTTP server setup → Request middleware: installs middleware (evidence: `cmd/url_shortener/main.go`)
- Generated API routes → API controller: routes to handlers
- API controller → Shortener service: dispatches API calls
- Shortener service → Create short URL: creates mappings (evidence: `internal/pkg/server/service/app_url_shorten_service_impl.go`)
- Shortener service → Resolve short code: resolves codes (evidence: `internal/pkg/server/service/app_url_shorten_service_impl.go`)
- HTTP server setup → Runtime configuration: loads settings (evidence: `cmd/url_shortener/main.go`)
- Create short URL → MongoDB adapter: queries and writes (evidence: `internal/app/createshorturl/create_short_url.go`)
- Resolve short code → Redis cache adapter: checks and caches (evidence: `internal/app/resolveshorturl/resolve_short_url.go`)
- Resolve short code → MongoDB adapter: loads on miss (evidence: `internal/app/resolveshorturl/resolve_short_url.go`)
- MongoDB adapter → MongoDB mappings: reads and writes (evidence: `internal/pkg/database/mongodb/db.go`)
- Redis cache adapter → Redis cache: reads and writes (evidence: `internal/pkg/redis/redis.go`)
- HTTP server setup → Prometheus endpoint: registers endpoint (evidence: `cmd/url_shortener/main.go`)
- HTTP server setup → Swagger UI setup: serves UI assets (evidence: `cmd/url_shortener/main.go`)
- Swagger UI setup → OpenAPI specification: loads specification (evidence: `api/url-shortener/swagger-initializer.js`)
