74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
"""MCM TUI Web-Server via textual-serve.
|
||
|
||
Startet einen Web-Server, der die TUI pro Browser-Verbindung als Subprocess ausführt.
|
||
Voraussetzung: MCM API-Server muss bereits laufen (python main_api_only.py).
|
||
|
||
Verwendung:
|
||
# Lokal:
|
||
python serve_tui.py
|
||
|
||
# Raspberry Pi / entfernter Rechner:
|
||
python serve_tui.py --host 0.0.0.0 --public-host 192.168.1.100 --port 8001
|
||
# Browser: http://192.168.1.100:8001
|
||
"""
|
||
|
||
import asyncio
|
||
import socket
|
||
import sys
|
||
|
||
from textual_serve.server import Server
|
||
|
||
from config import settings
|
||
|
||
|
||
def _local_ip() -> str:
|
||
"""Ermittelt die lokale IP-Adresse (für den public_url-Fallback)."""
|
||
try:
|
||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||
s.connect(("8.8.8.8", 80))
|
||
return s.getsockname()[0]
|
||
except Exception:
|
||
return "localhost"
|
||
|
||
|
||
def main() -> None:
|
||
import argparse
|
||
|
||
parser = argparse.ArgumentParser(description="MCM TUI Web-Server")
|
||
parser.add_argument("--host", default="0.0.0.0", help="Bind-Adresse (default: 0.0.0.0)")
|
||
parser.add_argument(
|
||
"--port", type=int, default=settings.web_port,
|
||
help=f"Port (default: {settings.web_port})"
|
||
)
|
||
parser.add_argument(
|
||
"--public-host", default=None,
|
||
help="Öffentlicher Hostname/IP für WebSocket-URL (default: automatisch ermittelt)"
|
||
)
|
||
args = parser.parse_args()
|
||
|
||
# Öffentliche URL für die WebSocket-Verbindung im Browser
|
||
public_host = args.public_host or (
|
||
"localhost" if args.host in ("localhost", "127.0.0.1") else _local_ip()
|
||
)
|
||
public_url = f"http://{public_host}:{args.port}"
|
||
|
||
python = sys.executable
|
||
command = f"{python} tui_standalone.py"
|
||
|
||
print(f"MCM TUI Web-Server lauscht auf {args.host}:{args.port}")
|
||
print(f"Browser-URL: {public_url}")
|
||
print("Ctrl+C zum Beenden.")
|
||
|
||
server = Server(
|
||
command=command,
|
||
host=args.host,
|
||
port=args.port,
|
||
title="MCM – MultiCustomerMessenger",
|
||
public_url=public_url,
|
||
)
|
||
asyncio.run(server.serve())
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|