00d4b0a5ff07a72699b25250bacd9b7feac28530
🍎 Fruit Store API
A simple RESTful API built with Flask that manages fruits in a store.
The project also includes logging, a log monitoring script, and automated testing script.
📂 Project Structure
fruit-api/
│── fruit_api.py # Main Flask API
│── requirements.txt # Dependencies
│── Dockerfile # Docker support
│── log_monitor.sh # Log monitoring script
│── test_api.sh # Automated testing script
│── fruit_api.log # API logs
│── README.md # Documentation
│── .gitignore # Ignore venv/logs
📌 Features
- CRUD operations on fruits (
GET
,POST
,PUT
,DELETE
) - Search fruits by name
- Filter fruits by category
- Health check endpoint
- Logging of all requests & responses to
fruit_api.log
- Bash script for monitoring logs (
log_monitor.sh
) - Bash script for automated API testing (
test_api.sh
) - Swagger/OpenAPI documentation
⚙️ Setup Instructions
1. Clone the Repository
git clone https://github.com/YOUR-USERNAME/fruit-api.git
cd fruit-api
2. Create Virtual Environment & Install Dependencies
python3 -m venv venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
pip install -r requirements.txt
3. Run the API
python fruit_api.py
or
python3 fruit_api.py
API will start at:
👉 http://localhost:5000
🚀 API Endpoints
Base URL
http://localhost:5000
Endpoints
Method | Endpoint | Description |
---|---|---|
GET | /fruits |
Get all fruits |
GET | /fruits/<id> |
Get fruit by ID |
POST | /fruits |
Add a new fruit |
PUT | /fruits/<id> |
Update an existing fruit |
DELETE | /fruits/<id> |
Delete a fruit |
GET | /fruits/category/<category> |
Get fruits by category |
GET | /fruits/search?name=<name> |
Search fruits by name |
GET | /health |
Health check |
📑 Example Usage (with curl)
- Get all fruits
curl http://localhost:5000/fruits
- Add a fruit
curl -X POST http://localhost:5000/fruits -H "Content-Type: application/json" -d '{"name":"Mango","color":"Yellow","price":2.5,"quantity":50,"category":"Tropical"}'
- Update a fruit
curl -X PUT http://localhost:5000/fruits/1 -H "Content-Type: application/json" -d '{"name":"Green Apple","price":1.8}'
📝 Logging
- All API requests and responses are saved in:
fruit_api.log
Example log:
2025-09-29 18:05:22 - IP:127.0.0.1 - METHOD:GET - ENDPOINT:/fruits - STATUS:200
🔎 Monitoring (log_monitor.sh)
The log_monitor.sh
script:
- Monitors
fruit_api.log
every 2 hours - Detects errors (4XX, 5XX) and unusual keywords (
failed
,timeout
) - Captures server metrics (IP, CPU, RAM, Disk)
- Sends an alert JSON to Ghaymah endpoint
Run it in background:
bash log_monitor.sh &
🤖 API Testing (test_api.sh)
The test_api.sh
script:
- Runs automated
GET
,POST
,PUT
,DELETE
requests - Logs results in
test_results.log
Run tests:
bash test_api.sh
📘 Swagger Documentation
After starting the API, open:
👉 http://localhost:5000/docs
🐳 Docker Support
Build the image
docker build -t fruit-api .
Run container
docker run -p 5000:5000 fruit-api
الوصف
اللغات
Python
70.3%
Shell
24.7%
Dockerfile
5%