-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-dev.sh
More file actions
executable file
·374 lines (314 loc) · 11 KB
/
start-dev.sh
File metadata and controls
executable file
·374 lines (314 loc) · 11 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/bin/bash
# =============================================================================
# GAAP Development Environment Management Script
# For Linux/macOS
# =============================================================================
set -e
# Configuration
COMPOSE_FILE="docker-compose.dev.yml"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Auto-detect Docker Compose command (v2 plugin vs v1 standalone)
if docker compose version &> /dev/null; then
DOCKER_COMPOSE="docker compose"
elif docker-compose version &> /dev/null; then
DOCKER_COMPOSE="docker-compose"
else
echo "Error: Neither 'docker compose' nor 'docker-compose' found."
echo "Please install Docker with Compose plugin or standalone docker-compose."
exit 1
fi
# Service groups
MIDDLEWARE_SERVICES="postgres redis rabbitmq caddy"
API_SERVICES="gaap-api"
WEB_SERVICES="gaap-web"
ALL_SERVICES="$MIDDLEWARE_SERVICES $API_SERVICES $WEB_SERVICES"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# =============================================================================
# Helper Functions
# =============================================================================
print_header() {
echo -e "\n${BLUE}========================================${NC}"
echo -e "${BLUE} $1${NC}"
echo -e "${BLUE}========================================${NC}\n"
}
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠ $1${NC}"
}
print_error() {
echo -e "${RED}✗ $1${NC}"
}
print_info() {
echo -e "${BLUE}ℹ $1${NC}"
}
# Get services based on target
get_services() {
local target=$1
case $target in
middleware)
echo "$MIDDLEWARE_SERVICES"
;;
api)
echo "$API_SERVICES"
;;
web)
echo "$WEB_SERVICES"
;;
all)
echo "$ALL_SERVICES"
;;
*)
print_error "Unknown target: $target"
print_info "Valid targets: middleware, api, web, all"
exit 1
;;
esac
}
# =============================================================================
# Core Functions
# =============================================================================
# Start services
start_services() {
local target=${1:-all}
local services=$(get_services "$target")
print_header "Starting $target services"
print_info "Services: $services"
$DOCKER_COMPOSE -f "$COMPOSE_FILE" up -d $services
print_success "$target services started successfully!"
# Show logs hint
echo ""
print_info "To view logs, run: ./start-dev.sh logs $target"
}
# Stop services
stop_services() {
local target=${1:-all}
local services=$(get_services "$target")
print_header "Stopping $target services"
print_info "Services: $services"
$DOCKER_COMPOSE -f "$COMPOSE_FILE" stop $services
print_success "$target services stopped successfully!"
}
# Restart services
restart_services() {
local target=${1:-all}
local services=$(get_services "$target")
print_header "Restarting $target services"
stop_services "$target"
start_services "$target"
}
# Show service logs (supports multiple targets)
show_logs() {
local targets="${@:-all}"
local all_services=""
# Collect services from all specified targets
for target in $targets; do
local services=$(get_services "$target")
all_services="$all_services $services"
done
# Remove leading space and deduplicate
all_services=$(echo $all_services | tr ' ' '\n' | sort -u | tr '\n' ' ')
print_header "Showing logs for: $targets"
print_info "Services: $all_services"
$DOCKER_COMPOSE -f "$COMPOSE_FILE" logs -f $all_services
}
# Show service status
show_status() {
print_header "Service Status"
$DOCKER_COMPOSE -f "$COMPOSE_FILE" ps
}
# Clean up - remove containers, volumes, and build cache
clean() {
local target=${1:-all}
print_header "Cleaning up $target"
if [ "$target" = "all" ]; then
print_warning "This will remove all containers, anonymous volumes, and build cache."
read -p "Are you sure? (y/N): " confirm
if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then
print_info "Cleanup cancelled."
exit 0
fi
print_info "Stopping all services..."
$DOCKER_COMPOSE -f "$COMPOSE_FILE" down -v --remove-orphans
print_info "Pruning build cache..."
docker builder prune -f 2>/dev/null || true
print_success "Full cleanup completed!"
else
local services=$(get_services "$target")
print_info "Stopping and removing $target services..."
$DOCKER_COMPOSE -f "$COMPOSE_FILE" stop $services
$DOCKER_COMPOSE -f "$COMPOSE_FILE" rm -f $services
print_success "$target cleanup completed!"
fi
}
# Install/reinstall dependencies
install_deps() {
local target=${1:-all}
print_header "Installing dependencies for $target"
if [ "$target" = "middleware" ]; then
print_info "Middleware services don't require dependency installation."
return
fi
if [ "$target" = "api" ] || [ "$target" = "all" ]; then
print_info "Installing Go dependencies..."
$DOCKER_COMPOSE -f "$COMPOSE_FILE" exec gaap-api go mod download 2>/dev/null || \
(cd gaap-api && go mod download)
print_success "Go dependencies installed!"
fi
if [ "$target" = "web" ] || [ "$target" = "all" ]; then
print_info "Installing Node.js dependencies..."
# Stop web container, remove it to clear anonymous volumes, rebuild and restart
$DOCKER_COMPOSE -f "$COMPOSE_FILE" stop gaap-web 2>/dev/null || true
$DOCKER_COMPOSE -f "$COMPOSE_FILE" rm -f gaap-web 2>/dev/null || true
$DOCKER_COMPOSE -f "$COMPOSE_FILE" build --no-cache gaap-web
$DOCKER_COMPOSE -f "$COMPOSE_FILE" up -d gaap-web
print_success "Node.js dependencies installed!"
fi
print_success "Dependencies installation completed!"
}
# Rebuild services (for refreshing dependencies in containers)
rebuild() {
local target=${1:-all}
local services=$(get_services "$target")
print_header "Rebuilding $target services"
print_info "Services: $services"
# Stop and remove containers to clear anonymous volumes
$DOCKER_COMPOSE -f "$COMPOSE_FILE" stop $services
$DOCKER_COMPOSE -f "$COMPOSE_FILE" rm -f $services
# Rebuild images
$DOCKER_COMPOSE -f "$COMPOSE_FILE" build $services
# Start services
$DOCKER_COMPOSE -f "$COMPOSE_FILE" up -d $services
print_success "$target services rebuilt and started!"
print_info "Hot-reload is active. Changes to source files will be reflected automatically."
}
# Execute command in container
exec_cmd() {
local target=$1
shift
local cmd="$@"
if [ -z "$cmd" ]; then
print_error "No command specified."
exit 1
fi
case $target in
api)
$DOCKER_COMPOSE -f "$COMPOSE_FILE" exec gaap-api $cmd
;;
web)
$DOCKER_COMPOSE -f "$COMPOSE_FILE" exec gaap-web $cmd
;;
*)
print_error "exec only supports 'api' or 'web' targets"
exit 1
;;
esac
}
# Show help
show_help() {
echo -e "${BLUE}GAAP Development Environment Management Script${NC}"
echo ""
echo -e "${YELLOW}Usage:${NC}"
echo " ./start-dev.sh <command> [target]"
echo ""
echo -e "${YELLOW}Commands:${NC}"
echo " start [target] Start services (default: all)"
echo " stop [target] Stop services (default: all)"
echo " restart [target] Restart services (default: all)"
echo " logs [targets...] Show service logs, supports multiple (default: all)"
echo " status Show service status"
echo " clean [target] Clean up containers and volumes (default: all)"
echo " install [target] Install/reinstall dependencies (default: all)"
echo " rebuild [target] Rebuild and restart services (default: all)"
echo " exec <target> <cmd> Execute command in container (api/web only)"
echo " help Show this help message"
echo ""
echo -e "${YELLOW}Targets:${NC}"
echo " middleware PostgreSQL, Redis, RabbitMQ, Caddy"
echo " api GAAP API (GoFrame backend)"
echo " web GAAP Web (Next.js frontend)"
echo " all All services (default)"
echo ""
echo -e "${YELLOW}Examples:${NC}"
echo " ./start-dev.sh start # Start all services"
echo " ./start-dev.sh start middleware # Start only middleware"
echo " ./start-dev.sh restart web # Restart web service"
echo " ./start-dev.sh logs api web # View API and web logs together"
echo " ./start-dev.sh clean web # Clean web service"
echo " ./start-dev.sh install web # Reinstall npm dependencies"
echo " ./start-dev.sh rebuild api # Rebuild API service"
echo " ./start-dev.sh exec api go test ./... # Run tests in API container"
echo " ./start-dev.sh exec web npm run lint # Run linting in web container"
echo ""
echo -e "${YELLOW}Hot-Reload:${NC}"
echo " - API: Uses 'air' for automatic Go rebuilds"
echo " - Web: Uses Next.js built-in hot-reload (Turbopack)"
echo ""
echo -e "${YELLOW}Notes:${NC}"
echo " - Source code is mounted into containers for hot-reload"
echo " - Use 'install' or 'rebuild' to refresh dependencies"
echo " - Use 'clean' to remove containers and anonymous volumes"
echo ""
}
# =============================================================================
# Main
# =============================================================================
# Check if docker-compose file exists
if [ ! -f "$COMPOSE_FILE" ]; then
print_error "Docker compose file not found: $COMPOSE_FILE"
exit 1
fi
# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
print_error "Docker is not running. Please start Docker first."
exit 1
fi
# Parse command
command=${1:-help}
target=${2:-all}
case $command in
start)
start_services "$target"
;;
stop)
stop_services "$target"
;;
restart)
restart_services "$target"
;;
logs)
shift # Remove 'logs' from arguments
show_logs "$@"
;;
status)
show_status
;;
clean)
clean "$target"
;;
install)
install_deps "$target"
;;
rebuild)
rebuild "$target"
;;
exec)
shift 2 2>/dev/null || shift 1
exec_cmd "$target" "$@"
;;
help|--help|-h)
show_help
;;
*)
print_error "Unknown command: $command"
show_help
exit 1
;;
esac