misukisu commited on
Commit
4941a65
·
verified ·
1 Parent(s): cb8b795

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -42
app.py CHANGED
@@ -1,65 +1,91 @@
1
  import http.server
2
  import socketserver
 
 
3
  import os
4
- import re
5
 
6
  PORT = 7860
7
- LOG_FILE = "/bedrock/server_combined.log"
8
 
9
- HTML_TEMPLATE = """<!DOCTYPE html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  <html>
11
  <head>
12
- <title>Bedrock Server Console</title>
13
  <meta http-equiv="refresh" content="3">
14
  <style>
15
- body { font-family: monospace; background: #121212; color: #e0e0e0; padding: 20px; }
16
- .box { background: #1e1e1e; padding: 15px; border-radius: 8px; border: 1px solid #333; margin-bottom: 20px; }
17
- .claim { background: #2e4a2e; color: #a3e635; padding: 10px; border-radius: 5px; font-size: 16px; font-weight: bold; }
18
- pre { white-space: pre-wrap; word-wrap: break-word; font-size: 13px; color: #d4d4d4; }
19
- a { color: #38bdf8; text-decoration: underline; }
20
  </style>
21
  </head>
22
  <body>
23
- <h2>🧱 Hugging Face Bedrock Server</h2>
24
- <div class="box">
25
- <h3>🔗 playit.gg Claim URL:</h3>
26
- <p class="claim">{claim_url}</p>
27
  </div>
28
- <div class="box">
29
  <h3>📜 Live Console Output:</h3>
30
- <pre>{logs}</pre>
31
  </div>
32
  </body>
33
- </html>
34
- """
35
-
36
- class Handler(http.server.SimpleHTTPRequestHandler):
37
- def do_GET(self):
38
- logs = "Starting Bedrock..."
39
- claim_url = "Waiting for playit.gg claim link..."
40
-
41
- if os.path.exists(LOG_FILE):
42
- try:
43
- with open(LOG_FILE, "r", errors="ignore") as f:
44
- all_lines = f.readlines()
45
- logs = "".join(all_lines[-100:])
46
-
47
- match = re.search(r'(https://playit\.gg/claim/[a-zA-Z0-9]+)', logs)
48
- if match:
49
- url = match.group(1)
50
- claim_url = f'<a href="{url}" target="_blank">{url}</a> (Click to link to your playit account)'
51
- elif "playit" in logs.lower() and "ready" in logs.lower():
52
- claim_url = "Tunnel active! Check your playit.gg dashboard for your public Bedrock IP & Port."
53
- except Exception as e:
54
- logs = f"Error reading logs: {e}"
55
-
56
- html = HTML_TEMPLATE.format(claim_url=claim_url, logs=logs)
57
-
58
  self.send_response(200)
59
  self.send_header("Content-type", "text/html; charset=utf-8")
60
  self.end_headers()
61
  self.wfile.write(html.encode("utf-8"))
62
 
63
- with socketserver.TCPServer(("", PORT), Handler) as httpd:
64
- print(f"Web interface running on port {PORT}")
65
  httpd.serve_forever()
 
1
  import http.server
2
  import socketserver
3
+ import subprocess
4
+ import threading
5
  import os
6
+ import time
7
 
8
  PORT = 7860
9
+ logs = []
10
 
11
+ def stream_process(proc, prefix):
12
+ for line in iter(proc.stdout.readline, ""):
13
+ if line:
14
+ formatted = f"[{prefix}] {line.strip()}"
15
+ print(formatted, flush=True)
16
+ logs.append(formatted)
17
+ if len(logs) > 200:
18
+ logs.pop(0)
19
+
20
+ def start_services():
21
+ # 1. Start playit tunnel
22
+ playit_proc = subprocess.Popen(
23
+ ["playit"],
24
+ stdout=subprocess.PIPE,
25
+ stderr=subprocess.STDOUT,
26
+ text=True,
27
+ bufsize=1
28
+ )
29
+ threading.Thread(target=stream_process, args=(playit_proc, "PLAYIT"), daemon=True).start()
30
+
31
+ # 2. Start Bedrock server
32
+ env = os.environ.copy()
33
+ env["LD_LIBRARY_PATH"] = "/app"
34
+ bedrock_proc = subprocess.Popen(
35
+ ["./bedrock_server"],
36
+ cwd="/app",
37
+ env=env,
38
+ stdout=subprocess.PIPE,
39
+ stderr=subprocess.STDOUT,
40
+ text=True,
41
+ bufsize=1
42
+ )
43
+ threading.Thread(target=stream_process, args=(bedrock_proc, "BEDROCK"), daemon=True).start()
44
+
45
+ # Start background services
46
+ start_services()
47
+
48
+ class WebHandler(http.server.SimpleHTTPRequestHandler):
49
+ def do_GET(self):
50
+ log_text = "\n".join(logs)
51
+ claim_url = "Looking for claim URL in logs..."
52
+
53
+ for line in logs:
54
+ if "playit.gg/claim/" in line:
55
+ parts = line.split()
56
+ for p in parts:
57
+ if "playit.gg/claim/" in p:
58
+ claim_url = f'<a href="{p}" target="_blank" style="color:#4ade80;">{p}</a>'
59
+ break
60
+
61
+ html = f"""<!DOCTYPE html>
62
  <html>
63
  <head>
64
+ <title>Bedrock Server</title>
65
  <meta http-equiv="refresh" content="3">
66
  <style>
67
+ body {{ background: #0f172a; color: #f8fafc; font-family: monospace; padding: 20px; }}
68
+ .card {{ background: #1e293b; border-radius: 8px; padding: 15px; margin-bottom: 15px; }}
69
+ pre {{ white-space: pre-wrap; font-size: 12px; color: #cbd5e1; }}
 
 
70
  </style>
71
  </head>
72
  <body>
73
+ <h2>🎮 Minecraft Bedrock Server</h2>
74
+ <div class="card">
75
+ <h3>🔗 Playit Claim Link:</h3>
76
+ <p>{claim_url}</p>
77
  </div>
78
+ <div class="card">
79
  <h3>📜 Live Console Output:</h3>
80
+ <pre>{log_text if log_text else 'Starting processes...'}</pre>
81
  </div>
82
  </body>
83
+ </html>"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  self.send_response(200)
85
  self.send_header("Content-type", "text/html; charset=utf-8")
86
  self.end_headers()
87
  self.wfile.write(html.encode("utf-8"))
88
 
89
+ with socketserver.TCPServer(("", PORT), WebHandler) as httpd:
90
+ print(f"Server console online at port {PORT}", flush=True)
91
  httpd.serve_forever()