Files
mtg-search/startup.sh
T
2026-04-28 02:31:41 +09:30

186 lines
5.5 KiB
Bash

#!/bin/bash
# Startup script for MTG Search Project
# This script helps set up and run the project
set -e
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${BLUE}=== MTG Search Project Startup ===${NC}"
# Check prerequisites
check_prerequisites() {
echo -e "\n${BLUE}Checking prerequisites...${NC}"
# Check Java
if ! command -v java &> /dev/null; then
echo -e "${RED}✗ Java not found. Please install Java 21+${NC}"
exit 1
fi
JAVA_VERSION=$(java -version 2>&1 | grep -oP 'version "\K[0-9.]+')
echo -e "${GREEN}✓ Java $JAVA_VERSION found${NC}"
# Check Node.js
if ! command -v node &> /dev/null; then
echo -e "${RED}✗ Node.js not found. Please install Node.js 20+${NC}"
exit 1
fi
NODE_VERSION=$(node -v)
echo -e "${GREEN}✓ Node.js $NODE_VERSION found${NC}"
# Check PostgreSQL
if ! command -v psql &> /dev/null; then
if ! command -v docker &> /dev/null; then
echo -e "${RED}✗ PostgreSQL and Docker not found. Please install one of them${NC}"
exit 1
fi
echo -e "${GREEN}✓ Docker found (will use for PostgreSQL)${NC}"
else
PG_VERSION=$(psql --version)
echo -e "${GREEN}$PG_VERSION found${NC}"
fi
}
# Setup database
setup_database() {
echo -e "\n${BLUE}Setting up database...${NC}"
# Check if PostgreSQL is running
if ! psql -U postgres -h localhost -d postgres -c "SELECT 1" &> /dev/null; then
echo -e "${BLUE}Starting PostgreSQL with Docker...${NC}"
if docker ps -a --format '{{.Names}}' | grep -q mtgsearch-postgres; then
docker start mtgsearch-postgres
echo -e "${GREEN}✓ PostgreSQL container started${NC}"
else
docker run --name mtgsearch-postgres \
-e POSTGRES_DB=mtgsearch \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-p 5432:5432 \
-d postgres:17-alpine
echo -e "${BLUE}Waiting for PostgreSQL to be ready...${NC}"
sleep 10
echo -e "${GREEN}✓ PostgreSQL container created and started${NC}"
fi
else
echo -e "${GREEN}✓ PostgreSQL is already running${NC}"
fi
# Create database if it doesn't exist
psql -U postgres -h localhost -c "CREATE DATABASE mtgsearch;" 2>/dev/null || true
echo -e "${GREEN}✓ Database ready${NC}"
}
# Build backend
build_backend() {
echo -e "\n${BLUE}Building backend...${NC}"
cd backend
../gradlew clean build -x test
cd ..
echo -e "${GREEN}✓ Backend built successfully${NC}"
}
# Install frontend dependencies
install_frontend() {
echo -e "\n${BLUE}Installing frontend dependencies...${NC}"
cd frontend
npm install
cd ..
echo -e "${GREEN}✓ Frontend dependencies installed${NC}"
}
# Build frontend
build_frontend() {
echo -e "\n${BLUE}Building frontend...${NC}"
cd frontend
npm run build
cd ..
echo -e "${GREEN}✓ Frontend built successfully${NC}"
}
# Display startup options
show_menu() {
echo -e "\n${BLUE}=== Startup Options ===${NC}"
echo "1. Full Setup (database + build everything)"
echo "2. Setup Database Only"
echo "3. Build Backend Only"
echo "4. Build Frontend Only"
echo "5. Run Backend (requires database)"
echo "6. Run Frontend Dev Server"
echo "7. Run Both (Backend + Frontend)"
echo "8. Docker Compose (recommended)"
echo "9. Exit"
echo ""
}
# Main menu
main_menu() {
while true; do
show_menu
read -p "Select option (1-9): " choice
case $choice in
1)
check_prerequisites
setup_database
build_backend
install_frontend
build_frontend
echo -e "\n${GREEN}✓ Setup complete!${NC}"
echo -e "${BLUE}Run 'make run' to start the backend${NC}"
echo -e "${BLUE}In another terminal, run 'cd frontend && npm run dev' for frontend${NC}"
;;
2)
check_prerequisites
setup_database
;;
3)
build_backend
;;
4)
install_frontend
build_frontend
;;
5)
check_prerequisites
echo -e "\n${BLUE}Starting backend on http://localhost:8080${NC}"
cd backend
../gradlew bootRun
;;
6)
echo -e "\n${BLUE}Starting frontend dev server on http://localhost:5173${NC}"
cd frontend
npm run dev
;;
7)
echo -e "\n${BLUE}Starting Backend + Frontend${NC}"
echo -e "${BLUE}Frontend: http://localhost:5173${NC}"
echo -e "${BLUE}Backend: http://localhost:8080${NC}"
(cd backend && ../gradlew bootRun) &
(cd frontend && npm run dev) &
wait
;;
8)
echo -e "\n${BLUE}Starting with Docker Compose${NC}"
docker-compose up --build
;;
9)
echo -e "${GREEN}Goodbye!${NC}"
exit 0
;;
*)
echo -e "${RED}Invalid option. Please try again.${NC}"
;;
esac
done
}
# Run main menu
main_menu