A FastAPI-based HTTP proxy that forwards requests to specified URLs, while caching responses using Redis to optimize repeated requests. This implementation includes detailed logging to track the flow of requests and caching mechanisms.
- Forward HTTP POST requests to specified URLs.
- Cache responses to avoid redundant network calls.
- Detailed logging for easy debugging and monitoring.
- Configurable with Redis for caching.
- Set up as a systemd service for automatic startup and resource monitoring.
- Python 3.7+
- FastAPI
- Redis (Upstash or local Redis instance)
- httpx
- uvicorn
-
Clone the repository:
git clone https://github.com/yourusername/fastapi-http-proxy-with-caching.git cd fastapi-http-proxy-with-caching -
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the dependencies:
pip install -r requirements.txt
Upstash is a serverless database solution that offers Redis-compatible caching with a pay-as-you-go pricing model. It is an excellent choice for applications that require scalable and cost-effective caching.
Upstash's pricing model is straightforward and cost-effective, making it a great choice for caching needs:
- Pay as You Go: $0.2 per 100K commands
- Pro 2K: $280 per month
- Pro 10K: $680 per month
For example, caching:
- 1 million objects: This would typically involve around 1 million commands. At $0.2 per 100K commands, the cost would be $2.
- 10 million requests: Assuming each request involves a caching command, this would cost $20 at the pay-as-you-go rate.
To use Upstash as your Redis provider, follow these steps:
-
Sign Up for Upstash:
- Go to Upstash's website and sign up for an account.
- Create a new Redis database and note the provided endpoint and credentials.
-
Configure Redis URL in the Application:
-
Open
main.pyand set theredis_urlvariable with your Upstash Redis credentials:redis_url = "redis://:your_upstash_password@your_upstash_endpoint:your_upstash_port"
-
-
Run the Application:
-
Start the FastAPI server using uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8000
-
Start the FastAPI server using uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8000Send a POST request to the /webhook-test/post-response endpoint with a url query parameter specifying the target URL.
Example:
curl -X POST "http://127.0.0.1:8000/webhook-test/post-response?url=https://example.com/api" -H "Content-Type: application/json" -d '{"key": "value"}'The application includes detailed logging to help trace the flow of requests and responses. Logs are printed to the console.
To ensure that your FastAPI application runs automatically on system restart and operates as a background service, you can set it up as a systemd service on your server.
-
Finding the Uvicorn Path:
Ensure that the virtual environment is activated:
source /path/to/your/venv/bin/activateThen find the path to the
uvicornexecutable:which uvicorn
This will output something like:
/path/to/your/venv/bin/uvicorn
Use this path in the
ExecStartdirective of your systemd service file. -
Finding Your Username:
Your username can be found by running:
whoami
This will output your current user's name, which should be used in the
Userdirective. -
Finding the Working Directory:
The working directory is where your FastAPI application (e.g.,
main.py) is located. Use thepwdcommand in your project directory to find the full path:pwdThis will output something like:
/path/to/your/fastapi-app
Use this path in the
WorkingDirectorydirective. -
Setting Up the Environment Path:
The environment path should point to the
bindirectory of your virtual environment. It is typically:/path/to/your/venv/bin
-
Create the Systemd Service File:
sudo nano /etc/systemd/system/fastapi.service
-
Add the Following Content to the Service File:
Replace the placeholders with the actual paths and user information found in the steps above.
[Unit] Description=FastAPI Service After=network.target [Service] User=your_username Group=www-data WorkingDirectory=/path/to/your/fastapi-app Environment="PATH=/path/to/your/venv/bin" ExecStart=/path/to/your/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000 [Install] WantedBy=multi-user.target
-
Reload Systemd Daemon:
sudo systemctl daemon-reload
-
Enable the FastAPI Service to Start on Boot:
sudo systemctl enable fastapi.service -
Start the FastAPI Service:
sudo systemctl start fastapi.service
-
Check the Status of the Service:
sudo systemctl status fastapi.service
You can monitor the system resource usage of the FastAPI service using standard Linux tools.
-
Check CPU and Memory Usage:
top
Find your service by its name or PID and monitor its resource usage.
-
Detailed Resource Usage:
htop
Use
htopfor a more user-friendly and detailed view of system resources. -
View Service Logs:
sudo journalctl -u fastapi.service -b
This command will display detailed logs for the FastAPI service, which can help diagnose any issues.
By following these steps, you can ensure that your FastAPI application runs correctly as a systemd service and can monitor its resource usage effectively. If there are any further issues, please provide detailed logs for further assistance.