Skip to content

Commit 6100eb2

Browse files
author
James N.
committed
update bash script
1 parent b2a6ea2 commit 6100eb2

File tree

1 file changed

+73
-38
lines changed

1 file changed

+73
-38
lines changed

voice_agent/app/start.sh

Lines changed: 73 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +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; }
413

5-
# --------------------------- Start the Backend Service ---------------------------
6-
echo ""
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
23+
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; }
44+
echo
45+
echo "Installing backend Python packages…"
1946

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=$!
47+
venv_py=".venv/bin/python"
48+
[[ -x "$venv_py" ]] || { popd >/dev/null; die "Could not find the virtual-env Python executable."; }
2549

26-
cd ..
50+
"$venv_py" -m pip install -r requirements.txt || { popd >/dev/null; die "Backend dependency installation failed."; }
2751

28-
# --------------------------- Start the Frontend Service ---------------------------
29-
echo ""
30-
echo "Installing frontend npm packages..."
52+
echo
53+
echo "Starting backend service (listening on port 8765)…"
3154

32-
cd frontend || { echo "frontend folder not found."; exit 1; }
33-
npm install || { echo "Frontend npm installation failed."; exit 1; }
55+
# Launch FastAPI (or whatever app.py holds) in the background
56+
"$venv_py" app.py --reload &
57+
backend_pid=$!
3458

35-
# Note:
36-
# • The .env file for the frontend should be placed in the frontend directory (i.e., ./frontend/.env)
37-
# • This file can contain environment variables such as BACKEND_WS_URL that Vite will load.
38-
#
39-
# Example .env file:
40-
VITE_BACKEND_WS_URL="ws://localhost:8765"
59+
popd > /dev/null
4160

42-
echo ""
43-
echo "Starting frontend dev server..."
44-
npm run dev &
45-
FRONTEND_PID=$!
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."; }
4671

47-
cd ..
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=$!
4878

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

52-
echo ""
81+
###############################################################################
82+
# Wait forever until user presses Ctrl-C
83+
###############################################################################
84+
echo
5385
echo "Both backend and frontend services are running."
5486
echo "Press Ctrl+C to stop."
5587

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

0 commit comments

Comments
 (0)