# arkadipsom-code/crispurl-shortener architecture

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

- Interactive diagram: https://gitdiagram.com/arkadipsom-code/crispurl-shortener
- Repository: https://github.com/arkadipsom-code/crispurl-shortener

## Overview

#### Purpose and deployment shape
CrispURL is a full-stack URL shortener with two runtimes: a vanilla-JS browser SPA and an Express REST/redirect service. The frontend is deployed independently (Vercel); the backend is deployed as an API gateway (Render) backed by Neon PostgreSQL.

- Frontend entry: `frontend/index.html`, `frontend/app.js`
- Backend process entry: `backend/server.js`

#### HTTP gateway and route boundary
Express configures middleware, CORS, API route mounting, and the public dynamic redirect path in `backend/server.js`. URL-specific endpoint definitions are isolated from server bootstrapping in `backend/routes/url.route.js`.

The primary external interface is:
- Write API: `POST /api/v1/urls/shorten`
- Read/redirect gateway: `GET /:shortCode`, returning HTTP 302 for resolved links.

#### Controller layer: request and response policy
The controller owns HTTP-facing concerns: parsing the shortening payload, validating that `longUrl` is structurally valid and uses `http:` or `https:`, delegating work, and selecting status/error responses.

- `backend/controllers/url.controller.js`
- `backend/routes/url.route.js`

This separates untrusted client input and Express request objects from database-oriented business logic.

#### URL service: persistence and consistency
The service implements the core write and read workflows against PostgreSQL.

- `backend/services/url.service.js`
- `backend/config/db.js`
- `backend/models/url.model.js`

For a new URL, it first deduplicates by `long_url`. New records are created under a database transaction: insert to obtain the sequential database ID, derive a code, then update that row’s `short_code` before commit. This transaction boundary protects the ID-to-code lifecycle and avoids collision-prone random token generation.

For redirects, it resolves by indexed `short_code`, triggers an atomic click-count increment asynchronously, and returns the destination URL for the controller/server to redirect.

#### Deterministic short-code generation
Short codes are not hashes. The Base62 module converts the database-generated numeric ID into an alphanumeric token using a 62-character alphabet.

- `backend/utils/base62.js`
- `backend/services/url.service.js`

The database sequence provides uniqueness; deterministic encoding compresses that unique ID into URL-safe text. PostgreSQL’s unique `short_code` constraint/index is the persistence-level safeguard assumed by the design.

#### Database and external infrastructure
A pooled Neon PostgreSQL connection is configured in `backend/config/db.js`. The architecture relies on PostgreSQL transactions for writes and indexed lookups for redirect reads. The expected `urls` record includes the long URL, generated short code, click count, and creation timestamp, represented in `backend/models/url.model.js`.

No cache, queue, authentication service, or external URL-validation service is indicated. Click tracking is intentionally decoupled from redirect completion so the read path prioritizes fast 302 responses.

#### Frontend submission flow
The frontend presents the shortening UI and asynchronously calls the backend shortening endpoint, then renders the returned short link.

- `frontend/index.html`
- `frontend/app.js`

Its boundary is strictly API consumption: URL creation occurs server-side, while direct browser navigation to a short code is handled by the backend redirect gateway rather than the SPA.

