#!/usr/bin/env bash # Run backend (Go) and frontend (Vite) together; stop both on Ctrl+C set -e # Colors for output GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color # Get the directory where the script is located SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" # Function to cleanup background processes cleanup() { echo -e "\n${YELLOW}Shutting down services...${NC}" if [ ! -z "$BACKEND_PID" ]; then kill $BACKEND_PID 2>/dev/null || true wait $BACKEND_PID 2>/dev/null || true fi if [ ! -z "$FRONTEND_PID" ]; then kill $FRONTEND_PID 2>/dev/null || true wait $FRONTEND_PID 2>/dev/null || true fi echo -e "${GREEN}All services stopped.${NC}" exit 0 } # Trap Ctrl+C and cleanup trap cleanup SIGINT SIGTERM # Check if pnpm is available, otherwise use npm if command -v pnpm &> /dev/null; then PACKAGE_MANAGER="pnpm" elif command -v npm &> /dev/null; then PACKAGE_MANAGER="npm" else echo -e "${RED}Error: Neither pnpm nor npm is installed.${NC}" exit 1 fi # Check if Go is installed if ! command -v go &> /dev/null; then echo -e "${RED}Error: Go is not installed.${NC}" exit 1 fi if ! go build -o /dev/null .;then echo -e "${RED}Error: backend can't compile.${NC}" exit 1 fi echo -e "${GREEN}Starting backend (Go)...${NC}" go run main.go & BACKEND_PID=$! # Wait a moment for backend to start sleep 2 echo -e "${GREEN}Starting frontend (Vite)...${NC}" cd frontend $PACKAGE_MANAGER run dev & FRONTEND_PID=$! cd .. echo -e "${GREEN}Both services are running!${NC}" echo -e "${YELLOW}Backend PID: $BACKEND_PID${NC}" echo -e "${YELLOW}Frontend PID: $FRONTEND_PID${NC}" echo -e "${YELLOW}Press Ctrl+C to stop both services${NC}" # Wait for both processes wait $BACKEND_PID $FRONTEND_PID