-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
210 lines (182 loc) Β· 7.09 KB
/
dev.sh
File metadata and controls
210 lines (182 loc) Β· 7.09 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/bash
# SpecForge Development Environment Startup Script (Bash/Git Bash/WSL)
# Mirrors logic from start_dev.ps1 for cross-platform availability.
# ----------------------------------------------------
# 0. Configuration & Colors
# ----------------------------------------------------
ONLY_MIGRATIONS=false
for arg in "$@"; do
case $arg in
--only-migrations)
ONLY_MIGRATIONS=true
shift
;;
esac
done
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
CYAN='\033[0;36m'
GRAY='\033[0;90m'
NC='\033[0m' # No Color
echo -e "\n${CYAN}====================================================${NC}"
echo -e "${CYAN} SpecForge Dev Environment Start ${NC}"
echo -e "${CYAN}====================================================${NC}\n"
# ----------------------------------------------------
# 1. Prerequisite Checks
# ----------------------------------------------------
echo -e "${YELLOW}0. Checking Prerequisites...${NC}"
# Docker
if ! command -v docker &> /dev/null; then
echo -e "${RED} β Docker is not installed or not in PATH!${NC}"
exit 1
fi
# Go
if ! command -v go &> /dev/null; then
echo -e "${RED} β Go is not installed! (Required for Backend)${NC}"
exit 1
fi
# Node/NPM
if ! command -v npm &> /dev/null; then
echo -e "${RED} β NPM is not installed! (Required for Frontend)${NC}"
exit 1
fi
echo -e "${GREEN} β
All prerequisites met${NC}"
# ----------------------------------------------------
# 2. Start Infrastructure (Docker)
# ----------------------------------------------------
echo -e "\n${YELLOW}1. Starting Database & Migrations (Docker)...${NC}"
docker compose up -d postgres migrate
if [ $? -eq 0 ]; then
echo -e "${GREEN} β
Docker services started${NC}"
else
echo -e "${RED} β Failed to start Docker services. Check docker-compose.yml${NC}"
exit 1
fi
# Wait for Postgres Port 5432
echo -ne "${GRAY} Waiting for Postgres (5432)...${NC}"
retries=30
while [ $retries -gt 0 ]; do
if (echo > /dev/tcp/localhost/5432) >/dev/null 2>&1; then
echo -e "${GREEN} Ready!${NC}"
break
fi
echo -n "."
sleep 1
((retries--))
done
if [ $retries -le 0 ]; then
echo -e "${RED} β Timeout waiting for Postgres.${NC}"
exit 1
fi
if [ "$ONLY_MIGRATIONS" = true ]; then
echo -e "\n${GREEN}π Migrations completed successfully!${NC}"
exit 0
fi
# ----------------------------------------------------
# 3. Start Application (Local)
# ----------------------------------------------------
echo -e "\n${YELLOW}2. Starting Backend (Go)...${NC}"
# Check if Backend is already running
if (echo > /dev/tcp/localhost/8080) >/dev/null 2>&1; then
echo -e "${CYAN} βΉοΈ Backend appears to be running already on port 8080${NC}"
else
echo -e "${GREEN} π Launching Backend in new window...${NC}"
# Environment variables for local run (connecting to localhost DB)
export DATABASE_URL='postgres://specforge:specforge@localhost:5432/specforge?sslmode=disable'
export JWT_SECRET='supersecret'
export PORT='8080'
# Launch logic based on OS/Terminal
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
# Windows (Git Bash) - Using 'start' to spawn a new CMD/Bash window
start "SpecForge Backend" bash -c "echo 'Starting SpecForge Backend...'; cd backend && go run ./cmd/server; echo 'Process exited. Press Enter to close...'; read"
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
osascript -e 'tell app "Terminal" to do script "cd '$(pwd)'/backend && export DATABASE_URL='\''postgres://specforge:specforge@localhost:5432/specforge?sslmode=disable'\'' && export JWT_SECRET='\''supersecret'\'' && export PORT='\''8080'\'' && go run ./cmd/server"'
else
# Linux (attempt common terminal emulators)
if command -v gnome-terminal &> /dev/null; then
gnome-terminal -- bash -c "cd backend && go run ./cmd/server; exec bash"
elif command -v xterm &> /dev/null; then
xterm -e "cd backend && go run ./cmd/server" &
else
echo -e "${YELLOW} β οΈ No terminal emulator found. Running in background (logs to backend.log)${NC}"
(cd backend && go run ./cmd/server) > backend.log 2>&1 &
fi
fi
fi
echo -e "\n${YELLOW}3. Starting Frontend (Vite)...${NC}"
# Check if Frontend is already running
if (echo > /dev/tcp/localhost/5173) >/dev/null 2>&1; then
echo -e "${CYAN} βΉοΈ Frontend appears to be running on port 5173${NC}"
else
echo -e "${GREEN} π Launching Frontend in new window...${NC}"
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
# Windows (Git Bash)
start "SpecForge Frontend" bash -c "echo 'Starting SpecForge Frontend...'; cd frontend && npm run dev; echo 'Process exited. Press Enter to close...'; read"
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
osascript -e 'tell app "Terminal" to do script "cd '$(pwd)'/frontend && npm run dev"'
else
# Linux
if command -v gnome-terminal &> /dev/null; then
gnome-terminal -- bash -c "cd frontend && npm run dev; exec bash"
elif command -v xterm &> /dev/null; then
xterm -e "cd frontend && npm run dev" &
else
echo -e "${YELLOW} β οΈ No terminal emulator found. Running in background (logs to frontend.log)${NC}"
(cd frontend && npm run dev) > frontend.log 2>&1 &
fi
fi
fi
# ----------------------------------------------------
# 4. Verification
# ----------------------------------------------------
echo -e "\n${YELLOW}4. Verifying Endpoints (Waiting for startup)...${NC}"
# Poll Backend Health
backendReady=false
echo -ne "${GRAY} Waiting for Backend...${NC}"
for ((i=0; i<20; i++)); do
if curl -s http://localhost:8080/health | grep -q "OK" &> /dev/null; then
backendReady=true
break
fi
echo -n "."
sleep 1
done
echo ""
if [ "$backendReady" = true ]; then
echo -e "${GREEN} β
Backend is UP${NC}"
else
echo -e "${RED} β Backend failed to start within timeout${NC}"
fi
# Poll Frontend
frontendReady=false
echo -ne "${GRAY} Waiting for Frontend...${NC}"
for ((i=0; i<20; i++)); do
if (echo > /dev/tcp/localhost/5173) >/dev/null 2>&1; then
frontendReady=true
break
fi
echo -n "."
sleep 1
done
echo ""
if [ "$frontendReady" = true ]; then
echo -e "${GREEN} β
Frontend is UP${NC}"
else
echo -e "${RED} β Frontend failed to start within timeout${NC}"
fi
# ----------------------------------------------------
# 5. Final Status
# ----------------------------------------------------
echo -e "\n${CYAN}----------------------------------------${NC}"
if [ "$backendReady" = true ] && [ "$frontendReady" = true ]; then
echo -e "${GREEN}π Development Environment is READY!${NC}"
echo -e " Frontend: ${CYAN}http://localhost:5173${NC}"
echo -e " Backend: ${CYAN}http://localhost:8080${NC}"
else
echo -e "${YELLOW} β οΈ Some services failed to start correctly.${NC}"
echo -e " Check windows/logs for details."
fi
echo -e "${CYAN}----------------------------------------${NC}\n"