Main entry points: [`backend/services/url.service.js`](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/services/url.service.js) (URL service), [`backend/server.js`](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/server.js) (HTTP gateway), [`backend/controllers/url.controller.js`](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/controllers/url.controller.js) (Request controller), [`frontend/app.js`](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/frontend/app.js) (Submission client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Browser SPA"]
  node_ui["Shortening UI<br/>HTML UI<br/>[index.html]"]
  node_spa["Submission client<br/>vanilla JS<br/>[app.js]"]
end

subgraph group_backend["Express service"]
  node_server["HTTP gateway<br/>Express server<br/>[server.js]"]
  node_routes["URL routes<br/>Express routes<br/>[url.route.js]"]
  node_controller["Request controller<br/>HTTP policy<br/>[url.controller.js]"]
  node_service["URL service<br/>business logic<br/>[url.service.js]"]
  node_base62["Base62 encoder<br/>code generator<br/>[base62.js]"]
end

subgraph group_data["PostgreSQL persistence"]
  node_dbconfig["DB pool<br/>PostgreSQL client<br/>[db.js]"]
  node_model["URL record model<br/>data model<br/>[url.model.js]"]
  node_urls[("urls table<br/>PostgreSQL table")]
  node_transaction{{"Write transaction<br/>database transaction"}}
  node_clicks["Async click increment<br/>persistence update"]
end

node_browser(("Browser navigation<br/>client"))

node_ui -->|"drives"| node_spa
node_spa -->|"POST /api/v1/urls/shorten"| node_server
node_server -->|"mounts API"| node_routes
node_routes -->|"dispatches"| node_controller
node_controller -->|"validated longUrl"| node_service
node_browser -->|"GET /:shortCode"| node_server
node_server -->|"redirect handling"| node_controller
node_service -->|"uses pool"| node_dbconfig
node_dbconfig -->|"connects"| node_urls
node_service -->|"deduplicates by long_url"| node_urls
node_service -->|"creates URL"| node_transaction
node_transaction -->|"insert and update code"| node_urls
node_transaction -->|"encodes generated ID"| node_base62
node_model -.->|"maps record"| node_urls
node_service -->|"indexed short_code lookup"| node_urls
node_service -.->|"after resolution"| node_clicks
node_clicks -.->|"atomic increment"| node_urls
node_controller -->|"destination or API response"| node_server
node_server -->|"302 redirect"| node_browser
node_controller -->|"short link response"| node_spa

click node_ui "https://github.com/arkadipsom-code/crispurl-shortener/blob/main/frontend/index.html"
click node_spa "https://github.com/arkadipsom-code/crispurl-shortener/blob/main/frontend/app.js"
click node_server "https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/server.js"
click node_routes "https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/routes/url.route.js"
click node_controller "https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/controllers/url.controller.js"
click node_service "https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/services/url.service.js"
click node_base62 "https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/utils/base62.js"
click node_dbconfig "https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/config/db.js"
click node_model "https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/models/url.model.js"

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_ui,node_spa toneBlue
class node_server,node_routes,node_controller,node_service,node_base62 toneAmber
class node_dbconfig,node_model,node_urls,node_transaction,node_clicks toneMint
class node_browser toneNeutral
```

## Components

### Browser SPA

Independently deployed on Vercel.

- [Shortening UI](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/frontend/index.html): HTML UI · `frontend/index.html`
- [Submission client](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/frontend/app.js): vanilla JS · Posts URLs and renders returned short links. · `frontend/app.js`

### Express service

REST API and public redirect gateway deployed on Render.

- [HTTP gateway](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/server.js): Express server · Configures middleware, CORS, API mounting, and dynamic redirects. · `backend/server.js`
- [URL routes](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/routes/url.route.js): Express routes · Defines shortening API endpoints. · `backend/routes/url.route.js`
- [Request controller](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/controllers/url.controller.js): HTTP policy · Validates input and selects HTTP success or error responses. · `backend/controllers/url.controller.js`
- [URL service](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/services/url.service.js): business logic · Deduplicates writes and resolves redirect destinations. · `backend/services/url.service.js`
- [Base62 encoder](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/utils/base62.js): code generator · Encodes database IDs into deterministic short codes. · `backend/utils/base62.js`

### PostgreSQL persistence

Neon-backed URL storage and transaction boundary.

- [DB pool](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/config/db.js): PostgreSQL client · Pooled Neon connection configuration. · `backend/config/db.js`
- [URL record model](https://github.com/arkadipsom-code/crispurl-shortener/blob/main/backend/models/url.model.js): data model · Represents long URL, code, clicks, and creation time. · `backend/models/url.model.js`
- **urls table**: PostgreSQL table · Unique indexed short codes support redirect lookups.
- **Write transaction**: database transaction · Insert for ID, encode it, update short code, then commit.
- **Async click increment**: persistence update · Runs without delaying the redirect response.

### Other components

- **Browser navigation**: client · Navigates directly to public short-code paths.

## Connections

- Shortening UI → Submission client: drives
- Submission client → HTTP gateway: POST /api/v1/urls/shorten
- HTTP gateway → URL routes: mounts API
- URL routes → Request controller: dispatches
- Request controller → URL service: validated longUrl
- Browser navigation → HTTP gateway: GET /:shortCode
- HTTP gateway → Request controller: redirect handling
- URL service → DB pool: uses pool
- DB pool → urls table: connects
- URL service → urls table: deduplicates by long_url
- URL service → Write transaction: creates URL
- Write transaction → urls table: insert and update code
- Write transaction → Base62 encoder: encodes generated ID
- URL record model → urls table: maps record
- URL service → urls table: indexed short_code lookup
- URL service → Async click increment: after resolution
- Async click increment → urls table: atomic increment
- Request controller → HTTP gateway: destination or API response
- HTTP gateway → Browser navigation: 302 redirect
- Request controller → Submission client: short link response
