75166ab28752dc6048e9fda65adab993a0e1f1f7
🍎 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:
pip install -r requirements.txt
▶️ Running Locally
-
Clone the repo:
git clone https://github.com/yourusername/fruit-api.git cd fruit-api
-
Run the API:
python fruit_api.py
-
API starts at:
http://localhost:5000
🐳 Run with Docker
-
Build the image:
docker build -t fruit-api .
-
Run the container:
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:{ "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:
#!/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 monitor_logs.sh
Or add to cron (every 2 hours):
0 */2 * * * /path/to/monitor_logs.sh >> log_report.txt
الوصف
اللغات
Python
70.3%
Shell
24.7%
Dockerfile
5%