-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfapi.sh
More file actions
executable file
·714 lines (625 loc) · 20.1 KB
/
Copy pathfapi.sh
File metadata and controls
executable file
·714 lines (625 loc) · 20.1 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# ── Runtime detection (prefer native Podman pods) ─────────────────────────────
# ── Variable initialization (required for set -u) ──────────────────────────────
RUNTIME=""
COMPOSE_CMD=""
detect_runtime() {
if command -v podman &>/dev/null; then
RUNTIME_MODE="podman-pod"
RUNTIME="podman"
elif command -v docker &>/dev/null && docker compose version &>/dev/null 2>&1; then
RUNTIME_MODE="docker-compose"
COMPOSE_CMD="docker compose"
elif command -v docker &>/dev/null && command -v docker-compose &>/dev/null; then
RUNTIME_MODE="docker-compose"
COMPOSE_CMD="docker-compose"
else
echo "Error: No container runtime found."
echo "Install one of:"
echo " - Podman (native pods)"
echo " - Docker Desktop (includes docker compose V2)"
echo " - Docker Engine + docker-compose"
exit 1
fi
}
detect_runtime
# ── Color helpers ─────────────────────────────────────────────────────────────
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
RED='\033[1;31m'
NC='\033[0m'
echo_running() { echo -e "${YELLOW}>>> $1${NC}"; }
echo_success() { echo -e "${GREEN}>>> $1${NC}"; }
echo_error() { echo -e "${RED}>>> $1${NC}"; }
# ── Mode derivation (ecosystem vs standalone) ─────────────────────────────────
if [[ -n "${FLUENT_ECOSYSTEM:-}" ]]; then
POD_NAME="$FLUENT_POD_NAME"
CONTAINER_PREFIX="$FLUENT_CONTAINER_PREFIX"
DB_CONTAINER="${CONTAINER_PREFIX}db"
API_CONTAINER="${CONTAINER_PREFIX}api"
WORKER_CONTAINER="${CONTAINER_PREFIX}worker"
PGDATA_VOLUME="${FLUENT_PGDATA_VOLUME:-fluent-pgdata}"
DB_PORT="${FLUENT_DB_PORT:-5432}"
API_PORT="${FLUENT_API_PORT:-9999}"
SKIP_DB=1
else
POD_NAME="fluent-api"
CONTAINER_PREFIX="fluent-api-"
DB_CONTAINER="fluent-api-db"
API_CONTAINER="fluent-api-api"
WORKER_CONTAINER="fluent-api-worker"
PGDATA_VOLUME="fluent-api-pgdata"
DB_PORT="${DB_PORT:-5432}"
API_PORT="${API_PORT:-9999}"
SKIP_DB=0
fi
# ── Podman volume / pod management ───────────────────────────────────────────
pod_create() {
if $RUNTIME pod exists "$POD_NAME" 2>/dev/null; then
echo_success "Pod $POD_NAME already exists"
return
fi
echo_running "Creating pod $POD_NAME..."
$RUNTIME pod create \
--name "$POD_NAME" \
--share "net,ipc,uts" \
-p "${DB_PORT}:5432" \
-p "${API_PORT}:9999"
}
pod_destroy() {
if $RUNTIME pod exists "$POD_NAME" 2>/dev/null; then
echo_running "Removing pod $POD_NAME..."
$RUNTIME pod rm "$POD_NAME" -f
fi
}
create_volumes() {
echo_running "Creating volumes..."
$RUNTIME volume create $PGDATA_VOLUME 2>/dev/null || true
}
wait_for_db() {
echo_running "Waiting for database to be ready..."
while ! $RUNTIME exec $DB_CONTAINER pg_isready -U postgres -d fluent 2>/dev/null; do
sleep 2
done
echo_success "Database is ready"
}
wait_for_api() {
echo_running "Waiting for API to be ready..."
local retries=30
while [ "$retries" -gt 0 ]; do
if $RUNTIME exec $API_CONTAINER curl -sf http://localhost:9999/health 2>/dev/null; then
echo_success "API is ready"
return
fi
retries=$((retries - 1))
echo -n "."
sleep 3
done
echo ""
echo_error "API did not become healthy in time"
exit 1
}
# ── Podman container start functions ──────────────────────────────────────────
start_db_container() {
if $RUNTIME container exists $DB_CONTAINER 2>/dev/null; then
echo_success "Database container already exists"
return
fi
echo_running "Starting database container..."
$RUNTIME run -d \
--name $DB_CONTAINER \
--pod "$POD_NAME" \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=fluent \
-v $PGDATA_VOLUME:/var/lib/postgresql/data \
--health-cmd "pg_isready -U postgres -d fluent" \
--health-interval 5s \
--health-timeout 5s \
--health-retries 5 \
docker.io/postgres:16-alpine
echo_success "Database container started"
}
start_api_container() {
if $RUNTIME container exists $API_CONTAINER 2>/dev/null; then
echo_success "API container already exists"
return
fi
echo_running "Building API image..."
$RUNTIME build -t fluent-api "$SCRIPT_DIR" -f Dockerfile.dev
local -a env_flags=(
-e "NODE_ENV=development"
-e "BOOTSTRAP_DATABASE_URL=postgres://postgres:postgres@localhost:5432/fluent"
-e "MIGRATIONS_DATABASE_URL=postgres://api_migrator:password@localhost:5432/fluent"
-e "DATABASE_URL=postgres://api_user:password@localhost:5432/fluent"
-e "EXPORTS_DIR=/app/exports"
)
if [[ -f "$SCRIPT_DIR/.env" ]]; then
env_flags+=(--env-file "$SCRIPT_DIR/.env")
fi
echo_running "Starting API container..."
$RUNTIME run -d \
--name $API_CONTAINER \
--pod "$POD_NAME" \
"${env_flags[@]}" \
-v "$SCRIPT_DIR/src:/app/src:ro" \
-v "$SCRIPT_DIR/tsconfig.json:/app/tsconfig.json:ro" \
-v "$SCRIPT_DIR/drizzle.config.ts:/app/drizzle.config.ts:ro" \
-v "$SCRIPT_DIR/docker-entrypoint.sh:/app/docker-entrypoint.sh:ro" \
--tmpfs /tmp:noexec,nosuid,size=64m \
--tmpfs /app/.cache:noexec,nosuid,size=128m \
--tmpfs /app/exports:noexec,nosuid,size=256m \
--security-opt no-new-privileges:true \
--cap-drop ALL \
--user 1001:1001 \
--read-only \
fluent-api
echo_success "API container started"
}
start_worker_container() {
if $RUNTIME container exists $WORKER_CONTAINER 2>/dev/null; then
echo_success "Worker container already exists"
return
fi
local -a env_flags=(
-e "NODE_ENV=development"
-e "BOOTSTRAP_DATABASE_URL=postgres://postgres:postgres@localhost:5432/fluent"
-e "MIGRATIONS_DATABASE_URL=postgres://api_migrator:password@localhost:5432/fluent"
-e "DATABASE_URL=postgres://api_user:password@localhost:5432/fluent"
-e "EXPORTS_DIR=/app/exports"
)
if [[ -f "$SCRIPT_DIR/.env" ]]; then
env_flags+=(--env-file "$SCRIPT_DIR/.env")
fi
echo_running "Starting worker container..."
$RUNTIME run -d \
--name $WORKER_CONTAINER \
--pod "$POD_NAME" \
"${env_flags[@]}" \
-v "$SCRIPT_DIR/src:/app/src:ro" \
-v "$SCRIPT_DIR/tsconfig.json:/app/tsconfig.json:ro" \
--tmpfs /tmp:noexec,nosuid,size=64m \
--tmpfs /app/.cache:noexec,nosuid,size=128m \
--tmpfs /app/exports:noexec,nosuid,size=256m \
--security-opt no-new-privileges:true \
--cap-drop ALL \
--user 1001:1001 \
--read-only \
fluent-api \
npx tsx watch src/workers/standalone-worker.ts
echo_success "Worker container started"
}
# ── Podman command functions ───────────────────────────────────────────────────
podman_up() {
local service="${1:-all}"
case "$service" in
all)
if [[ "$SKIP_DB" -eq 0 ]]; then
create_volumes
pod_create
start_db_container
wait_for_db
fi
start_api_container
wait_for_api
start_worker_container
echo_success "All services started!"
;;
db)
create_volumes
pod_create
start_db_container
echo_success "Database started!"
;;
api)
pod_create
start_api_container
echo_success "API service started!"
;;
worker)
pod_create
start_worker_container
echo_success "Worker started!"
;;
*)
echo_error "Unknown service: $service (use: db, api, worker, or omit for all)"
exit 1
;;
esac
}
podman_down() {
local service="${1:-all}"
if [ "$service" = "all" ]; then
pod_destroy
echo_success "All services stopped."
else
echo_running "Stopping $service..."
$RUNTIME rm -f "${CONTAINER_PREFIX}$service" 2>/dev/null || true
echo_success "$service stopped."
fi
}
podman_restart() {
local service="${1:-all}"
if [ "$service" = "all" ]; then
pod_destroy
podman_up all
else
echo_running "Restarting $service..."
$RUNTIME rm -f "${CONTAINER_PREFIX}$service" 2>/dev/null || true
case "$service" in
db) start_db_container ;;
api) start_api_container ;;
worker) start_worker_container ;;
*) echo_error "Unknown service: $service"; exit 1 ;;
esac
echo_success "Restarted $service"
fi
}
podman_logs() {
local service="${1:-}"
if [ -z "$service" ]; then
$RUNTIME pod logs -f "$POD_NAME"
else
# Route through pod logs --container to avoid name-resolution bugs
# in some Podman builds where 'podman logs <hyphenated-name>' fails.
$RUNTIME pod logs --container "${CONTAINER_PREFIX}$service" -f "$POD_NAME"
fi
}
podman_status() {
$RUNTIME pod ps
if $RUNTIME pod exists "$POD_NAME" 2>/dev/null; then
echo ""
echo "Containers in pod $POD_NAME (all states):"
$RUNTIME ps -a --filter "pod=$POD_NAME"
fi
}
podman_shell() {
local service="${1:-api}"
if [ "$service" = "db" ]; then
$RUNTIME exec -it $DB_CONTAINER psql -U postgres -d fluent
else
$RUNTIME exec -it "${CONTAINER_PREFIX}$service" sh
fi
}
podman_exec_api() {
if ! $RUNTIME ps --format "{{.Names}}" 2>/dev/null | grep -qx "$API_CONTAINER"; then
echo_error "API container ($API_CONTAINER) is not running. Run './fapi.sh up' first."
exit 1
fi
$RUNTIME exec "$API_CONTAINER" "$@"
}
podman_clean() {
local service="${1:-all}"
if [ "$service" = "all" ]; then
pod_destroy
$RUNTIME volume rm $PGDATA_VOLUME 2>/dev/null || true
echo_success "All containers and volumes removed."
else
$RUNTIME rm -f "${CONTAINER_PREFIX}$service" 2>/dev/null || true
echo_success "$service container removed."
fi
}
podman_fresh() {
pod_destroy
$RUNTIME volume rm $PGDATA_VOLUME 2>/dev/null || true
$RUNTIME rmi -f fluent-api 2>/dev/null || true
echo_success "Removed all containers, volumes, and images."
}
podman_build() {
local service="${1:-api}"
if [ "$service" = "api" ] || [ "$service" = "all" ]; then
echo_running "Building API image..."
$RUNTIME build -t fluent-api "$SCRIPT_DIR" -f Dockerfile.dev
echo_success "API image built."
else
echo_error "Unknown buildable service: $service (only 'api' has a custom image)"
exit 1
fi
}
podman_db_psql() {
$RUNTIME exec -it $DB_CONTAINER psql -U postgres -d fluent
}
# ── Docker Compose command functions ──────────────────────────────────────────
compose_up() {
local service="${1:-}"
if [ -z "$service" ] || [ "$service" = "all" ]; then
$COMPOSE_CMD up -d --build
else
$COMPOSE_CMD up -d --build --no-deps "$service"
fi
}
compose_down() {
local service="${1:-}"
if [ -z "$service" ] || [ "$service" = "all" ]; then
$COMPOSE_CMD down
else
$COMPOSE_CMD rm -sf "$service"
fi
}
compose_restart() {
local service="${1:-}"
if [ -z "$service" ] || [ "$service" = "all" ]; then
$COMPOSE_CMD restart
else
$COMPOSE_CMD restart "$service"
fi
}
compose_logs() {
$COMPOSE_CMD logs -f "$@"
}
compose_status() {
$COMPOSE_CMD ps
}
compose_shell() {
local service="${1:-api}"
if [ "$service" = "db" ]; then
docker exec -it "$DB_CONTAINER" psql -U postgres -d fluent
else
docker exec -it "${CONTAINER_PREFIX}$service" sh
fi
}
compose_exec_api() {
if ! docker ps --format '{{.Names}}' 2>/dev/null | grep -qx "$API_CONTAINER"; then
echo_error "API container ($API_CONTAINER) is not running. Run './fapi.sh up' first."
exit 1
fi
docker exec "$API_CONTAINER" "$@"
}
compose_clean() {
local service="${1:-all}"
if [ "$service" = "all" ]; then
$COMPOSE_CMD down -v
echo_success "All containers and volumes removed."
else
$COMPOSE_CMD rm -sf "$service"
echo_success "$service container removed."
fi
}
compose_fresh() {
$COMPOSE_CMD down -v --rmi local --remove-orphans
echo_success "Removed all containers, volumes, and images."
}
compose_build() {
local service="${1:-}"
if [ -z "$service" ] || [ "$service" = "all" ]; then
$COMPOSE_CMD build --no-cache
else
$COMPOSE_CMD build --no-cache "$service"
fi
}
compose_db_psql() {
docker exec -it "$DB_CONTAINER" psql -U postgres -d fluent
}
# ── Runtime dispatch helpers ───────────────────────────────────────────────────
exec_api() {
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_exec_api "$@"
else
compose_exec_api "$@"
fi
}
# ── Runtime mode display ──────────────────────────────────────────────────────
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
detect_runtime
echo "Runtime mode: $RUNTIME_MODE"
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
echo "Using native Podman pods"
else
echo "Using Docker Compose (${COMPOSE_CMD})"
fi
echo ""
cmd="${1:-help}"
shift || true
case "$cmd" in
up)
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_up "${1:-all}"
else
compose_up "${1:-}"
fi
;;
down)
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_down "${1:-all}"
else
compose_down "${1:-}"
fi
;;
restart)
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_restart "${1:-all}"
else
compose_restart "${1:-}"
fi
;;
logs)
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_logs "${1:-}"
else
compose_logs "$@"
fi
;;
status)
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_status
else
compose_status
fi
;;
shell)
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_shell "${1:-api}"
else
compose_shell "${1:-api}"
fi
;;
# ── Development commands ───────────────────────────────────────────────────
test)
exec_api npm run test "$@"
;;
lint)
exec_api npm run lint "$@"
;;
lint:fix)
exec_api npm run lint:fix "$@"
;;
format)
exec_api npm run format "$@"
;;
format:check)
exec_api npm run format:check "$@"
;;
typecheck)
exec_api npm run typecheck "$@"
;;
run)
exec_api npm run "$@"
;;
# ── Database commands ──────────────────────────────────────────────────────
db:migrate)
echo_running "Running fluent-api migrations..."
exec_api npx drizzle-kit migrate
;;
db:seed)
echo_running "Seeding organizations..."
exec_api npm run db:seed:org
echo_running "Seeding roles..."
exec_api npm run db:seed:roles
echo_running "Seeding RBAC data..."
exec_api npm run db:seed:rbac
echo_running "Seeding dev users..."
exec_api npm run db:seed:dev-users
echo_running "Seeding languages..."
exec_api npm run db:seed:languages
echo_running "Seeding books..."
exec_api npm run db:seed:books
echo_running "Seeding bibles..."
exec_api npm run db:seed:bibles
echo_running "Seeding bible texts..."
exec_api npm run db:seed:bible-texts
echo_success "All seeds complete."
;;
db:init)
echo_running "Full database initialization (migrations + seeds)..."
read -rp "This will run all migrations and seeds. Continue? [y/N] " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
exec_api npm run db:setup
echo_success "Database initialization complete."
else
echo "Aborted."
fi
;;
db:generate)
name="${1:?Usage: fapi.sh db:generate <name>}"
exec_api npx drizzle-kit generate --name "$name"
;;
db:studio)
echo_running "Running Drizzle Studio on host (requires local Node.js)..."
echo " Connects to DB via DATABASE_URL in .env (localhost:${DB_PORT})"
npx drizzle-kit studio
;;
db:psql)
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_db_psql
else
compose_db_psql
fi
;;
# ── Lifecycle commands ─────────────────────────────────────────────────────
clean)
echo_running "This will stop and remove containers$([ "${1:-all}" = "all" ] && echo " and volumes" || true)."
read -rp "Continue? [y/N] " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_clean "${1:-all}"
else
compose_clean "${1:-all}"
fi
else
echo "Aborted."
fi
;;
fresh)
echo_running "This will destroy ALL containers, volumes, and images."
read -rp "Continue? [y/N] " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_fresh
else
compose_fresh
fi
echo ""
echo_success "Clean slate. Run './fapi.sh up' to rebuild and start everything."
else
echo "Aborted."
fi
;;
build)
if [ "$RUNTIME_MODE" = "podman-pod" ]; then
podman_build "${1:-api}"
else
compose_build "${1:-}"
fi
;;
setup)
if [[ ! -f .env ]]; then
if [[ -f .env.example ]]; then
cp .env.example .env
echo "Created .env from .env.example"
else
touch .env
echo "Created empty .env file"
fi
else
echo ".env already exists, skipping."
fi
echo "Remember to fill in the DB URLs (BOOTSTRAP/MIGRATIONS/DATABASE) and BETTER_AUTH_SECRET in .env before running db:init."
;;
help|*)
cat <<'USAGE'
Usage: ./fapi.sh <command> [service] [args]
Operating modes:
Standalone ./fapi.sh up — own DB on 5432 + API + worker (safe alongside platform)
Service ./fapi.sh up api — API only; point DATABASE_URL at an existing DB
Ecosystem ./fluent.sh up — platform orchestrator owns the shared DB on 5432
Services: db | api | worker | (omit for all)
Container management:
up [service] Start services (default: all — DB on 5432, then API, then worker)
down [service] Stop and remove services (default: all)
restart [service] Restart services (default: all)
logs [service] Tail logs (default: all)
status Show container/pod status
shell [service] Open a shell (default: api; db opens psql)
Development (runs in API container):
test Run test suite
lint Run ESLint
lint:fix Run ESLint with auto-fix
format Format code with Prettier
format:check Check formatting with Prettier
typecheck Run TypeScript type checking
run <script> Run an npm script inside the API container
Database:
db:init Run migrations + all seeds (interactive confirmation)
db:migrate Run Drizzle migrations
db:seed Seed all data (org, roles, RBAC, dev users, languages, books, bibles, bible texts)
db:generate <name> Generate a new Drizzle migration
db:studio Launch Drizzle Studio on the host
db:psql Open psql session
Lifecycle:
clean [service] Remove containers and volumes (default: all)
fresh Nuke everything: containers, volumes, and images
build [service] Rebuild images without cache
setup Create .env from .env.example if missing
Environment variables:
DB_PORT Standalone DB host port (default: 5432)
API_PORT API service host port (default: 9999)
BOOTSTRAP_DATABASE_URL Superuser URL the container uses to self-provision (bootstrap)
MIGRATIONS_DATABASE_URL api_migrator URL for Drizzle migrations (DDL)
DATABASE_URL api_user runtime URL (least-privilege; set in .env to use platform DB)
USAGE
;;
esac
fi