From 2f7cd70d3657d093f5bcf03e599cffe8a92c7ad7 Mon Sep 17 00:00:00 2001 From: xucheng Date: Thu, 30 Oct 2025 15:03:03 +0800 Subject: [PATCH] feat: add run script to start frontend and backend with pm2 --- scripts/run.sh | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 scripts/run.sh diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100755 index 0000000..ea59065 --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,73 @@ +#!/bin/bash +# Exit immediately if a command exits with a non-zero status. +set -e + +# --- Backend Setup --- +echo "Setting up backend..." +cd backend + +# Create a virtual environment if it doesn't exist +if [ ! -d ".venv" ]; then + echo "Creating virtual environment..." + python3 -m venv .venv +fi + +# Activate virtual environment and install dependencies +source .venv/bin/activate +pip install -r requirements.txt +deactivate + +echo "Backend setup complete." +cd .. + +# --- Frontend Setup --- +echo "Setting up frontend..." +cd frontend +npm install +echo "Frontend setup complete." +cd .. + +# --- PM2 Execution --- +echo "Starting application with pm2..." + +# Check if pm2 is installed +if ! command -v pm2 &> /dev/null +then + echo "pm2 could not be found. Please install it with 'npm install -g pm2'" + exit 1 +fi + +# Create a pm2 config file if it doesn't exist +if [ ! -f "pm2.config.js" ]; then + echo "Creating pm2.config.js..." + cat > pm2.config.js << EOL +module.exports = { + apps : [{ + name: "frontend", + cwd: "./frontend", + script: "npm", + args: "start", + env: { + "PORT": 3000 + } + }, { + name: "backend", + cwd: "./backend", + script: "./.venv/bin/uvicorn", + args: "app.main:app --host 0.0.0.0 --port 8000", + interpreter: "none", + env: { + "PYTHONPATH": "." + } + }] +}; +EOL +fi + +# Start processes with pm2 +pm2 start pm2.config.js + +echo "Application started with pm2." +echo "Use 'pm2 list' to see the status of the applications." +echo "Use 'pm2 logs' to see the logs." +echo "Use 'pm2 stop all' to stop the applications."