-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
Β·153 lines (127 loc) Β· 4.65 KB
/
start.sh
File metadata and controls
executable file
Β·153 lines (127 loc) Β· 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
# Three-Tier Task App - Quick Start Script
echo "π Starting Three-Tier Task Management Application..."
# Function to detect host IP
detect_host_ip() {
# Try to detect the primary network interface IP
if command -v ip &> /dev/null; then
# Linux with ip command
HOST_IP=$(ip route get 1.1.1.1 | grep -oP 'src \K\S+' 2>/dev/null)
elif command -v ifconfig &> /dev/null; then
# Linux/macOS with ifconfig
HOST_IP=$(ifconfig | grep -E 'inet.*broadcast' | grep -v 127.0.0.1 | awk '{print $2}' | head -1)
fi
# Fallback to localhost if detection fails
if [[ -z "$HOST_IP" || "$HOST_IP" == "127.0.0.1" ]]; then
HOST_IP="localhost"
fi
echo "π Detected host IP: $HOST_IP"
}
# Function to validate hostname/IP
validate_host() {
local host="$1"
# Check if it's an IP address
if [[ $host =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo "π Using IP address: $host"
return 0
# Check if it's a hostname
elif [[ $host =~ ^[a-zA-Z0-9][a-zA-Z0-9\.-]*[a-zA-Z0-9]$ ]] || [[ $host == "localhost" ]]; then
echo "π·οΈ Using hostname: $host"
echo "βΉοΈ Note: React dev server host check is disabled for hostname support"
return 0
else
echo "β οΈ Warning: '$host' may not be a valid hostname or IP"
return 1
fi
}
# Detect host IP if not already set
if [[ -z "$HOST_IP" ]]; then
detect_host_ip
fi
# Validate the HOST_IP
validate_host "$HOST_IP"
# Export HOST_IP for Docker Compose
export HOST_IP
echo "π§ Using HOST_IP: $HOST_IP"
# Check if production mode is requested
PRODUCTION_MODE=${PRODUCTION:-false}
if [[ "$PRODUCTION_MODE" == "true" ]]; then
echo "π Starting in PRODUCTION mode (static files, no host check issues)"
COMPOSE_FILES="-f docker-compose.yml -f docker-compose.prod.yml"
else
echo "π οΈ Starting in DEVELOPMENT mode"
if [[ "$HOST_IP" != "localhost" && "$HOST_IP" != "127.0.0.1" ]]; then
echo "β οΈ WARNING: Development mode with external hostname may show 'Invalid Host header'"
echo " For production deployment, use: PRODUCTION=true ./start.sh"
echo " Or access via: http://localhost:3000 (port forwarding)"
fi
COMPOSE_FILES="-f docker-compose.yml"
fi
# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
echo "β Docker is not running. Please start Docker and try again."
exit 1
fi
# Check if Docker Compose is available
if ! command -v docker > /dev/null 2>&1; then
echo "β Docker is not installed. Please install Docker and try again."
exit 1
fi
# Check if Docker Compose plugin is available
if ! docker compose version > /dev/null 2>&1; then
echo "β Docker Compose plugin is not available. Please install Docker Compose or update Docker."
exit 1
fi
echo "β
Docker and Docker Compose plugin are available"
# Create environment files if they don't exist
if [ ! -f backend/.env ]; then
echo "π Creating backend environment file..."
cp backend/.env.example backend/.env
fi
if [ ! -f frontend/.env ]; then
echo "π Creating frontend environment file..."
cp frontend/.env.example frontend/.env
fi
echo "ποΈ Building and starting services..."
# Build and start all services
docker compose $COMPOSE_FILES up --build -d
echo "β³ Waiting for services to be ready..."
# Wait for database to be healthy
echo "ποΈ Waiting for database..."
while ! docker compose exec -T database pg_isready -U taskuser -d taskmanager > /dev/null 2>&1; do
echo " Database not ready yet, waiting..."
sleep 2
done
# Wait for backend to be healthy
echo "π§ Waiting for backend API..."
while ! curl -f http://${HOST_IP}:3001/health > /dev/null 2>&1; do
echo " Backend not ready yet, waiting..."
sleep 2
done
# Wait for frontend to be ready
echo "π¨ Waiting for frontend..."
while ! curl -f http://${HOST_IP}:3000 > /dev/null 2>&1; do
echo " Frontend not ready yet, waiting..."
sleep 2
done
echo ""
echo "π Application is ready!"
echo ""
echo "π± Frontend: http://${HOST_IP}:3000"
echo "π§ Backend: http://${HOST_IP}:3001"
echo "ποΈ Database: ${HOST_IP}:5432"
echo ""
echo "π Health Check: curl http://${HOST_IP}:3001/health"
echo "π API Stats: curl http://${HOST_IP}:3001/api/stats"
echo ""
echo "π To view logs: docker compose logs -f"
echo "βΉοΈ To stop: docker compose down"
echo ""
# Open browser (optional)
if command -v open > /dev/null 2>&1; then
echo "π Opening browser..."
open http://${HOST_IP}:3000
elif command -v xdg-open > /dev/null 2>&1; then
echo "π Opening browser..."
xdg-open http://${HOST_IP}:3000
fi