This directory contains the core infrastructure and application code for the NanoLink backend.
The backend is split into two independent services that communicate via Apache Kafka:
- API Server (
index.ts&controllers/): A Fastify server that handles incoming HTTP requests. It resolves short URLs from Redis (cache-aside) and publishes click events to Kafka without blocking the HTTP response. - Analytics Worker (
analytics/analyticsWorker.ts): A headless Kafka consumer that runs in the background, batches incoming click events, and performs bulk inserts into PostgreSQL.
Checks if the server is up and running.
- URL:
/ - Method:
GET - Response:
{"Status": "OK"}
Accepts a long URL, validates it using Zod, and generates a short link using Base62.
- URL:
/url - Method:
POST - Body:
{"url": "https://www.google.com"} - Response:
{"Shortened URL": "http://localhost:3000/a1B2c3D"}
Redirects the user to the original website. Behind the scenes: Hits Redis first. If Cache Miss, falls back to Postgres. Publishes a Fire-And-Forget event to Kafka.
- URL:
/:shortId - Method:
GET - Behavior: HTTP 302 Redirect to the original
long_url. Returns 404 if not found.
Returns all saved URLs in the database with pagination.
- URL:
/all - Method:
GET - Response:
{"RESULT": [{"id": 1, "short_code": "a1B2c3D", "long_url": "https://... "}]}
Returns an aggregated list of all URLs joined with their total click counts.
- URL:
/analytics - Method:
GET - Response:
{"results": [{"short_code": "a1B2c3D", "total_clicks": 42}]}
The backend is fully containerized. The compose.yaml file defines 5 distinct services:
url_shortner: The main Fastify API.analytics_worker: The headless background worker.postgres_db: The relational database.redis_cache: The in-memory cache for ultra-fast reads.kafka_kraft: The message broker running in KRaft mode (no Zookeeper).