This is an API Gateway which serves as a central hub for all services.
This project uses GitHub Actions
.
This package uses pipenv for dependency management. And Requires Python 3.10
- Clone the repository
git clone "git@github.com:tomasanchez/grupo-3-tacs.git"
- Create the virtual environment.
cd grupo-3-tacs/api-gateway && python -m venv venv
- Install the dependencies.
cd grupo-3-tacs/api-gateway && pip install -r requirements-dev.txt
- Activate the virtual environment.
Use the activation script for your shell.
venv/bin/activate
Note: this may vary depending on your OS and shell.
- Run:
uvicorn app.main:app --reload
- Go to http://127.0.0.0:8000/docs to see the API documentation.
Run:
pytest
Variables prefixed with FASTAPI_
are used to configure the application.
Name | Description | Default Value |
---|---|---|
FASTAPI_DEBUG | Debug Mode | False |
FASTAPI_PROJECT_NAME | Swagger Title | API GATEWAY |
FASTAPI_PROJECT_DESCRIPTION | Swagger Description | ... |
FASTAPI_USE_LIMITER | Toggles Rate Limiting | False |
FASTAPI_LIMITER_THRESHOLD | Maximum requests number | 10 |
FASTAPI_LIMITER_INTERVAL | Time in which the threshold is reset in minutes | 1 |
FASTAPI_USE_LIMITER | Toggles Rate Limiting | False |
FASTAPI_VERSION | Application Version | app.version |
FASTAPI_DOCS_URL | Swagger Endpoint | /docs |
Variables prefixed with GATEWAY_
are used to configure the gateway.
Name | Description | Default Value |
---|---|---|
GATEWAY_SERVICES | Available services to connect | see below* |
GATEWAY_TIMEOUT | Requests time out in seconds | 59 |
Service interface
class Service:
name: str
base_url: str
readiness_url: str = "/readiness"
health_url: str = "/health"
Eg:
[
{
"name": "users",
"base_url": "http://users:8001",
"readiness_url": "actuator/readiness"
},
{
"name": "auth",
"base_url": "http://auth:8000"
}
]
Variables prefixed with REDIS_
are used to configure the redis connection.
Name | Description | Default Value |
---|---|---|
REDIS_HOST | Redis Host | localhost |
REDIS_PORT | Redis Port | 6379 |
REDIS_CLUSTER | If connection is using a cluster | False |
REDIS_ACTIVE | Whether to use redis or not | True |
REDIS_USERNAME | DB Username | |
REDIS_PASSWORD | DB Password |
This project uses the Docker
image uvicorn-gunicorn-fastapi:python3.10-slim
for superior performance.
This API Gateway uses a Redis
instance to store the rate limit counters.
The rate limit is set to X
requests per T
seconds.
Our FastAPI
application calls a middleware function which checks the rate limit before request is served.
It uses the RateLimiter
to increment the counter for the request host and checks if it has exceeded the limit, if so,
it returns it interrupts the execution sending a response with a 429
status code: too many requests.
The RateLimiter
implementation uses the Redis
instance to store the counters with the INCR
command, which
guarantees that is run atomically. RedisConnector
is a wrapper around the Redis
client which provides common methods
an errors for using both a single redis connection or a cluster. RedisClient
uses aioredis
for as ync operations.