Skip to content

Commit 28ad6d7

Browse files
authored
Merge pull request #88 from microsoft/james-dev
update bash script
2 parents 283e3aa + 916a511 commit 28ad6d7

File tree

1 file changed

+79
-49
lines changed

1 file changed

+79
-49
lines changed

voice_agent/app/start.sh

Lines changed: 79 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,91 @@
1-
#!/bin/sh
2-
# This script sets up and starts both the backend and the frontend for local development.
3-
# The backend is run with a Python virtual environment and the frontend uses Vite’s dev server.
1+
#!/usr/bin/env bash
2+
#
3+
# dev.sh – start backend (FastAPI) and frontend (Vite) in watch mode
4+
# while taking care of Python venv / npm install, plus
5+
# graceful shutdown on Ctrl-C.
6+
7+
set -euo pipefail
8+
9+
###############################################################################
10+
# Helper functions
11+
###############################################################################
12+
die () { echo "ERROR: $*" >&2; exit 1; }
13+
14+
cleanup () {
15+
echo # blank line
16+
echo "Stopping services…"
17+
[[ -n "${backend_pid:-}" ]] && kill "$backend_pid" 2>/dev/null || true
18+
[[ -n "${frontend_pid:-}" ]] && kill "$frontend_pid" 2>/dev/null || true
19+
exit 0
20+
}
21+
22+
trap cleanup INT TERM
423

5-
# --------------------------- Start the Backend Service ---------------------------
6-
echo ""
24+
###############################################################################
25+
# Backend
26+
###############################################################################
27+
echo
728
echo "Setting up the Python virtual environment for the backend..."
829

9-
cd backend || { echo "backend folder not found."; exit 1; }
30+
[[ -d backend ]] || die "backend folder not found."
31+
pushd backend > /dev/null
1032

11-
# Create virtual environment if it doesn't exist
12-
if [ ! -d ".venv" ]; then
13-
python3 -m venv .venv
33+
# ------------------------------------------------------------------
34+
# Create / upgrade the virtual-env
35+
# ------------------------------------------------------------------
36+
if [[ ! -d .venv ]]; then
37+
# --upgrade-deps guarantees fresh pip / setuptools / wheel
38+
python -m venv --upgrade-deps .venv
39+
else
40+
# env exists – still make sure the essentials are up to date
41+
.venv/bin/python -m pip install --upgrade pip setuptools wheel packaging
1442
fi
1543

16-
echo ""
17-
echo "Installing backend Python packages..."
18-
./.venv/bin/python -m pip install -r requirements.txt || { echo "Backend dependency installation failed."; exit 1; }
19-
20-
echo ""
21-
echo "Starting backend service (listening on port 8765)..."
22-
# Use --reload for auto-restarting during development
23-
./.venv/bin/python app.py --reload &
24-
BACKEND_PID=$!
25-
26-
cd ..
27-
28-
# --------------------------- Start the Frontend Service ---------------------------
29-
echo ""
30-
echo "Installing frontend npm packages..."
31-
32-
cd frontend || { echo "frontend folder not found."; exit 1; }
33-
npm install || { echo "Frontend npm installation failed."; exit 1; }
34-
35-
echo ""
36-
echo "Building frontend application..."
37-
npm run build || { echo "Frontend build failed."; exit 1; }
38-
# Note:
39-
# • The .env file for the frontend should be placed in the frontend directory (i.e., ./frontend/.env)
40-
# • This file can contain environment variables such as BACKEND_WS_URL that Vite will load.
41-
#
42-
# Example .env file:
43-
export VITE_BACKEND_WS_URL="ws://localhost:8765"
44+
echo
45+
echo "Installing backend Python packages…"
46+
47+
venv_py=".venv/bin/python"
48+
[[ -x "$venv_py" ]] || { popd >/dev/null; die "Could not find the virtual-env Python executable."; }
49+
50+
"$venv_py" -m pip install -r requirements.txt || { popd >/dev/null; die "Backend dependency installation failed."; }
4451

45-
echo ""
46-
echo "Starting frontend dev server..."
47-
node server.cjs &
48-
FRONTEND_PID=$!
49-
50-
echo "Frontend proxy started at: http://localhost:3000"
51-
52-
cd ..
52+
echo
53+
echo "Starting backend service (listening on port 8765)…"
54+
55+
# Launch FastAPI (or whatever app.py holds) in the background
56+
"$venv_py" app.py --reload &
57+
backend_pid=$!
58+
59+
popd > /dev/null
60+
61+
###############################################################################
62+
# Frontend
63+
###############################################################################
64+
echo
65+
echo "Installing frontend npm packages…"
66+
67+
[[ -d frontend ]] || { kill "$backend_pid"; die "frontend folder not found."; }
68+
pushd frontend > /dev/null
69+
70+
npm install || { popd >/dev/null; kill "$backend_pid"; die "Frontend npm installation failed."; }
71+
72+
echo
73+
echo "Starting frontend dev server…"
74+
75+
export VITE_BACKEND_WS_URL="ws://localhost:8765"
76+
npm run dev &
77+
frontend_pid=$!
5378

54-
# --------------------------- Clean-up on Exit ---------------------------
55-
trap "echo 'Stopping services...'; kill $BACKEND_PID; kill $FRONTEND_PID; exit 0" INT TERM
79+
popd > /dev/null
5680

57-
echo ""
81+
###############################################################################
82+
# Wait forever until user presses Ctrl-C
83+
###############################################################################
84+
echo
5885
echo "Both backend and frontend services are running."
5986
echo "Press Ctrl+C to stop."
6087

61-
wait
88+
# Keep script alive
89+
while true; do
90+
sleep 1
91+
done

0 commit comments

Comments
 (0)