8000 GitHub - hassangomaa/url-shortener: A fully-featured URL shortening API built with NestJS, PostgreSQL, and JWT authentication.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

hassangomaa/url-shortener

Repository files navigation

Nest Logo

πŸš€ URL Shortener API - NestJS

A fully-featured URL shortening API built with NestJS, PostgreSQL, and JWT authentication.

GitHub repo size GitHub stars GitHub license

πŸ“Œ Project Overview

The URL Shortener API is a full-fledged backend system designed to shorten URLs, track visits, and retrieve analytics. The system ensures scalability, security, and performance while strictly following DRY, SOLID, and clean architecture principles.

This project is fully Dockerized and uses PostgreSQL as the database, with raw SQL queries for optimized performance.

βœ… Developed by HASSAN GOMAA from scratch.


πŸš€ Features

  • Full RESTful APIs with raw SQL queries for maximum efficiency.
  • JWT Authentication (Signup & Signin).
  • Shorten URLs with user authentication.
  • Redirect via Short URL.
  • Track Visits (IP, User-Agent, Timestamp).
  • Retrieve Statistics (Visit count for each shortened URL).
  • Database Migrations & Seeding.
  • Optimized DB indexes & queries.
  • Swagger API Documentation for testing.
  • Docker & Docker Compose for easy deployment.

πŸ› οΈ Tech Stack

Tech Description
NestJS TypeScript-based backend framework
PostgreSQL Database for storing URLs and tracking visits
TypeScript Static typing for maintainability
JWT (JSON Web Tokens) Secure authentication
Docker & Docker Compose Containerized development & deployment
DB RAW QUERY Database migrations (raw SQL queries used for performance)
Bcrypt Secure password hashing
Swagger API Documentation
Postman API testing & debugging

πŸ“ Project Structure


src/
│── auth/ # Authentication module
β”‚ β”œβ”€β”€ controllers/ # Auth Controllers
β”‚ β”œβ”€β”€ services/ # Auth Services
β”‚ β”œβ”€β”€ dto/ # Data Transfer Objects (Request/Response)
│── urls/ # URL Shortening module
β”‚ β”œβ”€β”€ controllers/ # URL Controllers
β”‚ β”œβ”€β”€ services/ # URL Services
β”‚ β”œβ”€β”€ dto/ # Data Transfer Objects (Request/Response)
│── database/ # Database Service & Migrations
│── common/ # Shared utilities & helpers
│── config/ # App configuration
│── main.ts # App entry point
│── app.module.ts # Main module


πŸ”§ Setup & Installation

Prerequisites

  • Node.js (>=16.x)
  • Docker & Docker Compose
  • PostgreSQL (if not using Docker)

πŸš€ Quick Start (Docker)

git clone https://github.com/hassangomaa/url-shortener.git
cd url-shortener

# Start services
docker-compose up --build -d

# Run database migrations
docker exec -it url_shortener_app npx ts-node ./src/database/migrate.ts

πŸ› οΈ Local Setup (Without Docker)

# Install dependencies
npm install

# Setup environment variables
cp .env.example .env

# Start the app
npm run start:dev

πŸ”‘ Authentication (JWT)

  • Signup (POST /auth/sign-up)
  • Signin (POST /auth/sign-in)
  • JWT-based authentication is required for protected routes.

🌐 API Endpoints

Authentication

Method Endpoint Description
POST /auth/sign-up Register new user
POST /auth/sign-in Authenticate user and receive tokens

URL Shortening

Method Endpoint Description
POST /shorten Shortens a long URL (Auth required)
GET /:shortUrl Redirects to original URL
GET /stats/:shortUrl Fetches visit statistics for a short URL (Auth required)

πŸ“ˆ How Visit Tracking Works?

  1. A user shortens a URL using POST /shorten.
  2. A unique short code is generated and stored in the database.
  3. When a user visits the short URL (GET /:shortUrl):
    • The original URL is retrieved from the database.
    • A visit record is logged (including IP, timestamp, and user-agent).
  4. The owner can fetch visit statistics using GET /stats/:shortUrl.

πŸ—οΈ Database Migrations & Seeding

  • Run Migrations

    npx ts-node ./src/database/migrate.ts
  • Database Tables:

    • users: Stores registered users.
    • urls: Stores original & shortened URLs.
    • visits: Logs visits for analytics.

🐳 Docker Configuration

The project is fully Dockerized with the following services:

  • App Container (Runs NestJS)
  • PostgreSQL Database (Stores URLs & visits)

Start the Application

docker-compose up --build -d

docker-compose.yml


πŸ“ˆ Real-Life Example Usage

1️⃣ User Signs Up

Request:

{
  "email": "user@example.com",
  "password": "securePassword123"
}

Response:

{
  "accessToken": "jwt_token",
  "refreshToken": "jwt_refresh_token",
  "user": { "id": 1, "email": "user@example.com" }
}

2️⃣ User Shortens a URL

Request:

{
  "originalUrl": "https://www.example.com/long-url-path"
}

Response:

{
  "shortUrl": "abc123",
  "originalUrl": "https://www.example.com/long-url-path",
  "createdAt": "2025-02-12T10:00:00Z"
}

3️⃣ User Visits the Short URL

  • GET /abc123 β†’ Redirects to https://www.example.com/long-url-path
  • A visit record is logged.

4️⃣ User Checks URL Stats

Request:

{
  "shortUrl": "abc123"
}

Response:

{
  "shortUrl": "abc123",
  "visitCount": 10
}

πŸ“„ Conclusion

This project demonstrates a scalable, maintainable, and efficient URL Shortener using NestJS, PostgreSQL, Docker, and JWT Authentication. The clean architecture, raw SQL queries, and best security practices ensure performance and reliability.

πŸ”₯ Developed by HASSAN GOMAA. πŸš€

About

A fully-featured URL shortening API built with NestJS, PostgreSQL, and JWT authentication.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0