|
1 | | -# Custom-API-Gateway |
| 1 | +# RouteX - Custom API Gateway with Rate Limiting & JWT Authentication |
| 2 | + |
| 3 | +**RouteX** is a **custom-built API Gateway** built from scratch using **Go (backend)** and **React + Vite + TypeScript (frontend)**. It provides: |
| 4 | + |
| 5 | +* **Dynamic Route Mapping** (to upstream services) |
| 6 | +* **JWT & API Key Authentication** |
| 7 | +* **Per-user Rate Limiting (token bucket)** |
| 8 | +* **Admin Panel** to manage API Keys, Routes, and Test APIs |
| 9 | +* **MongoDB Integration** for persistence |
| 10 | +* **Dockerized setup** for easy deployment |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +### Backend (Go API Gateway) |
| 15 | + |
| 16 | +1. **Dynamic Route Mapping** → Map `/service-a` → `http://upstream-service-a.local` |
| 17 | + |
| 18 | +2. **Authentication** |
| 19 | + |
| 20 | + * Supports **JWT** (`Authorization: Bearer <token>`) |
| 21 | + * Supports **API Key** (`X-API-Key` header) |
| 22 | + |
| 23 | +3. **Rate Limiting** |
| 24 | + |
| 25 | + * Token bucket per API key/user |
| 26 | + * Configurable request rate (`N requests/minute`) |
| 27 | + |
| 28 | +4. **MongoDB Integration** |
| 29 | + |
| 30 | + * `api_keys` collection → stores key + rate limit |
| 31 | + * `routes` collection → stores path + upstream target |
| 32 | + |
| 33 | +5. **Admin APIs** |
| 34 | + |
| 35 | + * `/admin/api-keys` → Create/Delete API Keys |
| 36 | + * `/admin/routes` → Create/Delete Routes |
| 37 | + * `/admin/generate-token` → Generate JWT for an API Key |
| 38 | + |
| 39 | +### Frontend (React + Vite + TypeScript) |
| 40 | + |
| 41 | +1. **Admin Dashboard** with pages: |
| 42 | + |
| 43 | + * **API Keys**: Create & manage API keys & rate limits |
| 44 | + * **Routes**: Create & manage route mappings |
| 45 | + * **JWT Generator**: Generate a JWT for any API Key |
| 46 | + * **API Tester**: Test requests through the Gateway with JWT or API Key |
| 47 | + |
| 48 | +2. **Axios API Client**: Communicates with backend |
| 49 | +3. **CORS enabled** for dev (`localhost:5173` → `localhost:8080`) |
| 50 | + |
| 51 | +### Web Client |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +### Authentication Flow |
| 62 | + |
| 63 | +1. **Admin** creates an API Key via admin panel |
| 64 | +2. **JWT Generator** can issue a short-lived JWT for that API Key |
| 65 | +3. Clients call Gateway using **either**: |
| 66 | + |
| 67 | + * `Authorization: Bearer <jwt>` |
| 68 | + * `X-API-Key: <raw-api-key>` |
| 69 | + |
| 70 | +Backend validates token → checks MongoDB → enforces rate limit → forwards to upstream service. |
| 71 | + |
| 72 | +### Rate Limiting |
| 73 | + |
| 74 | +Implemented using **Token Bucket Algorithm** per API Key: |
| 75 | + |
| 76 | +* Each API key has a **max tokens** (rate limit) |
| 77 | +* Tokens refill every **minute** |
| 78 | +* Requests consume 1 token → If no tokens left → **429 Too Many Requests** |
| 79 | + |
| 80 | +## Backend API Endpoints |
| 81 | + |
| 82 | +| Method | Path | Description | |
| 83 | +| ------ | ----------------------- | ------------------------------------ | |
| 84 | +| GET | `/admin/api-keys` | List API keys | |
| 85 | +| POST | `/admin/api-keys` | Create API key `{key, rate_limit}` | |
| 86 | +| DELETE | `/admin/api-keys/:id` | Delete API key | |
| 87 | +| GET | `/admin/routes` | List routes | |
| 88 | +| POST | `/admin/routes` | Create route `{path, target_url}` | |
| 89 | +| DELETE | `/admin/routes/:id` | Delete route | |
| 90 | +| POST | `/admin/generate-token` | Generate JWT for an API Key | |
| 91 | +| ANY | `/your-service-path` | Forwards request to upstream service | |
| 92 | + |
| 93 | +## Frontend Pages |
| 94 | + |
| 95 | +1. **API Keys Manager**: CRUD for API keys |
| 96 | +2. **Routes Manager**: CRUD for route mappings |
| 97 | +3. **JWT Generator**: Input an API key → Get a signed JWT |
| 98 | +4. **API Tester**: Input path, select auth method (JWT/API Key), test request |
| 99 | + |
| 100 | +## Installation and Setup |
| 101 | + |
| 102 | +1. Clone the repository: |
| 103 | + |
| 104 | + ```bash |
| 105 | + git clone https://github.com/Harshvardhan2164/Custom-API-Gateway.git |
| 106 | + cd Custom-API-Gateway/ |
| 107 | + ``` |
| 108 | + |
| 109 | +2. **Start MongoDB, Backend & Frontend** |
| 110 | + |
| 111 | + ```bash |
| 112 | + docker compose build |
| 113 | + docker compose up -d |
| 114 | + ``` |
| 115 | + |
| 116 | +3. **Open Admin Panel** → `http://localhost:5173` |
| 117 | + |
| 118 | +4. **Create an API Key** with a rate limit |
| 119 | + |
| 120 | +5. **Create Routes** mapping `/service` → `http://httpbin.org/get` (or any upstream) |
| 121 | + |
| 122 | +6. **Generate JWT** for that API Key |
| 123 | + |
| 124 | +7. **Use API Tester** → send request with JWT or API Key |
| 125 | + |
| 126 | +8. If you exceed the rate limit → **429 Too Many Requests** |
| 127 | + |
| 128 | +## Future Enhancements |
| 129 | + |
| 130 | +* Redis-based distributed rate limiting |
| 131 | +* Multi-route custom rate limits |
| 132 | +* OAuth2 support |
| 133 | +* TLS termination with Nginx |
| 134 | + |
| 135 | +## Contributing |
| 136 | + |
| 137 | +Feel free to fork the repository, open issues, or submit pull requests. |
| 138 | + |
| 139 | +## License |
| 140 | + |
| 141 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments