Files
vibemake-pcb-studio/backend/launch.py
T

51 lines
1.9 KiB
Python

"""Start the bundled API with private environment configuration and external data."""
import json
import os
from pathlib import Path
import re
import sys
import shutil
def load_env(path):
if not path.is_file():
return
for number, raw in enumerate(path.read_text().splitlines(), 1):
line = raw.strip()
if not line or line.startswith('#'):
continue
if '=' not in line:
raise SystemExit(f'Invalid backend environment line {number}')
key, value = line.split('=', 1)
key, value = key.strip(), value.strip()
if not re.fullmatch(r'[A-Z][A-Z0-9_]*', key):
raise SystemExit(f'Invalid backend environment variable on line {number}')
if value.startswith('"'):
value = json.loads(value)
elif value.startswith("'") and value.endswith("'"):
value = value[1:-1]
os.environ.setdefault(key, value)
if __name__ == '__main__':
root = Path(__file__).resolve().parent
load_env(root / '.env.local')
sys.path[:0] = [str(root), str(root / 'api')]
os.environ.setdefault('AI_PCB_MODE', 'real')
os.environ.setdefault('AI_PCB_API_DATA', str(Path.home() / '.local/share/vibemake-pcb'))
from kicad_backend.model import credentials
credentials()
from tools.fetch_router import main as fetch_router
fetch_router()
from kicad_backend.runtime import KiCadRuntime
from kicad_backend.routing import executable
health = KiCadRuntime().doctor()
if not health['available']:
raise SystemExit('KiCad backend prerequisites failed: ' + '; '.join(health['findings']))
executable()
if not shutil.which('Xvfb'):
raise SystemExit('Install Xvfb for unattended local routing')
import uvicorn
uvicorn.run('app.main:create_app', factory=True, host=os.getenv('AI_PCB_API_HOST', '127.0.0.1'),
port=int(os.getenv('AI_PCB_API_PORT', '8000')), timeout_graceful_shutdown=8)