Skip to content

dk-a-dev/dev.ly-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dev.ly — Backend

The core URL shortener API for dev.ly. Handles user authentication, URL creation/management, short-code redirects, and pushes click events to the analytics pipeline via BullMQ. Built with Express 5 and PostgreSQL.


Features

  • JWT Authentication — Secure register/login with bcrypt password hashing
  • URL Shortening — Base62-encoded short codes with custom expiry
  • Redirect EngineGET /:shortCode → 302 redirect with async click logging
  • Redis URL Cache — Sub-millisecond redirects via cached URL lookups
  • BullMQ Click Queue — Fire-and-forget click events with guaranteed delivery to the analytics worker
  • Rate Limiting — Redis-backed rate limiting on auth & API endpoints (redirects are never blocked)
  • Database Migrations — Auto-run SQL migrations on startup

Tech Stack

Technology Purpose
Express 5 HTTP framework
PostgreSQL 15 Primary database
Redis 7 URL caching & rate limiting
BullMQ Job queue for click events
JSON Web Tokens Auth tokens
bcrypt Password hashing
Axios Internal HTTP calls

Project Structure

backend/
├── index.js                         # Entry point
├── Dockerfile                       # Docker build
├── package.json
└── src/
    ├── config/
    │   ├── db.js                    # PostgreSQL pool
    │   └── redis.js                 # Redis/IORedis client
    ├── controllers/
    │   ├── authController.js        # Register, Login, Profile
    │   └── urlController.js         # Create, List, Delete, Redirect
    ├── db/
    │   ├── migrate.js               # Auto-migration runner
    │   └── migrations/
    │       ├── 001_initial_schema.sql
    │       ├── 002_increase_short_code_length.sql
    │       ├── 003_add_analytics_columns.sql
    │       └── 004_add_utm_and_unique.sql
    ├── middlewares/
    │   ├── authMiddleware.js         # JWT verification
    │   └── rateLimiter.js            # Redis-backed rate limiter
    ├── queues/
    │   └── clickQueue.js             # BullMQ producer
    ├── routes/
    │   ├── authRoutes.js             # /api/auth/*
    │   └── urlRoutes.js              # /api/url/*
    └── utils/
        └── base62.js                 # Short code encoding

API Endpoints

Method Endpoint Auth Description
POST /api/auth/register Register a new user
POST /api/auth/login Login, returns JWT
GET /api/auth/profile Get current user profile
POST /api/url Create a short URL
GET /api/url List user's URLs
DELETE /api/url/:id Delete a URL
GET /:shortCode Redirect → original URL

Rate Limits

Endpoint Limit Behavior
POST /api/auth/* 10 req/min Returns 429
POST /api/url 30 req/min Returns 429
GET /:shortCode No limit Always redirects

Getting Started

Prerequisites

  • Node.js 18+
  • PostgreSQL 15+
  • Redis 7+

Install & Run

npm install
node index.js

The server starts on port 5001 (default).

Database Migrations

Migrations run automatically on startup. To run manually:

npm run migrate

Environment Variables

Create a .env file:

PORT=5001
DB_USER=postgres
DB_PASSWORD=postgres
DB_HOST=localhost
DB_NAME=devly
DB_PORT=5432
JWT_SECRET=supersecretkey_change_me_in_prod
ANALYTICS_URL=http://localhost:5002
REDIS_URL=redis://localhost:6379

Docker: Use DB_HOST=db, ANALYTICS_URL=http://analytics:5002, REDIS_URL=redis://redis:6379


🔗 Related Repos

Repo Description
dev.ly Main repo (system design & orchestration)
dev.ly-frontend Next.js dashboard
dev.ly-analytics Analytics microservice

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors