Skip to content

droiddevgeeks/TinyUrl

Repository files navigation

TinyURL API Ask DeepWiki

A URL shortening service built with NestJS. This project allows users to shorten long URLs and retrieve the original URLs using the shortened ones.

Features

  • Shorten long URLs into compact, shareable links.
  • Retrieve the original URL using the shortened link.
  • Centralized logging for all API requests and responses.
  • Environment-based configuration using .env files.

Prerequisites

Installation

  1. Clone the repository:

    git clone https://github.com/droiddevgeeks/TinyUrl.git
    cd TinyUrl
    
  2. Install dependencies:

    npm install
    
  3. Create a .env file in the root directory and configure the following:

    PORT=3000
    

Running the Application

  1. Start the development server:

    npm run start:dev
    
  2. The application will be available at http://localhost:3000.

API Endpoints

  1. Shorten a URL POST /url/shorten

    Example Request Payload

    {
      "originalUrl": "https://example.com"
    }
    

    Example Response Payload

    {
      "shortUrl": "http://localhost:3000/abc123"
    }
    
  2. Retrieve the Original URL GET /url/:shortUrl

    Example Response Payload

    {
      "originalUrl": "https://example.com"
    }
    

Project Structure

src/
├── cache/
│   └──cache.module.ts       # Redis cache module setup ([src/cache/cache.module.ts](src/cache/cache.module.ts))
│   └──cache.provider.ts     # Redis client provider ([src/cache/cache.provider.ts](src/cache/cache.provider.ts))
│   └──cache.service.ts      # Cache service abstraction ([src/cache/cache.service.ts](src/cache/cache.service.ts))
├── app.module.ts            # Main application module
├── app.controller.ts        # Root controller
├── app.service.ts           # Root service
├── middleware/
│   └── logger.middleware.ts # Centralized logging middleware
|   └── throttler.exception.filter.ts # Handles rate-limiting exceptions ([src/middleware/throttler.exception.filter.ts](src/middleware/throttler.exception.filter.ts))
│   └── throttler.gaurd.ts           # Custom throttler guard with logging ([src/middleware/throttler.gaurd.ts](src/middleware/throttler.gaurd.ts))
├── url/
│   ├── tinyurl.controller.ts # URL controller
│   ├── tinyurl.service.ts    # URL service
│   ├── dto/
│   │   └── create-url.dto.ts # Data Transfer Object for URL creation

Centralized Logging

All incoming requests and outgoing responses are logged using a custom middleware (LoggerMiddleware). Logs include:

Caching Layer

This project uses Redis for caching URL mappings to improve performance and reduce database load. The cache is managed via:

  • CacheModule: Registers the cache service and provider.
  • CacheProvider: Configures and provides a Redis client.
  • CacheService: Exposes methods to set/get cache entries and manages Redis connection lifecycle.

Make sure to configure your .env file with the correct Redis connection details.

Rate Limiting

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published