from flask import Flask, Response, request

app = Flask(__name__)

with open("bomb.png.br", "rb") as f:
    BOMB_BR = f.read()

with open("bomb.png.gz", "rb") as f:
    BOMB_GZIP = f.read()

print(f"[startup] br={len(BOMB_BR):,} bytes  gzip={len(BOMB_GZIP):,} bytes")


def _pick_encoding():
    accept = request.headers.get("Accept-Encoding", "")
    if "br" in accept:
        return "br", BOMB_BR
    if "gzip" in accept:
        return "gzip", BOMB_GZIP
    return "gzip", BOMB_GZIP


@app.route("/")
def index():
    encoding, body = _pick_encoding()
    return Response(
        body,
        mimetype="image/png",
        headers={
            "Content-Encoding": encoding,
            "Content-Length": str(len(body)),
            "Vary": "Accept-Encoding",
        },
    )


app.run(host="0.0.0.0", port=8393, threaded=True)
