from flask import Flask, render_template
import os
import requests

app = Flask(__name__)
CHECKER_URL = os.environ.get("CHECKER_URL", "http://checker:9090/get_flag")


@app.route("/", methods=["GET"])
def index():
    return render_template("index.html")


@app.route("/flag", methods=["GET"])
def flag():
    try:
        resp = requests.get(CHECKER_URL, timeout=3)
        data = resp.json()
        if data.get("status") == "fixed" and data.get("flag"):
            return render_template("index.html", flag=data.get("flag"))
        return render_template(
            "index.html",
            error=data.get("message", "Флаг недоступен. Чекер еще видит уязвимость."),
        )
    except requests.exceptions.RequestException as exc:
        return render_template("index.html", error=f"Ошибка чекера: {exc}"), 502


if __name__ == "__main__":
    port = int(os.environ.get("PORT", "5000"))
    app.run(host="0.0.0.0", port=port)
