Skip to content

Arya125-droid/api-gateway-analytics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Gateway Analytics Console

A full-stack, closed-loop telemetry and proxy system. This project acts as an API Gateway that intercepts incoming network traffic, mathematically rate-limits requests using a custom Token Bucket algorithm, proxies the traffic to a target server, and visualizes the network health in a real-time React dashboard.

Core Features

  • Custom Rate Limiting Engine: A mathematical implementation of the Token Bucket algorithm built from scratch in Express. Handles burst capacity and recovery rates without relying on third-party limiting libraries.
  • Asynchronous Telemetry Logging: Middleware that extracts request metadata (IP, latency, status codes) and writes to MongoDB asynchronously, ensuring zero blocking of the main thread and ultra-low overhead.
  • Live Analytics Dashboard: A React frontend utilizing Vite and Recharts to poll the MongoDB aggregation pipeline, visualizing traffic volume and system latency spikes in real-time.
  • Closed-Loop UI Testing: Built-in dashboard controls to actively fire network requests at the protected proxy and immediately visualize the rate-limiter's defensive responses (HTTP 429s).
  • Data Lifecycle Management: TTL (Time-To-Live) indexing in MongoDB automatically expires old logs after 1 day, keeping database costs and query times down.

Architecture

  1. The Shield (Proxy & Limiter): External requests hit /api/*. The gateway checks the Token Bucket. If permitted, the request is proxied to the target. If empty, the connection is instantly severed (429).
  2. The Surveillance (Logger): The exact millisecond latency and status code of the interaction is recorded and pushed to MongoDB Atlas.
  3. The Monitor (Dashboard): React fetches aggregated minute-by-minute metrics from the unprotected /admin/* routes, mapping the data cleanly onto SVG charts.

Tech Stack

  • Frontend: React (Vite), Recharts, Axios
  • Backend: Node.js, Express, HTTP-Proxy-Middleware, CORS
  • Database: MongoDB Atlas, Mongoose (with advanced Aggregation Pipelines)

Getting Started

Prerequisites

  • Node.js (v18+)
  • A MongoDB Atlas cluster (or local instance)

1. Clone the Repository

git clone https://github.com/Arya125-droid/api-gateway-analytics.git
cd api-gateway-analytics

2. Environment Setup

Create a .env file in the root directory and add your MongoDB connection string:

MONGO_URI=mongodb+srv://<username>:<password>@cluster.mongodb.net/gateway
PORT=5001

3. Install Dependencies

You need to install packages for both the backend and frontend.

# Install backend dependencies
npm install

# Install frontend dependencies
cd frontend
npm install
cd ..

4. Run the Application

You will need two terminal windows to run both servers simultaneously.

Terminal 1 (Backend Gateway):

cd backend
npm run dev

Runs on http://localhost:5001

Terminal 2 (React Dashboard):

cd frontend
npm run dev

Runs on http://localhost:5173

How to Use the Dashboard

  1. Open the dashboard in your browser.
  2. Click the "Fire Test Request" button to simulate external traffic hitting your proxy.
  3. Rapidly click the button to exhaust the Token Bucket and trigger the rate limiter.
  4. Watch the charts instantly stack red (Blocked) traffic over green (Success) traffic, while monitoring the latency curve for cold-start spikes.
  5. Click "Wipe Data" to clear the database directly from the UI and start a fresh testing session.

Configuration (Rate Limiting)

By default, the gateway is configured with a strict Token Bucket policy to demonstrate the blocking capabilities of the UI.

  • Burst Capacity (Bucket Size): 5 tokens total
  • Refill Rate: 1 token per second

How to adjust these limits:

Navigate to backend/src/rateLimiter.js and modify these two variables:

// backend/src/rateLimiter.js
const BUCKET_CAPACITY = 5; // Change this to increase/decrease instant burst allowance
const REFILL_RATE = 1;  // Change this to adjust how fast tokens regenerate

Note: If you change these values while the backend is running, Nodemon will automatically restart the server and apply your new limits instantly.

About

A production-grade Node.js API Gateway featuring a custom Token Bucket rate limiter, asynchronous MongoDB telemetry logging, and a real-time React analytics dashboard.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors