From 0322ba8376a46500a09df41e98ab311f207d3a85 Mon Sep 17 00:00:00 2001 From: Moustafa Medhat Date: Tue, 28 Jul 2026 12:50:45 +0000 Subject: [PATCH] Initial commit --- .dockerignore | 8 +++ .gitignore | 3 + Dockerfile | 16 +++++ app.py | 70 ++++++++++++++++++ health-check.sh | 30 ++++++++ requirements.txt | 1 + templates/dashboard.html | 151 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 279 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 app.py create mode 100755 health-check.sh create mode 100644 requirements.txt create mode 100644 templates/dashboard.html diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..398ddac --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +__pycache__/ +*.pyc +*.pyo +*.pyd +.git +.gitignore +.env +venv/ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a965659 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +*.pyc +health.log diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..06f1b97 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM python:3.11-slim + +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app + +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +EXPOSE 8080 + +CMD ["python", "app.py"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..59c70c1 --- /dev/null +++ b/app.py @@ -0,0 +1,70 @@ +from flask import Flask, jsonify, render_template, request +import time + +app = Flask(__name__) + +# Application start time +start_time = time.time() + +# Metrics +request_count = 0 +last_response_time = 0 + + +@app.before_request +def before_request(): + request.start_time = time.perf_counter() + + +@app.after_request +def after_request(response): + global request_count + global last_response_time + + request_count += 1 + last_response_time = round( + (time.perf_counter() - request.start_time) * 1000, 2 + ) + + return response + + +@app.route("/") +def home(): + return render_template("dashboard.html") + + +@app.route("/health") +def health(): + return jsonify({ + "status": "UP", + "message": "Application is healthy", + "uptime_seconds": round(time.time() - start_time, 2), + "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), + "version": "1.0.0" + }), 200 + + +@app.route("/metrics") +def metrics(): + return jsonify({ + "status": "UP", + "requests": request_count, + "response_time_ms": last_response_time, + "uptime_seconds": round(time.time() - start_time, 2) + }) + + +@app.route("/api/info") +def api_info(): + return jsonify({ + "application": "Ghaymah SRE API", + "language": "Python", + "framework": "Flask", + "version": "1.0.0", + "author": "Moustafa Medhat" + }) + + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8080) diff --git a/health-check.sh b/health-check.sh new file mode 100755 index 0000000..d9af605 --- /dev/null +++ b/health-check.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +URL="http://localhost:8080/health" +LOG_FILE="/home/ec2-user/projects/ghaymah-exam-moustafa-medhat-sre/q1-deploy-monitor/health.log" + +touch "$LOG_FILE" + +echo "===== Health Monitor Started =====" >> "$LOG_FILE" + +while true +do + START=$(date +%s%3N) + + HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$URL") + + END=$(date +%s%3N) + RESPONSE_TIME=$((END - START)) + + TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") + + if [ "$HTTP_CODE" -eq 200 ]; then + STATUS="UP" + else + STATUS="DOWN" + fi + + echo "[$TIMESTAMP] Status=$STATUS | HTTP=$HTTP_CODE | Response=${RESPONSE_TIME}ms" >> "$LOG_FILE" + + sleep 30 +done diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..22ac75b --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Flask==3.1.0 diff --git a/templates/dashboard.html b/templates/dashboard.html new file mode 100644 index 0000000..fe19a0d --- /dev/null +++ b/templates/dashboard.html @@ -0,0 +1,151 @@ + + + + + + Ghaymah SRE Dashboard + + + + + + +
+ +

🚀 Ghaymah SRE Dashboard

+ +
+ +
+
Status
+
Loading...
+
+ +
+
Response Time
+
0 ms
+
+ +
+
Requests
+
0
+
+ +
+ + + +
+ + + +