-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard_api.py
More file actions
87 lines (75 loc) · 2.66 KB
/
dashboard_api.py
File metadata and controls
87 lines (75 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
import json
import os
import subprocess
import signal
import sys
app = FastAPI(title="StreamGuard Dashboard API")
# Allow CORS for development
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Global process handler
MONITOR_PROCESS = None
SCRIPT_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sesgoruntu", "sesgoruntu.py")
STATUS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "status.json")
@app.get("/api/status")
def get_status():
"""Reads the shared status.json file"""
try:
# Check if process is actually running
is_running = MONITOR_PROCESS is not None and MONITOR_PROCESS.poll() is None
if os.path.exists(STATUS_FILE):
with open(STATUS_FILE, "r") as f:
data = json.load(f)
data["system"] = {"running": is_running}
return data
else:
return {
"stream_guard_ai": {"status": "OFFLINE", "message": "No status file found"},
"system": {"running": is_running}
}
except Exception as e:
return {"error": str(e)}
@app.post("/api/start")
def start_monitor():
"""Starts the monitoring script"""
global MONITOR_PROCESS
if MONITOR_PROCESS is not None and MONITOR_PROCESS.poll() is None:
return {"message": "Already running"}
try:
# Run python script in background
if os.name == 'nt':
creationflags = subprocess.CREATE_NEW_CONSOLE
else:
creationflags = 0
MONITOR_PROCESS = subprocess.Popen(
[sys.executable, SCRIPT_PATH],
creationflags=creationflags
)
return {"message": "StreamGuard Started", "pid": MONITOR_PROCESS.pid}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/stop")
def stop_monitor():
"""Stops the monitoring script"""
global MONITOR_PROCESS
if MONITOR_PROCESS is None:
return {"message": "Not running"}
try:
MONITOR_PROCESS.terminate()
MONITOR_PROCESS = None
return {"message": "StreamGuard Stopped"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Serve static files (HTML)
app.mount("/", StaticFiles(directory="web", html=True), name="static")
if __name__ == "__main__":
import uvicorn
print("🚀 Dashboard running at http://localhost:8000")
uvicorn.run(app, host="0.0.0.0", port=8000)