|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Harmonia Startup Script |
| 4 | +# This script starts all services using Docker containers |
| 5 | + |
| 6 | +set -e # Exit on error |
| 7 | + |
| 8 | +# Colors for output |
| 9 | +GREEN='\033[0;32m' |
| 10 | +BLUE='\033[0;34m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +RED='\033[0;31m' |
| 13 | +NC='\033[0m' # No Color |
| 14 | + |
| 15 | +echo -e "${BLUE}========================================${NC}" |
| 16 | +echo -e "${BLUE} Starting Harmonia Application${NC}" |
| 17 | +echo -e "${BLUE} (Containerized Version)${NC}" |
| 18 | +echo -e "${BLUE}========================================${NC}\n" |
| 19 | + |
| 20 | +# Step 1: Check Docker is running |
| 21 | +echo -e "${GREEN}[1/4] Checking Docker...${NC}" |
| 22 | +if ! docker info >/dev/null 2>&1; then |
| 23 | + echo -e "${RED}✗ Docker is not running. Please start Docker Desktop.${NC}" |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | +echo -e "${GREEN}✓ Docker is running${NC}\n" |
| 27 | + |
| 28 | +# Step 2: Clean up any existing containers |
| 29 | +echo -e "${GREEN}[2/4] Cleaning up existing containers...${NC}" |
| 30 | +docker compose -f docker/docker-compose.yml down --remove-orphans 2>/dev/null || true |
| 31 | +echo -e "${GREEN}✓ Cleanup completed${NC}\n" |
| 32 | + |
| 33 | +# Step 3: Build and start all services |
| 34 | +echo -e "${GREEN}[3/4] Building and starting all services...${NC}" |
| 35 | +echo -e "${YELLOW}This may take several minutes for the first run...${NC}" |
| 36 | +echo -e "${YELLOW}Building Docker images and starting containers...${NC}\n" |
| 37 | + |
| 38 | +# Start services in background and capture output |
| 39 | +docker compose -f docker/docker-compose.yml up --build -d |
| 40 | + |
| 41 | +if [ $? -eq 0 ]; then |
| 42 | + echo -e "${GREEN}✓ All services started successfully${NC}\n" |
| 43 | +else |
| 44 | + echo -e "${RED}✗ Failed to start services${NC}" |
| 45 | + echo -e "${YELLOW}Check logs with: docker compose -f docker/docker-compose.yml logs${NC}" |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +# Step 4: Wait for services to be ready |
| 50 | +echo -e "${GREEN}[4/4] Waiting for services to be ready...${NC}" |
| 51 | +echo -e "${YELLOW}Checking service health...${NC}" |
| 52 | + |
| 53 | +# Wait for postgres to be healthy |
| 54 | +echo -e "${YELLOW}• Waiting for PostgreSQL...${NC}" |
| 55 | +timeout=60 |
| 56 | +while [ $timeout -gt 0 ]; do |
| 57 | + if docker compose -f docker/docker-compose.yml ps postgres | grep -q "healthy"; then |
| 58 | + echo -e "${GREEN} ✓ PostgreSQL is ready${NC}" |
| 59 | + break |
| 60 | + fi |
| 61 | + sleep 2 |
| 62 | + ((timeout-=2)) |
| 63 | +done |
| 64 | + |
| 65 | +if [ $timeout -le 0 ]; then |
| 66 | + echo -e "${RED} ✗ PostgreSQL failed to start within 60 seconds${NC}" |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | + |
| 70 | +# Wait for server to be responding |
| 71 | +echo -e "${YELLOW}• Waiting for Spring Boot server...${NC}" |
| 72 | +timeout=120 |
| 73 | +while [ $timeout -gt 0 ]; do |
| 74 | + if curl -f -s http://localhost:8080/actuator/health >/dev/null 2>&1; then |
| 75 | + echo -e "${GREEN} ✓ Server is ready${NC}" |
| 76 | + break |
| 77 | + fi |
| 78 | + sleep 3 |
| 79 | + ((timeout-=3)) |
| 80 | +done |
| 81 | + |
| 82 | +if [ $timeout -le 0 ]; then |
| 83 | + echo -e "${YELLOW} ⚠ Server may still be starting (timeout reached)${NC}" |
| 84 | +fi |
| 85 | + |
| 86 | +# Check if client is responding |
| 87 | +echo -e "${YELLOW}• Waiting for React client...${NC}" |
| 88 | +timeout=30 |
| 89 | +while [ $timeout -gt 0 ]; do |
| 90 | + if curl -f -s http://localhost:4173 >/dev/null 2>&1; then |
| 91 | + echo -e "${GREEN} ✓ Client is ready${NC}" |
| 92 | + break |
| 93 | + fi |
| 94 | + sleep 2 |
| 95 | + ((timeout-=2)) |
| 96 | +done |
| 97 | + |
| 98 | +if [ $timeout -le 0 ]; then |
| 99 | + echo -e "${YELLOW} ⚠ Client may still be starting (timeout reached)${NC}" |
| 100 | +fi |
| 101 | + |
| 102 | +# Summary |
| 103 | +echo -e "\n${BLUE}========================================${NC}" |
| 104 | +echo -e "${GREEN}✓ Harmonia is now running!${NC}" |
| 105 | +echo -e "${BLUE}========================================${NC}" |
| 106 | +echo -e "${YELLOW}Server:${NC} http://localhost:8080" |
| 107 | +echo -e "${YELLOW}Client:${NC} http://localhost:4173" |
| 108 | +echo -e "${YELLOW}Database:${NC} PostgreSQL on localhost:5432" |
| 109 | +echo -e "\n${YELLOW}Useful commands:${NC}" |
| 110 | +echo -e "${YELLOW}• View logs:${NC} docker compose -f docker/docker-compose.yml logs -f" |
| 111 | +echo -e "${YELLOW}• Stop services:${NC} docker compose -f docker/docker-compose.yml down" |
| 112 | +echo -e "${YELLOW}• Restart services:${NC} docker compose -f docker/docker-compose.yml restart" |
| 113 | +echo -e "\n${YELLOW}Press Ctrl+C to stop all services${NC}\n" |
| 114 | + |
| 115 | +# Keep script running and handle Ctrl+C |
| 116 | +trap "echo -e '\n${RED}Shutting down all services...${NC}'; docker compose -f docker/docker-compose.yml down; exit" INT |
| 117 | + |
| 118 | +# Follow logs to keep script running |
| 119 | +docker compose -f docker/docker-compose.yml logs -f |
0 commit comments