This project is a self-hosted API gateway that allows you to configure and route requests to different API endpoints. It provides a simple frontend interface to add API configurations and a backend server that dynamically routes requests based on the stored configurations.
Clone the repository:
git clone https://github.com/cfreeman29/api-gateway.git
Navigate to the project directory:
cd api-gateway
Create a virtual environment for the Python backend:
python -m venv venv
Activate the virtual environment:
source venv/bin/activate
venv\Scripts\activate
Install the required Python packages:
pip install flask flask-cors requests
Start the backend server:
python app.py
The backend server will run on http://localhost:3000
.
Install Miniconda or Anaconda if you haven't already. You can download the appropriate version for your operating system from the official Miniconda or Anaconda website.
Open a terminal or command prompt.
Create a new conda environment for the project:
conda create --name api-gateway python=3.x
Replace 3.x
with the desired Python version (e.g., 3.9
).
Activate the conda environment:
conda activate api-gateway
Install the required Python packages:
pip install flask flask-cors requests
Start the backend server:
python app.py
The backend server will run on http://localhost:3000
.
frontend
directory:
cd frontend
node app.js
The frontend server will run on http://localhost:8000
.
The API gateway uses MongoDB to persist the API configurations. You can customize the MongoDB connection details by modifying the following constants in the config.py
file:
MONGODB_URI
: The URI of the MongoDB server (default: 'mongodb://localhost:27017').MONGODB_DB
: The name of the MongoDB database (default: 'api_gateway').MONGODB_COLLECTION
: The name of the MongoDB collection to store the API configurations (default: 'api_configs').Make sure to update these constants according to your MongoDB setup before running the API gateway.
Open a web browser and navigate to http://localhost:8000
.
You will see a simple form where you can enter the following details:
Fill in the form with the desired API configuration and click the "Add Configuration" button.
The API configuration will be sent to the backend and stored.
To route requests through the API gateway, make a request to http://localhost:3000/<path>
, where <path>
matches the configured path prefix.
For example, if you configured an API endpoint with the path /api/data
and the API endpoint https://api.example.com/data
, making a request to http://localhost:3000/api/data
will route the request to https://api.example.com/data
.
If a bearer token is provided in the configuration, it will be included in the Authorization
header of the routed request.
The API gateway will forward the response from the target API back to the client.
The API configurations are stored in memory within the Python backend. Each configuration consists of the following fields:
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License.