from flask import Flask, jsonify
from flask_cors import CORS
import time

app = Flask(__name__)
CORS(app)

request_count = 0


@app.route("/")
def home():
    global request_count
    request_count += 1

    return jsonify({
        "message": "Welcome to DevOps API",
        "status": "running"
    })


@app.route("/health")
def health():
    global request_count

    start_time = time.time()

    request_count += 1

    response_time = round((time.time() - start_time) * 1000, 2)

    return jsonify({
        "status": "healthy",
        "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
        "requests": request_count,
        "response_time_ms": response_time
    })


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)
