forked from ygwyg/MAHORAGA
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart
More file actions
executable file
·174 lines (144 loc) · 5.74 KB
/
start
File metadata and controls
executable file
·174 lines (144 loc) · 5.74 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
#!/usr/bin/env bash
# ============================================================================
# start — Start the Mahoraga backend and dashboard
#
# Usage:
# ./start # start both
# ./start backend # backend only
# ./start dashboard # dashboard only
# ============================================================================
set -euo pipefail
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# ---------------------------------------------------------------------------
# Session logging — mirror all output to logs/<unix-timestamp>.log
# ---------------------------------------------------------------------------
LOG_DIR="$PROJECT_ROOT/logs"
mkdir -p "$LOG_DIR"
LOG_FILE="$LOG_DIR/$(date +%s).log"
exec > >(tee >(perl -MPOSIX -pe 'BEGIN{$|=1} $_ = strftime("[%Y-%m-%d %H:%M:%S] ", localtime) . $_' > "$LOG_FILE")) 2>&1
clear
echo "Current directory: $(pwd)"
echo "Command: ./start"
echo "Starting Mahoraga... $(date)"
echo "Log file: $LOG_FILE"
echo "------------------------------------------------"
BACKEND_PORT="${WRANGLER_PORT:-8787}"
DASHBOARD_PORT=5173
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
cleanup() {
echo ""
echo "Shutting down..."
if [[ -n "${BACKEND_PID:-}" ]]; then
kill "$BACKEND_PID" 2>/dev/null && echo " Stopped backend (PID $BACKEND_PID)"
fi
if [[ -n "${DASHBOARD_PID:-}" ]]; then
kill "$DASHBOARD_PID" 2>/dev/null && echo " Stopped dashboard (PID $DASHBOARD_PID)"
fi
echo "Session log saved to: $LOG_FILE"
# Allow tee to flush remaining output
sleep 0.5
exit 0
}
trap cleanup SIGINT SIGTERM
free_port() {
local port=$1
local pids
pids=$(lsof -ti :"$port" 2>/dev/null || true)
if [[ -n "$pids" ]]; then
echo " ⚠️ Port $port in use by PID(s): $pids — killing..."
echo "$pids" | xargs kill -9 2>/dev/null || true
sleep 1
# Verify the port is actually free now
if lsof -ti :"$port" >/dev/null 2>&1; then
echo " ❌ Failed to free port $port"
return 1
fi
echo " ✅ Port $port freed"
fi
}
wait_for_port() {
local port=$1
local name=$2
local max_wait=30
local waited=0
while ! curl -s --max-time 1 "http://localhost:$port" >/dev/null 2>&1; do
sleep 1
waited=$((waited + 1))
if [[ $waited -ge $max_wait ]]; then
echo " ❌ $name did not start within ${max_wait}s"
return 1
fi
done
echo " ✅ $name ready on http://localhost:$port"
}
# ---------------------------------------------------------------------------
# Preflight checks
# ---------------------------------------------------------------------------
if [[ ! -f "$PROJECT_ROOT/.dev.vars" ]]; then
echo "❌ Missing .dev.vars — run: cp .env.example .dev.vars"
exit 1
fi
if [[ ! -f "$PROJECT_ROOT/wrangler.jsonc" ]]; then
echo "❌ Missing wrangler.jsonc — run: cp wrangler.example.jsonc wrangler.jsonc"
exit 1
fi
if [[ ! -d "$PROJECT_ROOT/node_modules" ]]; then
echo "Installing backend dependencies..."
(cd "$PROJECT_ROOT" && npm install)
fi
if [[ ! -d "$PROJECT_ROOT/dashboard/node_modules" ]]; then
echo "Installing dashboard dependencies..."
(cd "$PROJECT_ROOT/dashboard" && npm install)
fi
# ---------------------------------------------------------------------------
# Free ports if already in use
# ---------------------------------------------------------------------------
MODE="${1:-all}"
if [[ "$MODE" == "all" || "$MODE" == "backend" ]]; then
free_port "$BACKEND_PORT"
fi
if [[ "$MODE" == "all" || "$MODE" == "dashboard" ]]; then
free_port "$DASHBOARD_PORT"
fi
# ---------------------------------------------------------------------------
# Start services
# ---------------------------------------------------------------------------
echo ""
echo "╔══════════════════════════════════════════════════════════╗"
echo "║ MAHORAGA — Development Server ║"
echo "╚══════════════════════════════════════════════════════════╝"
echo ""
BACKEND_PID=""
DASHBOARD_PID=""
if [[ "$MODE" == "all" || "$MODE" == "backend" ]]; then
echo "Starting backend (Wrangler) on port $BACKEND_PORT..."
(cd "$PROJECT_ROOT" && npm run dev) &
BACKEND_PID=$!
wait_for_port "$BACKEND_PORT" "Backend"
fi
if [[ "$MODE" == "all" || "$MODE" == "dashboard" ]]; then
echo "Starting dashboard (Vite) on port $DASHBOARD_PORT..."
(cd "$PROJECT_ROOT/dashboard" && npm run dev) &
DASHBOARD_PID=$!
wait_for_port "$DASHBOARD_PORT" "Dashboard"
fi
echo ""
echo "──────────────────────────────────────────────────────────"
if [[ -n "$BACKEND_PID" ]]; then
echo " Backend: http://localhost:$BACKEND_PORT"
fi
if [[ -n "$DASHBOARD_PID" ]]; then
echo " Dashboard: http://localhost:$DASHBOARD_PORT"
fi
echo "──────────────────────────────────────────────────────────"
echo " Press Ctrl+C to stop all services"
echo "──────────────────────────────────────────────────────────"
echo ""
if [[ -d "/Applications/Firefox Developer Edition.app" ]]; then
open -a "Firefox Developer Edition" "http://localhost:$DASHBOARD_PORT" &
else
echo "Firefox Developer Edition not found. Please open http://localhost:$DASHBOARD_PORT manually."
fi
wait