Skip to content

DIYA73/api-rate-guardian

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

30 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🚦 API Rate Guardian

Production-ready rate limiting and abuse detection microservice built with Node.js, TypeScript, Redis, and Docker. Protect your APIs from abuse with distributed rate limiting, IP banning, and real-time monitoring.

TypeScript Node.js Redis Docker Live License


🌐 Live Demo

Backend API: https://api-rate-guardian-1.onrender.com

Admin UI: Run locally or deploy to Vercel (see instructions below)


πŸ“– Overview

API Rate Guardian is a specialized microservice designed to protect your APIs from abuse, DDoS attacks, and excessive traffic. Built with enterprise-grade patterns, it provides distributed rate limiting using Redis, making it perfect for multi-instance deployments.

🎯 Key Features

  • πŸ” JWT Authentication: Secure admin access with token-based auth
  • 🚦 Redis-Backed Rate Limiting: Distributed, accurate rate limiting across all instances
  • πŸ›‘ IP Ban System: Block and unblock abusive IPs with API or UI
  • πŸ“Š Real-Time Stats: Monitor requests, rate limits, and banned IPs
  • 🧭 Admin Dashboard: Web-based control panel for system management
  • ⚑ High Performance: Handles 1000+ req/s with minimal latency
  • 🐳 Docker Ready: Complete containerization with docker-compose
  • πŸ§ͺ Production Tested: Live deployment on Render with Redis Cloud

πŸ› οΈ Tech Stack

Backend

πŸš€  Node.js 18+ (Runtime)
πŸ“˜  TypeScript 5+ (Type Safety)
🌐  Express.js (Web Framework)
πŸ”΄  Redis 7+ (Rate Limiting Store)
πŸ“¦  ioredis (Redis Client)
πŸ”  jsonwebtoken (JWT Auth)
πŸ›‘οΈ  helmet (Security Headers)
πŸ”„  express-rate-limit (Base Rate Limiting)

Frontend (Admin UI)

βš›οΈ  React 18 (UI Library)
🎨  Tailwind CSS (Styling)
πŸ“Š  Chart.js (Data Visualization)
πŸ”„  Axios (API Client)

DevOps

🐳  Docker + Docker Compose
☁️  Render (Backend Hosting)
πŸ”΄  Redis Cloud (Managed Redis)
πŸ“Š  Winston (Logging)

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client    β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Express API       β”‚
β”‚  (Rate Guardian)    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ JWT Auth Middleware β”‚
β”‚ Rate Limiter        β”‚
β”‚ Admin Routes        β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Redis     β”‚
β”‚  (ioredis)  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ Counters  β”‚
β”‚ β€’ Bans      β”‚
β”‚ β€’ Stats     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Design Decisions:

  • Redis as Single Source of Truth: Ensures accurate rate limiting across multiple API instances
  • JWT for Admin Auth: Stateless authentication for scalability
  • Sliding Window Algorithm: More accurate than fixed window, prevents burst attacks
  • IP-based Tracking: Simple and effective for most use cases

✨ Features Breakdown

1. Rate Limiting

Default Limits:

  • 100 requests per 15 minutes per IP
  • Customizable per endpoint
  • Sliding window algorithm
  • Burst protection

Example:

// Apply rate limiting to specific routes
app.use('/api/v1/public', rateLimiter({
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100, // limit each IP to 100 requests per windowMs
  message: 'Too many requests, please try again later'
}));

2. IP Ban System

Features:

  • Ban IPs permanently or temporarily
  • Unban with single API call
  • Automatic ban expiry
  • Ban list export

API Endpoints:

# Ban an IP
POST /api/v1/admin/ban
Body: { "ip": "192.168.1.100", "reason": "Abuse detected" }

# Unban an IP
DELETE /api/v1/admin/ban/192.168.1.100

# List all banned IPs
GET /api/v1/admin/bans

3. Real-Time Monitoring

Metrics Tracked:

  • Total requests (last 24h, 7d, 30d)
  • Rate limit violations
  • Banned IPs count
  • Top abusive IPs
  • Request distribution by endpoint
  • Redis memory usage
  • API response times

