This repository was archived by the owner on Apr 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart_services.py
More file actions
86 lines (72 loc) · 2.44 KB
/
start_services.py
File metadata and controls
86 lines (72 loc) · 2.44 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
"""
Start both the FastAPI backend and Next.js frontend for local development.
Usage:
python start_services.py
Requirements:
- Backend Python deps installed (`pip install -r backend/requirements.txt`)
- Frontend deps installed (`npm install`)
"""
import os
import signal
import subprocess
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parent
# Use npm.cmd on Windows so the frontend script launches correctly.
if os.name == "nt":
FRONTEND_CMD = ["npm.cmd", "run", "dev", "--", "--port", "3000"]
else:
FRONTEND_CMD = ["npm", "run", "dev", "--", "--port", "3000"]
def start_process(cmd: list[str], cwd: Path, name: str) -> subprocess.Popen:
env = os.environ.copy()
if name == "frontend" and "NEXT_PUBLIC_API_URL" not in env:
env["NEXT_PUBLIC_API_URL"] = "http://localhost:8000/api"
print(f"[runner] starting {name}: {' '.join(cmd)} (cwd={cwd})", flush=True)
return subprocess.Popen(
cmd,
cwd=str(cwd),
env=env,
stdout=sys.stdout,
stderr=sys.stderr,
)
def main() -> int:
processes: list[tuple[str, subprocess.Popen]] = []
try:
backend_cmd = [
sys.executable,
"-m",
"uvicorn",
"backend.main:app",
"--reload",
"--host",
"0.0.0.0",
"--port",
"8000",
]
frontend_cmd = FRONTEND_CMD
processes.append(("backend", start_process(backend_cmd, ROOT, "backend")))
processes.append(("frontend", start_process(frontend_cmd, ROOT, "frontend")))
print("\n[runner] services running. Press Ctrl+C to stop.")
while True:
time.sleep(1)
for name, proc in processes:
ret = proc.poll()
if ret is not None:
print(f"[runner] {name} exited with code {ret}", flush=True)
return ret or 0
except KeyboardInterrupt:
print("\n[runner] received interrupt, stopping...", flush=True)
finally:
for name, proc in processes:
if proc.poll() is None:
print(f"[runner] terminating {name}...", flush=True)
proc.terminate()
time.sleep(2)
for name, proc in processes:
if proc.poll() is None:
print(f"[runner] killing {name}...", flush=True)
proc.kill()
return 0
if __name__ == "__main__":
raise SystemExit(main())