Skip to content

Soumik8114/Gateway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

17 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌐 Gateway

A multi-tenant API Gateway built with a split-architecture design β€” a Django Control Plane for management and a FastAPI Data Plane for high-performance request proxying, authentication, and rate limiting.


πŸ“‹ Table of Contents


Overview

Gateway is a self-hosted API gateway that lets you register upstream APIs behind a unified proxy layer. Each tenant (organization) can register multiple APIs, generate API keys with custom rate-limit plans, and route traffic through the gateway β€” all with built-in authentication, per-minute and per-month rate limiting, and usage tracking.


Architecture

The project follows a two-plane architecture:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         CONTROL PLANE           β”‚      β”‚          DATA PLANE              β”‚
β”‚         (Django)                β”‚      β”‚          (FastAPI)               β”‚
β”‚                                 β”‚      β”‚                                  β”‚
β”‚  β€’ Tenant registration/login    β”‚      β”‚  β€’ Reverse proxy to upstream     β”‚
β”‚  β€’ API registration             β”‚      β”‚  β€’ API key authentication        β”‚
β”‚  β€’ API key generation           β”‚      β”‚  β€’ Per-minute rate limiting      β”‚
β”‚  β€’ Billing plan management      β”‚      β”‚  β€’ Per-month rate limiting       β”‚
β”‚  β€’ Dashboard UI                 β”‚      β”‚  β€’ Usage tracking (Redis)        β”‚
β”‚  β€’ Django Admin                 β”‚      β”‚  β€’ Client ID-based routing       β”‚
β”‚                                 β”‚      β”‚                                  β”‚
β”‚  Port: 8000                     β”‚      β”‚  Port: 7000                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
             β”‚                                          β”‚
             └──────────── Shared SQLite DB β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  +
                            Redis (usage/rate limits)

Features

  • Multi-Tenancy β€” Each user registers as a tenant with a unique slug. Tenants are fully isolated.
  • API Registration β€” Register upstream APIs with a name, slug, and base URL. Access them through the gateway at /{tenant_slug}/{api_slug}/{path}.
  • API Key Authentication β€” SHA-256 hashed API keys passed via the X-API-Key header. Keys are shown only once on creation.
  • Client ID Support β€” Optional X-Client-ID header for per-client rate limiting within a tenant.
  • Billing Plans β€” Create plans with configurable requests_per_minute and requests_per_month limits.
  • Rate Limiting β€” Redis-backed per-minute and per-month rate limiting enforced at the data plane.
  • Usage Tracking β€” Asynchronous background usage recording to Redis.
  • Dashboard UI β€” Dark-themed tenant dashboard to manage APIs, keys, and plans.
  • Graceful Fallback β€” Falls back to fakeredis if Redis is unavailable, so development works without Redis.

Project Structure

Gateway/
β”œβ”€β”€ control_plane/              # Django management app (port 8000)
β”‚   β”œβ”€β”€ control_plane/          # Django project settings & URLs
β”‚   β”‚   β”œβ”€β”€ settings.py
β”‚   β”‚   β”œβ”€β”€ urls.py
β”‚   β”‚   └── wsgi.py
β”‚   β”œβ”€β”€ tenants/                # Tenant registration, login, dashboard
β”‚   β”‚   β”œβ”€β”€ models.py           # Tenant model
β”‚   β”‚   β”œβ”€β”€ views.py            # Auth views, dashboard, API/key creation
β”‚   β”‚   β”œβ”€β”€ forms.py            # RegisterForm, APIForm, APIKeyForm
β”‚   β”‚   β”œβ”€β”€ urls.py
β”‚   β”‚   β”œβ”€β”€ tests.py
β”‚   β”‚   β”œβ”€β”€ templates/          # Dashboard, login, register HTML
β”‚   β”‚   └── static/             # CSS styles
β”‚   β”œβ”€β”€ apis/                   # API & APIKey models and views
β”‚   β”‚   β”œβ”€β”€ models.py           # API, APIKey, Client models
β”‚   β”‚   β”œβ”€β”€ views.py
β”‚   β”‚   └── templates/
β”‚   β”œβ”€β”€ billing/                # Billing plan model
β”‚   β”‚   └── models.py           # Plan model (RPM & RPM limits)
β”‚   β”œβ”€β”€ usage/                  # Usage tracking app
β”‚   β”œβ”€β”€ setup_test_data.py      # Script to seed test data
β”‚   └── manage.py
β”œβ”€β”€ data_plane/                 # FastAPI proxy app (port 7000)
β”‚   └── fastapi_app/
β”‚       β”œβ”€β”€ main.py             # FastAPI app factory
β”‚       β”œβ”€β”€ proxy.py            # Reverse proxy + auth + rate limiting
β”‚       β”œβ”€β”€ dependencies.py     # X-API-Key header extraction
β”‚       β”œβ”€β”€ tables.py           # SQLAlchemy table definitions
β”‚       β”œβ”€β”€ config.py           # Database & Redis URL configuration
β”‚       β”œβ”€β”€ lifespan.py         # App startup/shutdown (DB, Redis, HTTP)
β”‚       β”œβ”€β”€ state.py            # AppState dataclass
β”‚       └── usage.py            # Async usage recording
β”œβ”€β”€ requirements.txt
└── .gitignore

