الملفات
ghaymah_course_lab2/readme.md
2025-09-28 11:02:04 +00:00

2.8 KiB

بسم الله الرحمن الرحيم

API Development with Monitoring

Objective:
Build a demo API (e.g., for a store, booking system, or any domain of your choice) with 6-7 documented endpoints, implement logging, monitor errors via a Bash script, and automate API requests.


Part 1: API Implementation

Requirements:

  1. Framework: Use any backend framework (e.g., Flask, Django, Express, Spring Boot).
  2. Endpoints:
    • Include 6-7 RESTful endpoints (e.g., GET /items, POST /order, PUT /update).
    • Implement basic CRUD operations.
  3. Documentation:
    • Document all endpoints using Swagger/OpenAPI.
  4. Logging:
    • Configure the API to log all output (requests, errors, etc.) to a file (e.g., api.log).

Part 2: Log Monitoring with Bash

Requirements:

  1. Script: Write a Bash script (monitor.sh) that:
    • Continuously reads the API log file (api.log).
    • Detects HTTP error codes (e.g., 4XX, 5XX) or unusual patterns (e.g., "failed", "timeout").
  2. Alerting:
    • On error detection, the script must:
      • Capture server metadata (IP, CPU/RAM usage, disk space) using commands like df, free, top.
      • Send a JSON payload to a Ghaymah endpoint (URL provided later) via curl. Example payload:
        {
          "error": "500 Internal Server Error",
          "timestamp": "2023-11-15T12:00:00Z",
          "message": "db not connected",
          "server_metrics": {
            "ip": "192.168.1.1",
            "cpu_usage": "75%",
            "ram_available": "2GB",
            "disk_space": "50GB"
          }
        }
        

Part 3: API Automation Testing

Requirements:

  1. Script: Write a second Bash script (test_api.sh) to automate API requests:
    • Send GET, POST, PUT, DELETE requests to your endpoints.
    • Include payloads for POST/PUT (e.g., adding a new item).
    • Log responses to a file (test_results.log).

Submission Guidelines

  1. GitPasha Deployment:
    • Push all files to a public GitPasha repository (https://app.gitpasha.com/).
    • Repository must include:
      • API source code.
      • Bash scripts (monitor.sh, test_api.sh).
      • Log files (sample or generated).
  2. Documentation:
    • A README.md with:
      • API setup instructions.
      • Script usage examples.
      • Swagger link (e.g., http://localhost:5000/docs).
      • GitPasha repository URL.
  3. Demo:
    • Run the API, trigger errors, and show alerts in action during presentation.

Note:

  • The Ghaymah endpoint URL and authentication details will be shared later.
  • Ensure the GitPasha repository is public and well-organized.