176 أسطر
4.2 KiB
Markdown
176 أسطر
4.2 KiB
Markdown
|
|
# 🍎 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** with clear pass/fail output.
|
|
|
|
---
|
|
## 📂 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
|
|
│── test_results.log # API tests logs
|
|
│── 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`) with **error alerts (4xx/5xx)**
|
|
- Bash script for automated API testing (`test_api.sh`) with ✅ / ❌ results
|
|
- Swagger/OpenAPI documentation at `/docs`
|
|
|
|
---
|
|
|
|
## ⚙️ Setup Instructions
|
|
|
|
### 1. Clone the Repository
|
|
```bash
|
|
git clone https://github.com/YOUR-USERNAME/fruit-api.git
|
|
cd fruit-api
|
|
```
|
|
|
|
### 2. Create Virtual Environment & Install Dependencies
|
|
```bash
|
|
python3 -m venv venv
|
|
source venv/bin/activate # Linux/Mac
|
|
venv\Scripts\activate # Windows
|
|
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
### 3. Run the API
|
|
```bash
|
|
python fruit_api.py
|
|
```
|
|
or
|
|
```bash
|
|
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**
|
|
```bash
|
|
curl http://localhost:5000/fruits
|
|
```
|
|
|
|
- **Add a fruit**
|
|
```bash
|
|
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**
|
|
```bash
|
|
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
|
|
bash log_monitor.sh &
|
|
```
|
|
|
|
---
|
|
|
|
## 🤖 API Testing (test_api.sh)
|
|
|
|
The `test_api.sh` script:
|
|
- Runs automated `GET`, `POST`, `PUT`, `DELETE` requests
|
|
- Prints ✅ if the status is correct (200/201), ❌ otherwise
|
|
- Logs results in `test_results.log`
|
|
|
|
Run tests:
|
|
```bash
|
|
bash test_api.sh
|
|
```
|
|
|
|
Example output:
|
|
```
|
|
🚀 Running API tests at Tue 30 Sep 2025 10:54:01 PM EEST
|
|
🔹 Testing /health ... ✅
|
|
🔹 Testing GET /fruits ... ✅
|
|
🔹 Testing POST /fruits ... ✅
|
|
🔹 Testing PUT /fruits/1 ... ✅
|
|
🔹 Testing DELETE /fruits/1 ... ✅
|
|
✅ All tests passed!
|
|
```
|
|
|
|
---
|
|
|
|
## 📘 Swagger Documentation
|
|
After starting the API, open:
|
|
👉 [http://localhost:5000/docs](http://localhost:5000/docs)
|
|
|
|
---
|
|
|
|
## 🐳 Docker Support
|
|
|
|
### Build the image
|
|
```bash
|
|
docker build -t fruit-api .
|
|
```
|
|
|
|
### Run container
|
|
```bash
|
|
docker run -p 5000:5000 fruit-api
|