-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.sh
287 lines (261 loc) · 10.2 KB
/
startup.sh
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
set -euo pipefail
PROJECT_ROOT=$(pwd)
LOG_FILE="${PROJECT_ROOT}/startup.log"
FRONTEND_PID_FILE="${PROJECT_ROOT}/frontend.pid"
BACKEND_PID_FILE="${PROJECT_ROOT}/backend.pid"
HEALTH_CHECK_INTERVAL=10
FRONTEND_HEALTH_URL="http://localhost:5173"
BACKEND_HEALTH_URL="http://localhost:3000/api/health"
export $(grep -v '^#' .env | xargs)
log_info() {
date +"%Y-%m-%d %H:%M:%S" "$@" | tee -a "$LOG_FILE"
}
log_error() {
date +"%Y-%m-%d %H:%M:%S" "$@" | tee -a "$LOG_FILE" >&2
}
cleanup() {
log_info "Cleaning up processes..."
if [ -f "$FRONTEND_PID_FILE" ]; then
kill "$(cat "$FRONTEND_PID_FILE")"
rm "$FRONTEND_PID_FILE"
fi
if [ -f "$BACKEND_PID_FILE" ]; then
kill "$(cat "$BACKEND_PID_FILE")"
rm "$BACKEND_PID_FILE"
fi
log_info "Cleanup complete."
}
check_dependencies() {
if ! command -v npm &> /dev/null; then
log_error "Error: npm is not installed. Please install Node.js and npm."
exit 1
fi
if ! command -v node &> /dev/null; then
log_error "Error: node is not installed. Please install Node.js."
exit 1
fi
}
check_port() {
local port="$1"
if nc -z localhost "$port"; then
log_error "Error: Port ${port} is already in use."
exit 1
fi
}
wait_for_service() {
local url="$1"
local timeout="$2"
local start_time=$(date +%s)
while true; do
if curl -s -o /dev/null -w "%{http_code}" "$url" | grep -q "200"; then
log_info "Service at ${url} is ready."
return 0
fi
local elapsed_time=$(( $(date +%s) - start_time ))
if [ "$elapsed_time" -ge "$timeout" ]; then
log_error "Error: Service at ${url} timed out after ${timeout} seconds."
return 1
fi
sleep "$HEALTH_CHECK_INTERVAL"
done
}
store_pid() {
local pid="$1"
local pid_file="$2"
echo "$pid" > "$pid_file"
}
verify_service() {
local url="$1"
if ! curl -s -o /dev/null -w "%{http_code}" "$url" | grep -q "200"; then
log_error "Error: Health check failed for service at $url."
return 1
fi
}
start_frontend() {
log_info "Starting frontend..."
cd "$PROJECT_ROOT"
npm install
npm run dev &
FRONTEND_PID=$!
store_pid "$FRONTEND_PID" "$FRONTEND_PID_FILE"
log_info "Frontend started with PID: $FRONTEND_PID"
if wait_for_service "$FRONTEND_HEALTH_URL" 60; then
log_info "Frontend service is ready."
else
log_error "Error: Frontend failed to start."
cleanup
exit 1
fi
}
start_backend() {
log_info "Starting backend..."
if ! command -v node &> /dev/null; then
log_error "Error: Node.js not found, please install node"
cleanup
exit 1
fi
node -e '
const http = require("http");
const port = 3000;
const server = http.createServer((req, res) => {
if (req.url === "/api/health") {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("OK");
} else if (req.url === "/api/auth/register" && req.method === "POST") {
let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", () => {
try {
const parsedBody = JSON.parse(body);
console.log("Registration requested:", parsedBody);
res.writeHead(201, { "Content-Type": "application/json" });
res.end(JSON.stringify({
"id": "user123",
"username": parsedBody.username,
"email": parsedBody.email,
"token": "mocked_jwt_token",
}));
} catch (error) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Invalid JSON" }));
console.error("Error parsing JSON:", error);
}
});
} else if (req.url === "/api/auth/login" && req.method === "POST") {
let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", () => {
try {
const parsedBody = JSON.parse(body);
console.log("Login requested:", parsedBody);
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({
"token": "mocked_jwt_token",
"user": {
"username": "user",
"email": parsedBody.email,
}
}));
} catch (error) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Invalid JSON" }));
console.error("Error parsing JSON:", error);
}
});
}else if (req.url === "/api/goals" && req.method === "POST") {
let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", () => {
try {
const parsedBody = JSON.parse(body);
console.log("Goal Creation requested:", parsedBody);
res.writeHead(201, { "Content-Type": "application/json" });
res.end(JSON.stringify({
"id": 1,
"name": parsedBody.name,
"description": parsedBody.description,
"startDate": parsedBody.startDate,
"endDate": parsedBody.endDate,
"targetValue": parsedBody.targetValue,
}));
} catch (error) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Invalid JSON" }));
console.error("Error parsing JSON:", error);
}
});
} else if (req.url === "/api/goals" && req.method === "GET") {
console.log("Get all goals requested");
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify([{
"id": 1,
"name": "Run 5k",
"description": "Complete 5 kilometers",
"startDate": null,
"endDate": null,
"targetValue": 5,
},
{
"id": 2,
"name": "Eat healthy",
"description": "Avoid fast food",
"startDate": null,
"endDate": null,
"targetValue": 30,
}]));
}else if (req.url === "/api/goals/1" && req.method === "PUT") {
let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", () => {
try {
const parsedBody = JSON.parse(body);
console.log("Update goal requested:", parsedBody);
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({
"id": 1,
"name": parsedBody.name,
"description": parsedBody.description,
"startDate": parsedBody.startDate,
"endDate": parsedBody.endDate,
"targetValue": parsedBody.targetValue,
}));
} catch (error) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Invalid JSON" }));
console.error("Error parsing JSON:", error);
}
});
} else if (req.url === "/api/profile" && req.method === "GET") {
console.log("Get profile requested");
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({
"username": "user",
"email": "user@example.com",
}));
} else if (req.url === "/api/dashboard" && req.method === "GET") {
console.log("Get Dashboard data requested");
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({
"totalWorkouts": 5,
"totalCaloriesBurned": 1200,
"averageWorkoutTime": 45,
"bestWorkoutTime": 60,
}));
}
else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not Found");
}
});
server.listen(port, () => {
console.log(\`Mock backend server is running on http://localhost:\${port}\`);
});
' &
BACKEND_PID=$!
store_pid "$BACKEND_PID" "$BACKEND_PID_FILE"
log_info "Backend started with PID: $BACKEND_PID"
if wait_for_service "$BACKEND_HEALTH_URL" 60; then
log_info "Backend service is ready."
else
log_error "Error: Backend failed to start."
cleanup
exit 1
fi
}
trap cleanup EXIT ERR INT TERM
check_dependencies
log_info "Starting application..."
start_frontend
start_backend
verify_service "$FRONTEND_HEALTH_URL"
verify_service "$BACKEND_HEALTH_URL"
log_info "Application started successfully."