Spaces:
Sleeping
Sleeping
Update panel.py
Browse files
panel.py
CHANGED
|
@@ -1,11 +1,18 @@
|
|
| 1 |
import os, asyncio, collections, shutil, urllib.request, json, time
|
|
|
|
| 2 |
from fastapi import FastAPI, WebSocket, Form, UploadFile, File, HTTPException
|
| 3 |
from fastapi.responses import HTMLResponse, Response
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
import uvicorn
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# --- CONFIG ---
|
| 8 |
-
app = FastAPI()
|
| 9 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
|
| 10 |
BASE_DIR = os.environ.get("SERVER_DIR", os.path.abspath("/app"))
|
| 11 |
PLUGINS_DIR = os.path.join(BASE_DIR, "plugins")
|
|
@@ -155,7 +162,7 @@ const ws = new WebSocket((location.protocol === "https:" ? "wss://" : "ws://") +
|
|
| 155 |
ws.onmessage = e => {
|
| 156 |
const l = document.createElement("div"); l.className = "log-line";
|
| 157 |
// Basic ANSI color parsing
|
| 158 |
-
l.innerHTML = e.data.replace(/</g, "<").replace(/\x1b\[31m/g, '<span class="text-red-400">').replace(/\x1b\[32m/g, '<span class="text-green-400">').replace(/\x1b\[33m/g, '<span class="text-yellow-400">').replace(/\x1b\[36m/g, '<span class="text-cyan-400">').replace(/\x1b\[0m/g, '</span>');
|
| 159 |
logs.appendChild(l);
|
| 160 |
if(logs.children.length > 300) logs.removeChild(logs.firstChild);
|
| 161 |
if(logs.scrollHeight - logs.scrollTop < logs.clientHeight + 50) logs.scrollTop = logs.scrollHeight;
|
|
@@ -393,11 +400,6 @@ async def boot_mc():
|
|
| 393 |
)
|
| 394 |
asyncio.create_task(stream_output(mc_process.stdout))
|
| 395 |
|
| 396 |
-
@app.on_event("startup")
|
| 397 |
-
async def start():
|
| 398 |
-
os.makedirs(PLUGINS_DIR, exist_ok=True)
|
| 399 |
-
asyncio.create_task(boot_mc())
|
| 400 |
-
|
| 401 |
@app.get("/")
|
| 402 |
def index(): return HTMLResponse(HTML_CONTENT)
|
| 403 |
|
|
|
|
| 1 |
import os, asyncio, collections, shutil, urllib.request, json, time
|
| 2 |
+
from contextlib import asynccontextmanager
|
| 3 |
from fastapi import FastAPI, WebSocket, Form, UploadFile, File, HTTPException
|
| 4 |
from fastapi.responses import HTMLResponse, Response
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
import uvicorn
|
| 7 |
|
| 8 |
+
@asynccontextmanager
|
| 9 |
+
async def lifespan(app: FastAPI):
|
| 10 |
+
os.makedirs(PLUGINS_DIR, exist_ok=True)
|
| 11 |
+
asyncio.create_task(boot_mc())
|
| 12 |
+
yield
|
| 13 |
+
|
| 14 |
# --- CONFIG ---
|
| 15 |
+
app = FastAPI(lifespan=lifespan)
|
| 16 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
|
| 17 |
BASE_DIR = os.environ.get("SERVER_DIR", os.path.abspath("/app"))
|
| 18 |
PLUGINS_DIR = os.path.join(BASE_DIR, "plugins")
|
|
|
|
| 162 |
ws.onmessage = e => {
|
| 163 |
const l = document.createElement("div"); l.className = "log-line";
|
| 164 |
// Basic ANSI color parsing
|
| 165 |
+
l.innerHTML = e.data.replace(/</g, "<").replace(/\\x1b\\[31m/g, '<span class="text-red-400">').replace(/\\x1b\\[32m/g, '<span class="text-green-400">').replace(/\\x1b\\[33m/g, '<span class="text-yellow-400">').replace(/\\x1b\\[36m/g, '<span class="text-cyan-400">').replace(/\\x1b\\[0m/g, '</span>');
|
| 166 |
logs.appendChild(l);
|
| 167 |
if(logs.children.length > 300) logs.removeChild(logs.firstChild);
|
| 168 |
if(logs.scrollHeight - logs.scrollTop < logs.clientHeight + 50) logs.scrollTop = logs.scrollHeight;
|
|
|
|
| 400 |
)
|
| 401 |
asyncio.create_task(stream_output(mc_process.stdout))
|
| 402 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
@app.get("/")
|
| 404 |
def index(): return HTMLResponse(HTML_CONTENT)
|
| 405 |
|