Dashboard Features:

  • Live request counter
  • Rate limit hit ratio
  • Geographic IP distribution (optional)
  • Historical trends (charts)

4. Admin Dashboard UI

Capabilities:

  • View system stats
  • Monitor rate limit violations
  • Ban/unban IPs manually
  • Search request logs
  • Configure rate limits (coming soon)
  • Export data to CSV

πŸš€ Quick Start

Prerequisites

  • Node.js 18+
  • Redis 7+ (local or Redis Cloud)
  • Docker (optional)
  • npm or yarn

Option 1: Local Development

1. Clone Repository

git clone https://github.com/DIYA73/api-rate-guardian.git
cd api-rate-guardian

2. Install Dependencies

npm install

3. Environment Setup

Create .env file:

# Server
NODE_ENV=development
PORT=5000

# Redis
REDIS_URL=redis://localhost:6379
# Or Redis Cloud:
# REDIS_URL=redis://default:password@your-redis-cloud.com:12345

# JWT
JWT_SECRET=your-super-secret-jwt-key-min-32-characters
JWT_EXPIRES_IN=24h

# Admin Credentials (for initial setup)
ADMIN_EMAIL=admin@example.com
ADMIN_PASSWORD=changeme123

# Rate Limiting
DEFAULT_RATE_LIMIT_WINDOW_MS=900000
DEFAULT_RATE_LIMIT_MAX=100

# CORS (comma-separated)
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:5173

4. Start Redis

# Using Docker
docker run -d -p 6379:6379 redis:7-alpine

# Or use local Redis installation
redis-server

5. Run Backend

# Development mode with hot reload
npm run dev

# Production mode
npm run build
npm start

Backend runs on http://localhost:5000

6. Run Admin UI (Optional)

cd admin-ui
npm install
npm run dev

Admin UI runs on http://localhost:3000

Option 2: Docker Compose

All-in-one command:

docker-compose up -d

This starts:

  • API server on port 5000
  • Redis on port 6379
  • Admin UI on port 3000

πŸ“ Project Structure

api-rate-guardian/
β”œβ”€β”€ src/                        # Backend source code
β”‚   β”œβ”€β”€ controllers/            # Request handlers
β”‚   β”‚   β”œβ”€β”€ auth.controller.ts
β”‚   β”‚   β”œβ”€β”€ admin.controller.ts
β”‚   β”‚   └── stats.controller.ts
β”‚   β”œβ”€β”€ middleware/             # Express middleware
β”‚   β”‚   β”œβ”€β”€ auth.middleware.ts
β”‚   β”‚   β”œβ”€β”€ rateLimiter.middleware.ts
β”‚   β”‚   └── ipBan.middleware.ts
β”‚   β”œβ”€β”€ routes/                 # API routes
β”‚   β”‚   β”œβ”€β”€ auth.routes.ts
β”‚   β”‚   β”œβ”€β”€ admin.routes.ts
β”‚   β”‚   └── public.routes.ts
β”‚   β”œβ”€β”€ services/               # Business logic
β”‚   β”‚   β”œβ”€β”€ redis.service.ts
β”‚   β”‚   β”œβ”€β”€ rateLimiting.service.ts
β”‚   β”‚   └── stats.service.ts
β”‚   β”œβ”€β”€ config/                 # Configuration
β”‚   β”‚   β”œβ”€β”€ redis.config.ts
β”‚   β”‚   └── jwt.config.ts
β”‚   β”œβ”€β”€ types/                  # TypeScript types
β”‚   β”‚   └── index.d.ts
β”‚   β”œβ”€β”€ utils/                  # Utilities
β”‚   β”‚   β”œβ”€β”€ logger.ts
β”‚   β”‚   └── errors.ts
β”‚   β”œβ”€β”€ app.ts                  # Express app
β”‚   └── server.ts               # Entry point
β”‚
β”œβ”€β”€ admin-ui/                   # React admin dashboard
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   └── App.tsx
β”‚   β”œβ”€β”€ public/
β”‚   β”‚   └── screenshots/        # UI screenshots
β”‚   └── package.json
β”‚
β”œβ”€β”€ tests/                      # Test files
β”‚   β”œβ”€β”€ unit/
β”‚   β”œβ”€β”€ integration/
β”‚   └── e2e/
β”‚
β”œβ”€β”€ docs/                       # Documentation
β”‚   β”œβ”€β”€ API.md                  # API documentation
β”‚   β”œβ”€β”€ DEPLOYMENT.md           # Deployment guide
β”‚   └── ARCHITECTURE.md         # Architecture details
β”‚
β”œβ”€β”€ screenshots/                # Project screenshots
β”œβ”€β”€ .env.example                # Environment template
β”œβ”€β”€ docker-compose.yml          # Docker Compose config
β”œβ”€β”€ Dockerfile                  # Backend Dockerfile
β”œβ”€β”€ tsconfig.json               # TypeScript config
└── README.md                   # This file

