129 أسطر
2.5 KiB
Markdown
129 أسطر
2.5 KiB
Markdown
# 🍎 Fruit API
|
|
|
|
A simple **RESTful API** built with Flask to manage fruits.
|
|
This API supports full **CRUD operations**, categorized fruit queries, and **search functionality**.
|
|
It also includes a **logging system** that saves logs locally and sends them to a remote dashboard for monitoring.
|
|
|
|
---
|
|
|
|
## 🚀 Features
|
|
- Get all fruits, get fruit by ID, add, update, and delete fruits.
|
|
- Search fruits by name or filter by category.
|
|
- Automatic **logging of every request and response**.
|
|
- Logs are stored in a local file (`fruit_api.log`) and sent to a remote dashboard (`https://deploy.ghaymah.systems/dashboard`).
|
|
- Health-check endpoint for monitoring.
|
|
- Dockerized for easy deployment.
|
|
|
|
---
|
|
|
|
## 📦 Requirements
|
|
- Python 3.9+
|
|
- Pip
|
|
|
|
Install dependencies:
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
---
|
|
|
|
## ▶️ Running Locally
|
|
|
|
1. Clone the repo:
|
|
```bash
|
|
git clone https://github.com/yourusername/fruit-api.git
|
|
cd fruit-api
|
|
```
|
|
|
|
2. Run the API:
|
|
```bash
|
|
python fruit_api.py
|
|
```
|
|
|
|
3. API starts at:
|
|
```
|
|
http://localhost:5000
|
|
```
|
|
|
|
---
|
|
|
|
## 🐳 Run with Docker
|
|
|
|
1. Build the image:
|
|
```bash
|
|
docker build -t fruit-api .
|
|
```
|
|
|
|
2. Run the container:
|
|
```bash
|
|
docker run -d -p 5000:5000 fruit-api
|
|
```
|
|
|
|
---
|
|
|
|
## 🌐 Endpoints
|
|
|
|
### Root
|
|
- `GET /` → Welcome message with available endpoints.
|
|
|
|
### Fruits
|
|
- `GET /fruits` → Get all fruits.
|
|
- `GET /fruits/<id>` → Get fruit by ID.
|
|
- `POST /fruits` → Add a new fruit.
|
|
Example body:
|
|
```json
|
|
{
|
|
"name": "Mango",
|
|
"color": "Yellow",
|
|
"price": 2.5,
|
|
"quantity": 50,
|
|
"category": "Tropical"
|
|
}
|
|
```
|
|
- `PUT /fruits/<id>` → Update an existing fruit.
|
|
- `DELETE /fruits/<id>` → Delete a fruit.
|
|
|
|
### Advanced Queries
|
|
- `GET /fruits/category/<category>` → Get fruits by category.
|
|
- `GET /fruits/search?name=<name>` → Search fruits by name.
|
|
|
|
### Health
|
|
- `GET /health` → Returns API health status.
|
|
|
|
---
|
|
|
|
## 📝 Logging
|
|
|
|
- Logs are stored locally in:
|
|
```
|
|
fruit_api.log
|
|
```
|
|
- Example log entry:
|
|
```
|
|
2025-09-27 23:01:15 [INFO] REQUEST: {'event': 'REQUEST', 'method': 'GET', 'path': '/fruits', 'ip': '127.0.0.1'}
|
|
```
|
|
|
|
|
|
|
|
## 🔍 Monitoring Script
|
|
A sample `monitor_logs.sh` is included to check log stats (200, 404, etc.) every 2 hours:
|
|
```bash
|
|
#!/bin/bash
|
|
LOG_FILE="fruit_api.log"
|
|
echo "📊 Log Report - $(date)"
|
|
grep "RESPONSE" $LOG_FILE | awk '{print $0}' | grep -oP "status': \K\d+" | sort | uniq -c
|
|
```
|
|
|
|
Run manually:
|
|
```bash
|
|
bash monitor_logs.sh
|
|
```
|
|
|
|
Or add to cron (every 2 hours):
|
|
```bash
|
|
0 */2 * * * /path/to/monitor_logs.sh >> log_report.txt
|
|
```
|
|
|
|
---
|
|
|