Tech Stack

Layer Technology
Control Plane Django 5.2, SQLite
Data Plane FastAPI, Uvicorn, httpx, SQLAlchemy
Rate Limiting Redis (with fakeredis fallback)
Database SQLite (shared between planes)
Templating Django Templates, Tailwind CSS

Getting Started

Prerequisites

  • Python 3.10+
  • Redis (optional β€” falls back to fakeredis for development)
  • Docker + Docker Compose (optional β€” for the fully dockerized setup)

Installation

# Clone the repository
git clone https://github.com/Soumik8114/Gateway.git
cd Gateway

# Create and activate a virtual environment
python -m venv venv
source venv/bin/activate        # Linux/macOS
venv\Scripts\activate           # Windows

# Install dependencies
pip install -r requirements.txt

Database Setup

cd control_plane

# Run migrations
python manage.py migrate

# Create a superuser (for Django Admin)
python manage.py createsuperuser

Running the Services

Terminal 1 β€” Control Plane (Django):

cd control_plane
python manage.py runserver 8000

Terminal 2 β€” Data Plane (FastAPI):

uvicorn data_plane.fastapi_app.main:app --host 0.0.0.0 --port 7000 --reload

Running with Docker (Compose)

This repo includes a docker-compose.yml that runs the full stack:

  • control_plane (Django) on http://localhost:8000
  • data_plane (FastAPI) on http://localhost:7000
  • redis on localhost:6379

The Control Plane and Data Plane share the same SQLite DB via a named Docker volume mounted at /data/db.sqlite3.

Start (foreground):

docker compose up --build

Start (background):

docker compose up --build -d

Stop:

docker compose down

Stop and remove volumes (clears Redis + SQLite data):

docker compose down -v

Django management commands (Docker)

The control_plane container runs migrations automatically on startup, but you’ll still likely want a superuser for Django Admin.

Create a superuser:

docker compose exec control_plane python manage.py createsuperuser

Run migrations manually (optional):

docker compose exec control_plane python manage.py migrate

Seed test data:

docker compose exec control_plane python setup_test_data.py

Run tests:

docker compose exec control_plane python manage.py test

Usage

Register a Tenant

  1. Navigate to http://localhost:8000/register/
  2. Fill in your username, email, tenant name, and password
  3. You'll be redirected to the tenant dashboard

Register an API

From the dashboard, register an upstream API:

Field Example
Name My API
Slug my-api
Upstream Base URL https://httpbin.org
Auth Header Name X-API-Key

Generate an API Key

From the dashboard, create an API key with a billing plan:

Field Example
Plan Name Starter Plan
Requests per Minute 60
Requests per Month 10000

⚠️ The raw API key is shown only once. Save it securely.

Proxy a Request

Send requests through the gateway:

curl -H "X-API-Key: <your-api-key>" \
     http://localhost:7000/{tenant-slug}/{api-slug}/get

Example:

curl -H "X-API-Key: secret_key_12345" \
     http://localhost:7000/test-tenant/test-api/get

This proxies the request to https://httpbin.org/get.


Rate Limiting

Rate limits are enforced at the data plane using Redis:

Limit Type Scope Response on Exceed
Per-Minute Per API Key or Client ID 429 Rate limit exceeded
Per-Month Per API Key or Client ID 429 Monthly rate limit exceeded

If an X-Client-ID header is provided, rate limits are applied per client rather than per API key.


Test Data

Seed the database with sample data for development:

cd control_plane
python setup_test_data.py

This creates:

Resource Value
User testuser / password
Tenant test-tenant
API test-api β†’ https://httpbin.org
Plan 5 req/min, 100 req/month
API Key secret_key_12345

Running Tests

cd control_plane
python manage.py test

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •