Download app.py from smokymath/minebutt: direct link, hf CLI and curl.
- Browser
- Download file 2.66 kB
-
https://huggingface.co/spaces/smokymath/minebutt/resolve/main/app.py
- Command line
-
hf download hf://spaces/smokymath/minebutt/app.py
-
curl -L -o app.py https://huggingface.co/spaces/smokymath/minebutt/resolve/main/app.py
2.66 kB
| from flask import Flask | |
| import subprocess | |
| import time | |
| import json | |
| import os | |
| import random | |
| import logging | |
| app = Flask(__name__) | |
| logging.basicConfig(level=logging.INFO) | |
| def start_ngrok(ngrok_auth, max_retries=5, initial_delay=5): | |
| """Start ngrok with exponential backoff retry strategy""" | |
| attempt = 0 | |
| delay = initial_delay | |
| while attempt < max_retries: | |
| try: | |
| logging.info(f"Starting ngrok attempt {attempt + 1}/{max_retries}") | |
| proc = subprocess.Popen( | |
| ["ngrok", "tcp", "25565", "--region=eu", "--log=stdout", "--authtoken", ngrok_auth], | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT, | |
| text=True | |
| ) | |
| tunnel_url = None | |
| for _ in range(40): # Increased timneout to 40 seconds per attempt | |
| try: | |
| result = subprocess.check_output( | |
| ["curl", "-s", "http://localhost:4040/api/tunnels"], | |
| timeout=8 # Increased individual request timeout | |
| ) | |
| data = json.loads(result) | |
| if data.get("tunnels"): | |
| tunnel_url = data["tunnels"][0]["public_url"] | |
| logging.info(f"🔗 Tunnel Established: {tunnel_url}") | |
| return tunnel_url.replace("tcp://", "") | |
| except (subprocess.CalledProcessError, json.JSONDecodeError): | |
| time.sleep(1) | |
| continue | |
| # If we exit loop without returning, log and retry | |
| logging.warning("Ngrok tunnel timeout, retrying...") | |
| except Exception as e: | |
| logging.error(f"Attempt {attempt + 1} failed: {str(e)}") | |
| # Exponential backoff with jitter | |
| time.sleep(delay + (random.random() * 2)) | |
| delay *= 2 | |
| attempt += 1 | |
| raise Exception(f"Failed to establish ngrok tunnel after {max_retries} attempts") | |
| def start_minecraft(): | |
| subprocess.Popen(["/bin/bash", "start.sh"]) | |
| def status(): | |
| return "🟢 Server Running - Check logs for connection URL" | |
| if __name__ == "__main__": | |
| ngrok_auth = os.environ.get("NGROK_AUTH_TOKEN") | |
| if not ngrok_auth: | |
| logging.error("❌ Missing NGROK_AUTH_TOKEN environment variable") | |
| exit(1) | |
| try: | |
| url = start_ngrok(ngrok_auth) | |
| start_minecraft() | |
| logging.info(f"\n✅ Ready! Connect using: {url}") | |
| app.run(host='0.0.0.0', port=7860) | |
| except Exception as e: | |
| logging.error(f"❌ Critical startup failure: {str(e)}") | |
| exit(1) |