-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·309 lines (262 loc) · 7.65 KB
/
dev.sh
File metadata and controls
executable file
·309 lines (262 loc) · 7.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/bash
# ═══════════════════════════════════════════════════════════════
# Crypto Vision — Local Development Services
# ═══════════════════════════════════════════════════════════════
# Usage:
# ./dev.sh api # Start main API (port 8080)
# ./dev.sh dashboard # Start dashboard (port 3000)
# ./dev.sh news # Start news app (port 3001)
# ./dev.sh video # Start video app (port 3002)
# ./dev.sh redis # Start Redis (port 6379)
# ./dev.sh all # Start all services
# ./dev.sh status # Show running services
# ═══════════════════════════════════════════════════════════════
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PID_DIR="$ROOT_DIR/.dev-pids"
# Create pid directory
mkdir -p "$PID_DIR"
# ─────────────────────────────────────────────────────────────
# Helper Functions
# ─────────────────────────────────────────────────────────────
log_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
log_success() {
echo -e "${GREEN}✓${NC} $1"
}
log_warn() {
echo -e "${YELLOW}⚠${NC} $1"
}
log_error() {
echo -e "${RED}✗${NC} $1"
}
# Save PID for later cleanup
save_pid() {
local service=$1
local pid=$2
echo $pid > "$PID_DIR/$service.pid"
}
get_pid() {
local service=$1
if [ -f "$PID_DIR/$service.pid" ]; then
cat "$PID_DIR/$service.pid"
fi
}
# Check if service is running
is_running() {
local service=$1
local pid=$(get_pid "$service")
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
return 0
fi
return 1
}
# Start a service
start_service() {
local service=$1
local port=$2
local dir=$3
local cmd=$4
if is_running "$service"; then
log_warn "$service is already running (PID: $(get_pid "$service"))"
return 0
fi
log_info "Starting $service on port $port..."
cd "$dir"
eval "$cmd" &
local pid=$!
save_pid "$service" "$pid"
log_success "$service started (PID: $pid)"
}
# Stop a service
stop_service() {
local service=$1
local pid=$(get_pid "$service")
if [ -z "$pid" ]; then
log_warn "$service is not running"
return 0
fi
if kill -0 "$pid" 2>/dev/null; then
log_info "Stopping $service (PID: $pid)..."
kill $pid 2>/dev/null || true
sleep 1
if kill -0 "$pid" 2>/dev/null; then
kill -9 $pid 2>/dev/null || true
fi
rm -f "$PID_DIR/$service.pid"
log_success "$service stopped"
else
log_warn "$service is not running"
rm -f "$PID_DIR/$service.pid"
fi
}
# Show status
show_status() {
echo ""
echo "Service Status:"
echo "─────────────────────────────────────────────"
local services=("redis" "api" "dashboard" "news" "video")
local ports=("6379" "8080" "3000" "3001" "3002")
for i in "${!services[@]}"; do
local service="${services[$i]}"
local port="${ports[$i]}"
local pid=$(get_pid "$service")
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
echo -e "${GREEN}●${NC} $service (PID: $pid, port: $port)"
else
echo -e "${RED}●${NC} $service (not running)"
fi
done
echo ""
}
# ─────────────────────────────────────────────────────────────
# Main Commands
# ─────────────────────────────────────────────────────────────
cmd_redis() {
if is_running "redis"; then
log_warn "Redis is already running"
else
log_info "Starting Redis..."
redis-server --port 6379 &
local pid=$!
save_pid "redis" "$pid"
sleep 1
log_success "Redis started on port 6379 (PID: $pid)"
fi
}
cmd_api() {
start_service "api" "8080" "$ROOT_DIR" "npm run dev"
sleep 2
echo ""
log_info "API available at http://localhost:8080"
echo ""
}
cmd_dashboard() {
start_service "dashboard" "3000" "$ROOT_DIR/apps/dashboard" "npm run dev"
sleep 2
echo ""
log_info "Dashboard available at http://localhost:3000"
echo ""
}
cmd_news() {
start_service "news" "3001" "$ROOT_DIR/apps/news" "PORT=3001 npm run dev"
sleep 2
echo ""
log_info "News app available at http://localhost:3001"
echo ""
}
cmd_video() {
start_service "video" "3002" "$ROOT_DIR/apps/video" "PORT=3002 npm run dev"
sleep 2
echo ""
log_info "Video app available at http://localhost:3002"
echo ""
}
cmd_all() {
log_info "Starting all services..."
echo ""
cmd_redis
cmd_api
cmd_dashboard
cmd_news
cmd_video
show_status
log_info "All services started!"
log_warn "To view logs: tail -f <service>.log"
log_warn "To stop: ./dev.sh stop <service> or ./dev.sh stop all"
}
cmd_stop() {
local target=$1
if [ "$target" == "all" ] || [ -z "$target" ]; then
log_info "Stopping all services..."
stop_service "redis"
stop_service "api"
stop_service "dashboard"
stop_service "news"
stop_service "video"
echo ""
log_success "All services stopped"
else
stop_service "$target"
fi
}
cmd_status() {
show_status
}
cmd_help() {
cat << EOF
${BLUE}Crypto Vision — Local Development${NC}
Usage: ./dev.sh <command> [options]
Commands:
${GREEN}api${NC} Start main API server (port 8080)
${GREEN}dashboard${NC} Start dashboard (port 3000)
${GREEN}news${NC} Start news app (port 3001)
${GREEN}video${NC} Start video app (port 3002)
${GREEN}redis${NC} Start Redis server (port 6379)
${GREEN}all${NC} Start all services
${GREEN}stop${NC} Stop service(s) - usage: ./dev.sh stop [service|all]
${GREEN}status${NC} Show service status
${GREEN}help${NC} Show this help message
Examples:
./dev.sh api # Start only API
./dev.sh all # Start everything
./dev.sh stop api # Stop API
./dev.sh stop all # Stop all services
./dev.sh status # Check what's running
Services:
api - http://localhost:8080
dashboard - http://localhost:3000
news - http://localhost:3001
video - http://localhost:3002
redis - localhost:6379
EOF
}
# ─────────────────────────────────────────────────────────────
# Main Entry Point
# ─────────────────────────────────────────────────────────────
if [ -z "$1" ]; then
cmd_help
exit 0
fi
case "$1" in
redis)
cmd_redis
;;
api)
cmd_api
;;
dashboard)
cmd_dashboard
;;
news)
cmd_news
;;
video)
cmd_video
;;
all)
cmd_all
;;
stop)
cmd_stop "$2"
;;
status)
cmd_status
;;
help|--help|-h)
cmd_help
;;
*)
log_error "Unknown command: $1"
echo ""
cmd_help
exit 1
;;
esac