gohorsejobs/start.sh
Tiago Yamamoto d2266eb0f9 fix(start.sh): option 6 now runs full pipeline (reset → migrate → seed)
- Step 1: Drop all tables
- Step 2: Run migrations
- Step 3: Seed data
- Fixed misleading 'seeded' message
2025-12-24 10:53:19 -03:00

220 lines
7.2 KiB
Bash
Executable file

#!/bin/bash
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Function to kill background processes on exit
cleanup() {
echo -e "\n${YELLOW}🛑 Stopping services...${NC}"
kill $(jobs -p) 2>/dev/null
exit
}
trap cleanup SIGINT SIGTERM
# Header
clear
echo -e "${CYAN}"
echo "╔══════════════════════════════════════════════╗"
echo "║ 🐴 GoHorse Jobs - Dev Server ║"
echo "╚══════════════════════════════════════════════╝"
echo -e "${NC}"
# Interactive menu
echo -e "${GREEN}Select an option:${NC}\n"
echo -e " ${CYAN}── Services ──${NC}"
echo -e " ${YELLOW}1)${NC} 🚀 Start (Frontend + Backend)"
echo -e " ${YELLOW}2)${NC} 🌱 Start with Seed (Reset DB + Seed + Start)"
echo -e " ${YELLOW}3)${NC} 📦 Start All (Frontend + Backend + Backoffice)"
echo -e ""
echo -e " ${CYAN}── Database ──${NC}"
echo -e " ${YELLOW}4)${NC} 🗄️ Run Migrations"
echo -e " ${YELLOW}5)${NC} 🌿 Seed Only (append data)"
echo -e " ${YELLOW}6)${NC} 🔄 Seed Reset (drop all + seed fresh)"
echo -e ""
echo -e " ${CYAN}── Testing ──${NC}"
echo -e " ${YELLOW}7)${NC} 🧪 Run Tests (Backend E2E)"
echo -e " ${YELLOW}0)${NC} ❌ Exit"
echo ""
read -p "Enter option [0-7]: " choice
case $choice in
1)
echo -e "\n${GREEN}🚀 Starting Development Environment...${NC}\n"
# Backend
echo -e "${BLUE}🔹 Checking Backend...${NC}"
cd backend && go mod tidy
if command -v swag &> /dev/null; then
echo -e "${BLUE}🔹 Generating Swagger Docs...${NC}"
swag init -g cmd/api/main.go --parseDependency --parseInternal 2>/dev/null
fi
cd ..
# Frontend deps
if [ ! -d "frontend/node_modules" ]; then
echo -e "${BLUE}🔹 Installing Frontend Dependencies...${NC}"
cd frontend && npm install && cd ..
fi
# Start services
echo -e "${BLUE}🔹 Starting Backend on port 8521...${NC}"
(cd backend && go run cmd/api/main.go) &
BACKEND_PID=$!
sleep 2
echo -e "${BLUE}🔹 Starting Frontend on port 8963...${NC}"
(cd frontend && npm run dev -- -p 8963) &
FRONTEND_PID=$!
echo -e "\n${GREEN}✅ Services running:${NC}"
echo -e " ${CYAN}Backend:${NC} http://localhost:8521"
echo -e " ${CYAN}Frontend:${NC} http://localhost:8963"
echo -e " ${CYAN}Swagger:${NC} http://localhost:8521/swagger/index.html"
echo ""
wait $BACKEND_PID $FRONTEND_PID
;;
2)
echo -e "\n${GREEN}🌱 Starting with Database Reset & Seed...${NC}\n"
# Backend prep
echo -e "${BLUE}🔹 Checking Backend...${NC}"
cd backend && go mod tidy
if command -v swag &> /dev/null; then
swag init -g cmd/api/main.go --parseDependency --parseInternal 2>/dev/null
fi
cd ..
# Seeder deps
cd seeder-api
if [ ! -d "node_modules" ]; then
echo -e "${BLUE}🔹 Installing Seeder Dependencies...${NC}"
npm install
fi
echo -e "${YELLOW}🔹 Resetting Database...${NC}"
npm run seed:reset
cd ..
# Start backend
echo -e "${BLUE}🔹 Starting Backend...${NC}"
(cd backend && go run cmd/api/main.go) &
BACKEND_PID=$!
echo -e "${YELLOW}⏳ Waiting for Backend...${NC}"
sleep 5
# Seed
echo -e "${BLUE}🔹 Seeding Database (30 companies, 990 jobs)...${NC}"
cd seeder-api && npm run seed && cd ..
# Frontend
if [ ! -d "frontend/node_modules" ]; then
cd frontend && npm install && cd ..
fi
echo -e "${BLUE}🔹 Starting Frontend...${NC}"
(cd frontend && npm run dev -- -p 8963) &
FRONTEND_PID=$!
echo -e "\n${GREEN}✅ Services running with fresh data!${NC}"
echo -e " ${CYAN}Backend:${NC} http://localhost:8521"
echo -e " ${CYAN}Frontend:${NC} http://localhost:8963"
echo ""
wait $BACKEND_PID $FRONTEND_PID
;;
3)
echo -e "\n${GREEN}📦 Starting All Services (Including Backoffice)...${NC}\n"
# Backend
cd backend && go mod tidy && cd ..
# Dependencies
[ ! -d "frontend/node_modules" ] && (cd frontend && npm install && cd ..)
[ ! -d "backoffice/node_modules" ] && (cd backoffice && npm install && cd ..)
echo -e "${BLUE}🔹 Starting Backend on port 8521...${NC}"
(cd backend && go run cmd/api/main.go) &
sleep 2
echo -e "${BLUE}🔹 Starting Frontend on port 8963...${NC}"
(cd frontend && npm run dev -- -p 8963) &
echo -e "${BLUE}🔹 Starting Backoffice on port 3001...${NC}"
(cd backoffice && npm run start:dev) &
echo -e "\n${GREEN}✅ All services running:${NC}"
echo -e " ${CYAN}Backend:${NC} http://localhost:8521"
echo -e " ${CYAN}Frontend:${NC} http://localhost:8963"
echo -e " ${CYAN}Backoffice:${NC} http://localhost:3001"
echo -e " ${CYAN}Swagger:${NC} http://localhost:3001/api/docs"
echo ""
wait
;;
4)
echo -e "\n${GREEN}🗄️ Running Migrations...${NC}\n"
cd seeder-api
[ ! -d "node_modules" ] && npm install
npm run migrate
;;
5)
echo -e "\n${GREEN}🌿 Seeding Database Only...${NC}\n"
cd seeder-api
[ ! -d "node_modules" ] && npm install
npm run seed
echo -e "\n${GREEN}✅ Seeding completed!${NC}"
;;
6)
echo -e "\n${GREEN}🔄 Resetting Database & Seeding Fresh...${NC}\n"
cd seeder-api
[ ! -d "node_modules" ] && npm install
echo -e "${YELLOW}⚠️ This will DROP all tables and recreate!${NC}"
read -p "Are you sure? [y/N]: " confirm
if [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]]; then
echo -e "\n${BLUE}🔹 Step 1/3: Dropping all tables...${NC}"
npm run seed:reset
echo -e "\n${BLUE}🔹 Step 2/3: Running migrations...${NC}"
npm run migrate
echo -e "\n${BLUE}🔹 Step 3/3: Seeding data...${NC}"
npm run seed
echo -e "\n${GREEN}✅ Database fully reset and seeded!${NC}"
else
echo -e "${YELLOW}Cancelled.${NC}"
fi
;;
7)
echo -e "\n${GREEN}🧪 Running Backend E2E Tests...${NC}\n"
cd backend && go test -tags=e2e -v ./tests/e2e/... 2>&1
echo -e "\n${GREEN}✅ Tests completed!${NC}"
;;
0)
echo -e "${YELLOW}Bye! 👋${NC}"
exit 0
;;
*)
echo -e "${RED}Invalid option. Exiting.${NC}"
exit 1
;;
esac