πŸ”Œ API Endpoints

Public Endpoints

Health Check

GET /api/v1/health

Response 200:
{
  "status": "ok",
  "redis": "connected",
  "uptime": 3600
}

Test Rate Limiting

GET /api/v1/public/test

Response 200:
{
  "message": "Request successful",
  "requestsRemaining": 95
}

Response 429 (Rate Limited):
{
  "error": "Too many requests, please try again later",
  "retryAfter": 300
}

Authentication

Admin Login

POST /api/v1/auth/login

Body:
{
  "email": "admin@example.com",
  "password": "your-password"
}

Response 200:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "expiresIn": "24h"
}

Admin Endpoints (JWT Required)

Get System Stats

GET /api/v1/admin/stats
Authorization: Bearer <token>

Response 200:
{
  "totalRequests24h": 15234,
  "totalRequests7d": 98765,
  "rateLimitViolations": 234,
  "bannedIPs": 12,
  "topAbusiveIPs": [
    { "ip": "1.2.3.4", "requests": 5000 }
  ],
  "redisMemoryUsage": "15.2 MB"
}

Ban IP Address

POST /api/v1/admin/ban
Authorization: Bearer <token>

Body:
{
  "ip": "192.168.1.100",
  "reason": "Excessive requests",
  "duration": 3600  // seconds (optional, omit for permanent)
}

Response 201:
{
  "message": "IP banned successfully",
  "ip": "192.168.1.100",
  "expiresAt": "2024-01-15T12:00:00Z"
}

Unban IP Address

DELETE /api/v1/admin/ban/:ip
Authorization: Bearer <token>

Response 200:
{
  "message": "IP unbanned successfully",
  "ip": "192.168.1.100"
}

List Banned IPs

GET /api/v1/admin/bans
Authorization: Bearer <token>

Response 200:
{
  "bans": [
    {
      "ip": "192.168.1.100",
      "reason": "Excessive requests",
      "bannedAt": "2024-01-15T10:00:00Z",
      "expiresAt": "2024-01-15T12:00:00Z"
    }
  ]
}

Get Redis Stats

GET /api/v1/admin/redis-stats
Authorization: Bearer <token>

Response 200:
{
  "connected": true,
  "memoryUsage": "15.2 MB",
  "totalKeys": 1543,
  "uptime": 86400,
  "version": "7.0.5"
}

πŸ“Έ Screenshots

Admin Login

Admin Login Secure JWT-based authentication

Dashboard Overview

Dashboard Real-time stats and monitoring

Redis Statistics

Redis Stats Redis health and memory usage

IP Ban Management

IP Bans Ban and unban abusive IPs


πŸ§ͺ Testing

Run Tests

# Unit tests
npm test

# Integration tests
npm run test:integration

# E2E tests
npm run test:e2e

# Coverage
npm run test:coverage

Test Rate Limiting

# Send 150 requests to trigger rate limit
for i in {1..150}; do
  curl http://localhost:5000/api/v1/public/test
done

After 100 requests, you'll receive:

{
  "error": "Too many requests, please try again later",
  "retryAfter": 300
}

🐳 Docker Deployment

Build and Run

Backend Only:

docker build -t api-rate-guardian .
docker run -p 5000:5000 --env-file .env api-rate-guardian

Full Stack (Backend + Redis + Admin UI):

