Files
vibemake-pcb-studio/backend/launch.py
T
2026-11-06 17:38:44 +08:00

68 lines
2.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 providers.config import current_binding
from providers.factory import check_configuration
check_configuration(current_binding())
from tools.fetch_router import main as fetch_router
fetch_router()
engine = os.getenv('AI_PCB_EDA_BACKEND', 'easyeda')
if engine == 'easyeda':
pack = Path(os.getenv('AI_PCB_PACK_DIR', str(root / 'easyeda_backend'))).resolve()
sys.path.insert(0, str(pack))
from eda_runtime.adapter import find_eda, cli_call
executable = find_eda(os.getenv('AI_PCB_EDA_EXECUTABLE'))
health = cli_call(executable, ['doctor'])
if health.get('connected') is not True:
raise SystemExit('Connect the EasyEDA Pro CLI bridge before starting the backend')
if not shutil.which('java'):
raise SystemExit('Install Java 17+ for local routing')
if not shutil.which(os.getenv('AI_PCB_BLENDER', 'blender')):
raise SystemExit('Install Blender for component 3D previews')
elif engine == 'kicad':
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')
else:
raise SystemExit('AI_PCB_EDA_BACKEND must be easyeda or kicad')
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)