Files
rfcp/backend/run_server.py

69 lines
2.0 KiB
Python

"""Entry point for PyInstaller bundle"""
import os
import sys
# Force unbuffered stdout/stderr — critical for piped output (Electron, bat files)
os.environ['PYTHONUNBUFFERED'] = '1'
if hasattr(sys.stdout, 'reconfigure'):
try:
sys.stdout.reconfigure(line_buffering=True)
except Exception:
pass
if hasattr(sys.stderr, 'reconfigure'):
try:
sys.stderr.reconfigure(line_buffering=True)
except Exception:
pass
print("[RFCP] run_server.py starting...", flush=True)
# Set base path for PyInstaller
if getattr(sys, 'frozen', False):
base_dir = os.path.dirname(sys.executable)
os.chdir(base_dir)
print(f"[RFCP] Frozen mode, base dir: {base_dir}", flush=True)
# Fix uvicorn TTY detection — redirect None streams to a log file
log_path = os.path.join(base_dir, 'rfcp-server.log')
log_file = open(log_path, 'w')
if sys.stdout is None:
sys.stdout = log_file
if sys.stderr is None:
sys.stderr = log_file
if sys.stdin is None:
sys.stdin = open(os.devnull, 'r')
print(f"[RFCP] Log file: {log_path}", flush=True)
print("[RFCP] Importing uvicorn...", flush=True)
import uvicorn
print("[RFCP] Importing app.main...", flush=True)
try:
from app.main import app
print("[RFCP] App imported successfully", flush=True)
except Exception as e:
print(f"[RFCP] FATAL: Failed to import app: {e}", flush=True)
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == '__main__':
host = os.environ.get('RFCP_HOST', '127.0.0.1')
port = int(os.environ.get('RFCP_PORT', '8888'))
print(f"[RFCP] Starting uvicorn on {host}:{port}", flush=True)
try:
uvicorn.run(
app,
host=host,
port=port,
log_level='warning',
access_log=False,
)
except Exception as e:
print(f"[RFCP] FATAL: uvicorn.run failed: {e}", flush=True)
import traceback
traceback.print_exc()
sys.exit(1)