docker-compose up -d

Production Deployment:

docker-compose -f docker-compose.prod.yml up -d

πŸš€ Deployment

Deploy to Render

Backend:

  1. Create new Web Service
  2. Connect GitHub repository
  3. Configure:
    • Build Command: npm install && npm run build
    • Start Command: npm start
  4. Add environment variables
  5. Deploy

Redis:

  • Use Redis Cloud (free tier available)
  • Or add Redis addon on Render

Admin UI:

  • Deploy to Vercel
  • Set REACT_APP_API_URL to your Render backend URL

Environment Variables for Production

NODE_ENV=production
PORT=5000
REDIS_URL=redis://your-redis-cloud-url:port
JWT_SECRET=production-secret-min-32-characters
ALLOWED_ORIGINS=https://your-admin-ui.vercel.app

πŸ“Š Performance

Benchmarks:

  • Throughput: 1,000+ requests/second
  • Latency: <50ms average (p99: <100ms)
  • Memory: ~100MB base (scales with Redis data)
  • CPU: <10% on moderate load

Load Testing:

# Using Apache Bench
ab -n 10000 -c 100 http://localhost:5000/api/v1/public/test

# Using autocannon
npx autocannon -c 100 -d 60 http://localhost:5000/api/v1/public/test

πŸ”’ Security

Implemented Security Measures:

  • βœ… JWT authentication for admin routes
  • βœ… Helmet.js security headers
  • βœ… CORS configuration
  • βœ… Input validation
  • βœ… Rate limiting (of course!)
  • βœ… IP-based access control
  • βœ… Environment variable secrets
  • βœ… Redis password authentication
  • βœ… HTTPS enforcement (production)

Security Headers:

{
  "X-Content-Type-Options": "nosniff",
  "X-Frame-Options": "DENY",
  "X-XSS-Protection": "1; mode=block",
  "Strict-Transport-Security": "max-age=31536000"
}

πŸ—ΊοΈ Roadmap

βœ… Phase 1: Core (Completed)

  • Redis-backed rate limiting
  • IP ban system
  • JWT authentication
  • Admin API
  • Docker deployment
  • Live production deployment

🚧 Phase 2: Enhanced Features (In Progress)

  • Admin UI enhancements
  • Custom rate limit rules per endpoint
  • Geographic IP tracking
  • Email notifications for abuse
  • API key-based rate limiting (not just IP)
  • Advanced analytics dashboard

πŸ“‹ Phase 3: Enterprise Features (Planned)

  • Multi-tenancy support
  • Custom rule engine (e.g., "ban if 500+ req in 1 min")
  • Webhook integrations
  • Machine learning-based anomaly detection
  • DDoS protection layer
  • GraphQL endpoint
  • Prometheus metrics export
  • Kubernetes deployment manifests

🀝 Integration Examples

Use as Middleware in Your App

import axios from 'axios';

// Check if IP is banned before processing request
async function checkRateLimit(ip: string): Promise<boolean> {
  try {
    const response = await axios.get(
      `https://api-rate-guardian-1.onrender.com/api/v1/check/${ip}`
    );
    return response.data.allowed;
  } catch (error) {
    // Fail open - allow request if rate guardian is down
    return true;
  }
}

// In your Express app
app.use(async (req, res, next) => {
  const clientIP = req.ip;
  const allowed = await checkRateLimit(clientIP);
  
  if (!allowed) {
    return res.status(429).json({ error: 'Rate limit exceeded' });
  }
  
  next();
});

🀝 Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Write tests for new features
  4. Follow TypeScript best practices
  5. Update documentation
  6. Submit a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ‘¨β€πŸ’» Author

DIYA73


πŸ™ Acknowledgments

  • Redis team for the amazing in-memory database
  • Express.js community
  • Open-source contributors

⭐ If API Rate Guardian helps protect your APIs, please star the repository!

🚦 Protecting APIs, one request at a time.


Made with ❀️ for developers who care about API security

About

🚦 Production-ready rate limiting microservice with Redis, IP banning, real-time monitoring & admin dashboard. Protect APIs from abuse. Node.js + TypeScript + Docker. LIVE on Render

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors