-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathstartup.sh
More file actions
74 lines (63 loc) · 1.87 KB
/
Copy pathstartup.sh
File metadata and controls
74 lines (63 loc) · 1.87 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
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="/workspaces/${localWorkspaceFolderBasename:-$(basename "$PWD")}"
SERVER_DIR="$ROOT_DIR/mern/server"
CLIENT_DIR="$ROOT_DIR/mern/client"
CONFIG_FILE="$SERVER_DIR/config.env"
ATLAS_URI_VALUE="${ATLAS_URI:-mongodb://localhost:27017/}"
PORT_VALUE="${PORT:-5050}"
ensure_config() {
cat > "$CONFIG_FILE" <<EOF
ATLAS_URI=$ATLAS_URI_VALUE
PORT=$PORT_VALUE
EOF
echo "Wrote $CONFIG_FILE with ATLAS_URI and PORT."
}
wait_for_mongodb() {
local max_tries=60
local try=1
# Atlas Local uses a replica set — wait until a writable primary is elected,
# not just until mongod responds to ping (which happens before primary election).
until node --input-type=module -e "
import { MongoClient } from 'mongodb';
const c = new MongoClient(process.env.ATLAS_URI);
await c.connect();
const h = await c.db('admin').command({ hello: 1 });
await c.close();
if (!h.isWritablePrimary) throw new Error('no primary yet');
" >/dev/null 2>&1; do
if (( try >= max_tries )); then
echo "MongoDB primary did not become ready in time."
return 1
fi
echo "Waiting for MongoDB primary... ($try/$max_tries)"
try=$((try + 1))
sleep 2
done
echo "MongoDB primary is ready."
}
start_server_if_needed() {
if pgrep -f "node --env-file=config.env server" >/dev/null; then
echo "Express server already running."
return
fi
(
cd "$SERVER_DIR"
nohup npm start > /tmp/mern-server.log 2>&1 &
)
echo "Started Express server (log: /tmp/mern-server.log)."
}
start_client_if_needed() {
if pgrep -f "vite" >/dev/null; then
echo "Vite dev server already running."
return
fi
(
cd "$CLIENT_DIR"
nohup npm run dev -- --host 0.0.0.0 > /tmp/mern-client.log 2>&1 &
)
echo "Started Vite dev server (log: /tmp/mern-client.log)."
}
ensure_config
wait_for_mongodb
echo "Codespaces startup complete."