An API Gateway for routing and proxying requests to various internal services, such as settings-service
and callback-router
, while handling JWT authentication and exposing metrics for monitoring.
To successfully deploy and run this project, you need the following:
- Docker: Installed Docker version 19.03 or higher
- Docker Compose: Installed Docker Compose version 1.27 or higher
- Postman/Insomnia or any other HTTP client for testing the API
-
Clone the repository:
git clone https://github.com/EvanBrightside/api-gateway.git cd api-gateway
-
Start the containers using Docker Compose:
Run the following command to build and start all containers:
docker-compose build docker-compose up
-
Verify that all services are running:
Make sure all containers have started successfully. You should see logs for the API Gateway and proxied services like
settings-service
andcallback-router
.
To access protected routes, JWT authentication is required. You can obtain a token by sending a request to the /auth
route.
Description: Authenticate and receive a JWT token.
URL: /auth/
Method: POST
Request Body (JSON):
{
"username": "admin"
}
Response Body (JSON):
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"expires_at_unix": 1727348342,
"expires_at_utc": "2024-09-26 10:59:02 UTC"
}
Example Request using curl:
curl -X POST http://localhost:8080/auth -d '{"username": "admin"}' -H "Content-Type: application/json"
Description: Proxies a request to the settings-service.
URL: /api/settings/
Method: POST
Authentication Required: Yes (Bearer JWT token)
Request Body (JSON):
{
"setting": "value"
}
Response Body (JSON):
{
"message": "Settings Service"
}
Example Request using curl:
curl -X POST http://localhost:8080/api/settings/ -d '{"setting": "value"}' -H "Authorization: Bearer <your-token>" -H "Content-Type: application/json"
Description: Proxies a request to the callback-router service.
URL: /api/callback-router/
Method: POST
Authentication Required: Yes (Bearer JWT token)
Request Body (JSON):
{
"callback": "value"
}
Response BODY (JSON):
{
"message": "Callback Router Service"
}
Example Request using curl:
curl -X POST http://localhost:8080/api/callback-router/ -d '{"callback": "value"}' -H "Authorization: Bearer <your-token>" -H "Content-Type: application/json"
- 401 Unauthorized: Authentication error if the token is invalid or missing.
Example response:
{
"error": "Invalid token"
}
- 502 Bad Gateway: Error proxying the request to the target service.
Example response:
{
"error": "Bad Gateway"
}
The API Gateway exposes metrics for Prometheus and can be visualized in Grafana.
Description: Returns Prometheus metrics.
- URL:
/metrics/
- Method:
GET
curl http://localhost:8080/metrics
Response: Returns metrics in Prometheus format.
Prometheus is set up to scrape the /metrics
endpoint from the API Gateway.
Once the containers are running, you can access the Prometheus dashboard by visiting:
http://localhost:9090
You can verify that Prometheus is correctly scraping the metrics from the API Gateway by searching for the metric api_gateway_requests_total
on the Prometheus UI.
Grafana can be used to visualize the metrics scraped by Prometheus. Here's how to set it up.
Once the containers are running, you can access the Grafana dashboard by visiting:
http://localhost:3000
Default credentials for Grafana:
- Username:
admin
- Password:
admin
You'll be prompted to change the password on the first login.
- Go to the Grafana Dashboard → Configuration → Data Sources.
- Click Add data source.
- Select Prometheus.
- Set the URL to
http://prometheus:9090
(orhttp://localhost:9090
if you're running Prometheus locally). - Click Save & Test to verify the connection.
-
After adding Prometheus as a data source, go to Create → Dashboard.
-
Add a new Graph panel.
-
In the Metrics tab, add your Prometheus metric, for example:
api_gateway_requests_total
-
You can now visualize the total number of requests processed by the API Gateway.
-
502 Bad Gateway: Occurs if the target service is unavailable or there is an error proxying the request.
-
401 Unauthorized: Authentication error, occurs when the JWT token is missing or invalid.
-
400 Bad Request: Request error, such as when the request body is malformed.
All logs for the API Gateway are available in real time through Docker:
docker-compose logs -f
-
Ensure that services like settings-service and callback-router are running correctly, as the API Gateway relies on them.
-
You can configure environment variables in the docker-compose.yml file if you need to specify custom parameters for each service.