Skip to content

gregsyu/url-shortener

Repository files navigation

Nest Logo

URL Shortener API

A URL shortening service built with NestJS, TypeScript, Nginx, Redis and PostgreSQL.

Nginx TypeScript NestJS Prisma PostgreSQL Redis


Rate Limiting

This API implements rate limiting to prevent abuse and ensure fair usage:

  • Global limit: 100 requests per minute across all endpoints
  • Authentication endpoints:
    • Sign up: 3 requests per minute
    • Sign in: 5 requests per minute
  • URL management endpoints:
    • Create short URL: 20 requests per minute
    • Delete URL: 10 requests per minute
  • Redirect endpoint: Exempt from rate limiting to ensure smooth redirection

Rate limits are implemented using NestJS Throttler module with Redis storage for different configurations for various route groups.


Tech Stack

  • Nginx
  • NestJS
  • TypeScript
  • PostgreSQL
  • Prisma ORM
  • Redis

Run with Docker

From a fresh clone:

git clone https://github.com/gregsyu/url-shortener.git
cd url-shortener
cp .env.example .env

Set JWT_SECRET in .env:

JWT_SECRET="your-secret-key"
REDIS_URL="redis://redis:6379"
POSTGRES_USER="postgres"
POSTGRES_PASSWORD="postgres"
POSTGRES_DB="url_shortener"

The Docker Compose file provides the container database URL automatically:

DATABASE_URL="postgresql://postgres:postgres@db:5432/url_shortener"
docker compose build                                   # build the app image
docker compose up -d db redis                          # start the databases
docker compose run --rm app pnpm prisma migrate deploy # run database migrations
docker compose up -d app nginx                         # start the application:

For production, keep migrations separate from application startup this avoids running schema changes on every app restart and makes migration failures explicit during deployment.

Tip

You can also run Prisma Studio to visually manage your database:

docker compose up -d prisma-studio

Then visit http://localhost:51212 to access Prisma Studio interface.

Note

The API will be available at http://localhost:8080.


To stop the containers:

docker compose down -v

Project Setup

Install dependencies

pnpm install

Environment variables

Create a .env file:

DATABASE_URL="postgresql://postgres:password@localhost:5432/url_shortener"
JWT_SECRET="your-secret-key"
REDIS_URL="redis://localhost:6379"

Database

Generate Prisma Client:

pnpm prisma generate

Apply the schema:

pnpm prisma db push

Running the Application

Development

pnpm run start:dev

Production

pnpm run build
pnpm run start:prod

API Endpoints

Authentication

Method Route Description
POST /auth/signup Create account
POST /auth/signin Login and receive JWT
GET /auth/me Check authentication

URLs

Method Route Description
POST /url Create short URL
GET /:code Redirect to original URL
GET /url/:code Get URL statistics
DELETE /url/:code Delete URL

Example Response

Creating a short URL:

{
  "id": "cmabc123xyz",
  "code": "xY7kLm2",
  "original": "https://google.com",
  "clicks": 0,
  "shortUrl": "http://localhost:8080/xY7kLm2"
}

About

A url shortener API made with nestjs

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages