-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathapp.sh
More file actions
executable file
·1368 lines (1180 loc) · 45.1 KB
/
app.sh
File metadata and controls
executable file
·1368 lines (1180 loc) · 45.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
# ═══════════════════════════════════════════════════════════════════
# 4DPocket — Application Management Script
#
# Single entry point for all development and deployment operations.
# Run ./app.sh help for full documentation.
# ═══════════════════════════════════════════════════════════════════
# ─── Colors & Helpers ────────────────────────────────────────────
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m'
info() { echo -e "${CYAN}==> $1${NC}"; }
success() { echo -e "${GREEN}==> $1${NC}"; }
warn() { echo -e "${YELLOW}==> $1${NC}"; }
error() { echo -e "${RED}==> ERROR: $1${NC}" >&2; }
step() { echo -e " ${GREEN}✓${NC} $1"; }
fail() { echo -e " ${RED}✗${NC} $1"; }
# ─── Constants ───────────────────────────────────────────────────
APP_NAME="4DPocket"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VERSION=$(grep '^version' "$SCRIPT_DIR/pyproject.toml" | head -1 | sed 's/.*"\(.*\)"/\1/')
FRONTEND_PORT=5173
PID_DIR="$SCRIPT_DIR/.pids"
LOG_DIR="$SCRIPT_DIR/logs"
BACKEND_PID_FILE="$PID_DIR/backend.pid"
FRONTEND_PID_FILE="$PID_DIR/frontend.pid"
WORKER_PID_FILE="$PID_DIR/worker.pid"
BACKEND_LOG="$LOG_DIR/backend.log"
FRONTEND_LOG="$LOG_DIR/frontend.log"
WORKER_LOG="$LOG_DIR/worker.log"
# Docker container names
POSTGRES_CONTAINER="4dp-postgres"
MEILI_CONTAINER="4dp-meili"
CHROMA_CONTAINER="4dp-chromadb"
OLLAMA_CONTAINER="4dp-ollama"
# Default PostgreSQL credentials (overridable via env)
PG_USER="${PG_USER:-4dp}"
PG_PASSWORD="${PG_PASSWORD:-4dp}"
PG_DB="${PG_DB:-4dpocket}"
PG_PORT="${PG_PORT:-5432}"
# ─── Environment ─────────────────────────────────────────────────
detect_os() {
if [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
OS="linux"
if [ -f /etc/os-release ]; then
# shellcheck disable=SC1091
. /etc/os-release
DISTRO="$ID"
else
DISTRO="unknown"
fi
else
OS="unknown"
fi
}
get_local_ip() {
if [[ "$OSTYPE" == "darwin"* ]]; then
ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || echo "localhost"
else
hostname -I 2>/dev/null | awk '{print $1}' || echo "localhost"
fi
}
load_env() {
if [ -f "$SCRIPT_DIR/.env" ]; then
set -a
# shellcheck disable=SC1091
source "$SCRIPT_DIR/.env"
set +a
fi
}
check_command() {
command -v "$1" &>/dev/null
}
get_backend_port() {
echo "${FDP_SERVER__PORT:-4040}"
}
# ─── Prerequisite Checking ───────────────────────────────────────
check_prerequisites() {
local missing=0
# Python 3.12+
if check_command python3; then
local py_version
py_version=$(python3 -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
if python3 -c 'import sys; exit(0 if sys.version_info >= (3, 12) else 1)' 2>/dev/null; then
step "Python $py_version"
else
fail "Python $py_version (need 3.12+)"
missing=1
fi
else
fail "Python not found"
missing=1
fi
# uv
if check_command uv; then
step "uv $(uv --version 2>/dev/null | awk '{print $2}')"
else
fail "uv not found"
echo -e " Install: ${YELLOW}curl -LsSf https://astral.sh/uv/install.sh | sh${NC}"
missing=1
fi
# Node.js
if check_command node; then
step "Node.js $(node --version)"
else
fail "Node.js not found"
missing=1
fi
# pnpm
if check_command pnpm; then
step "pnpm $(pnpm --version)"
else
fail "pnpm not found"
echo -e " Install: ${YELLOW}npm install -g pnpm${NC}"
missing=1
fi
# Git
if check_command git; then
step "git $(git --version | awk '{print $3}')"
else
fail "git not found"
missing=1
fi
# Optional tools
if check_command docker; then
step "Docker $(docker --version 2>/dev/null | awk '{print $3}' | tr -d ',')"
else
echo -e " ${DIM}-${NC} Docker ${DIM}(optional — for PostgreSQL, Meilisearch, Ollama)${NC}"
fi
if check_command tesseract; then
step "Tesseract OCR"
else
echo -e " ${DIM}-${NC} Tesseract OCR ${DIM}(optional — for image text extraction)${NC}"
fi
return $missing
}
install_deps_for_os() {
detect_os
info "Installing system dependencies for $OS..."
case "$OS" in
macos)
if ! check_command brew; then
error "Homebrew not found. Install from https://brew.sh"
return 1
fi
check_command python3 || brew install [email protected]
check_command uv || { curl -LsSf https://astral.sh/uv/install.sh | sh; }
check_command node || brew install node
check_command pnpm || npm install -g pnpm
;;
linux)
case "${DISTRO:-unknown}" in
ubuntu|debian)
sudo apt-get update -qq
sudo apt-get install -y python3 python3-dev python3-venv curl git
check_command uv || { curl -LsSf https://astral.sh/uv/install.sh | sh; }
check_command node || { curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash - && sudo apt-get install -y nodejs; }
check_command pnpm || npm install -g pnpm
;;
fedora|rhel|centos)
sudo dnf install -y python3 python3-devel curl git nodejs
check_command uv || { curl -LsSf https://astral.sh/uv/install.sh | sh; }
check_command pnpm || npm install -g pnpm
;;
arch|manjaro)
sudo pacman -S --needed python curl git nodejs npm
check_command uv || { curl -LsSf https://astral.sh/uv/install.sh | sh; }
check_command pnpm || npm install -g pnpm
;;
*)
error "Unsupported Linux distribution: ${DISTRO:-unknown}"
echo "Install manually: Python 3.12+, uv, Node.js 22+, pnpm, git"
return 1
;;
esac
;;
*)
error "Unsupported OS: $OSTYPE"
return 1
;;
esac
success "System dependencies installed."
}
# ─── Process Management ─────────────────────────────────────────
is_running() {
local pid_file="$1"
if [ -f "$pid_file" ]; then
local pid
pid=$(cat "$pid_file")
if kill -0 "$pid" 2>/dev/null; then
return 0
fi
rm -f "$pid_file"
fi
return 1
}
kill_by_pid_file() {
local pid_file="$1"
local name="$2"
if [ -f "$pid_file" ]; then
local pid
pid=$(cat "$pid_file")
if kill -0 "$pid" 2>/dev/null; then
kill "$pid" 2>/dev/null || true
# Wait up to 5s for graceful shutdown
local i=0
while kill -0 "$pid" 2>/dev/null && [ $i -lt 50 ]; do
sleep 0.1
i=$((i + 1))
done
# Force kill if still alive
if kill -0 "$pid" 2>/dev/null; then
kill -9 "$pid" 2>/dev/null || true
fi
step "$name stopped (was PID: $pid)"
fi
rm -f "$pid_file"
fi
}
kill_port() {
local port="$1"
local pids
pids=$(lsof -ti:"$port" 2>/dev/null || true)
if [ -n "$pids" ]; then
echo "$pids" | xargs kill 2>/dev/null || true
sleep 1
fi
}
# ─── Start Functions ─────────────────────────────────────────────
start_backend() {
local mode="${1:-dev}"
local port
port=$(get_backend_port)
if is_running "$BACKEND_PID_FILE"; then
warn "Backend already running (PID: $(cat "$BACKEND_PID_FILE"))"
return 0
fi
mkdir -p "$PID_DIR" "$LOG_DIR"
kill_port "$port"
# Ensure backend deps
if [ ! -d "$SCRIPT_DIR/.venv" ]; then
info "Installing backend dependencies..."
cd "$SCRIPT_DIR"
uv sync --all-extras --quiet
step "Backend dependencies installed"
fi
info "Starting backend (port $port, mode: $mode)..."
cd "$SCRIPT_DIR"
if [ "$mode" = "dev" ]; then
uv run uvicorn fourdpocket.main:app \
--host 0.0.0.0 --port "$port" --reload \
> "$BACKEND_LOG" 2>&1 &
else
uv run uvicorn fourdpocket.main:app \
--host 0.0.0.0 --port "$port" \
> "$BACKEND_LOG" 2>&1 &
fi
local pid=$!
echo "$pid" > "$BACKEND_PID_FILE"
sleep 2
if kill -0 "$pid" 2>/dev/null; then
step "Backend started (PID: $pid, port: $port)"
else
fail "Backend failed to start"
echo -e " ${DIM}Check: tail -f $BACKEND_LOG${NC}"
return 1
fi
}
start_frontend() {
local mode="${1:-dev}"
if [ "$mode" = "prod" ]; then
# Production: backend serves from frontend/dist
if [ ! -d "$SCRIPT_DIR/frontend/dist" ]; then
warn "frontend/dist not found. Building..."
do_build
fi
step "Frontend served by backend from frontend/dist/"
return 0
fi
if is_running "$FRONTEND_PID_FILE"; then
warn "Frontend already running (PID: $(cat "$FRONTEND_PID_FILE"))"
return 0
fi
mkdir -p "$PID_DIR" "$LOG_DIR"
kill_port "$FRONTEND_PORT"
# Ensure frontend deps
if [ ! -d "$SCRIPT_DIR/frontend/node_modules" ]; then
info "Installing frontend dependencies..."
cd "$SCRIPT_DIR/frontend"
pnpm install --silent
step "Frontend dependencies installed"
fi
info "Starting frontend dev server (port $FRONTEND_PORT)..."
cd "$SCRIPT_DIR/frontend"
pnpm dev --host 0.0.0.0 > "$FRONTEND_LOG" 2>&1 &
local pid=$!
echo "$pid" > "$FRONTEND_PID_FILE"
cd "$SCRIPT_DIR"
sleep 3
if kill -0 "$pid" 2>/dev/null; then
step "Frontend started (PID: $pid, port: $FRONTEND_PORT)"
else
fail "Frontend failed to start"
echo -e " ${DIM}Check: tail -f $FRONTEND_LOG${NC}"
return 1
fi
}
kill_stale_workers() {
# Kill Huey worker processes NOT tracked by our PID file.
# Prevents zombies (started outside app.sh or from prior crashes) from
# stealing tasks and poisoning the SQLite task queue with stale registries.
local tracked_pid=""
if [ -f "$WORKER_PID_FILE" ]; then
tracked_pid=$(cat "$WORKER_PID_FILE" 2>/dev/null || echo "")
fi
local stale
stale=$(pgrep -f "fourdpocket.workers.huey_worker" 2>/dev/null | grep -v "^${tracked_pid:-__none__}$" || true)
if [ -n "$stale" ]; then
warn "Found stale Huey worker process(es): $(echo "$stale" | tr '\n' ' ')"
echo "$stale" | xargs kill 2>/dev/null || true
sleep 1
local survivors
survivors=$(pgrep -f "fourdpocket.workers.huey_worker" 2>/dev/null | grep -v "^${tracked_pid:-__none__}$" || true)
[ -n "$survivors" ] && echo "$survivors" | xargs kill -9 2>/dev/null || true
step "Cleaned up stale workers"
fi
}
start_worker() {
if is_running "$WORKER_PID_FILE"; then
warn "Worker already running (PID: $(cat "$WORKER_PID_FILE"))"
kill_stale_workers
return 0
fi
kill_stale_workers
mkdir -p "$PID_DIR" "$LOG_DIR"
info "Starting Huey background worker..."
cd "$SCRIPT_DIR"
uv run python -m fourdpocket.workers.huey_worker \
--workers "${HUEY_WORKERS:-2}" --worker-type thread \
> "$WORKER_LOG" 2>&1 &
local pid=$!
echo "$pid" > "$WORKER_PID_FILE"
sleep 2
if kill -0 "$pid" 2>/dev/null; then
step "Worker started (PID: $pid, ${HUEY_WORKERS:-2} threads)"
else
fail "Worker failed to start"
echo -e " ${DIM}Check: tail -f $WORKER_LOG${NC}"
return 1
fi
}
# ─── Stop Functions ──────────────────────────────────────────────
stop_backend() { kill_by_pid_file "$BACKEND_PID_FILE" "Backend"; }
stop_frontend() { kill_by_pid_file "$FRONTEND_PID_FILE" "Frontend"; }
stop_worker() { kill_by_pid_file "$WORKER_PID_FILE" "Worker"; }
stop_all() {
info "Stopping all services..."
stop_worker
stop_frontend
stop_backend
success "All services stopped."
}
# ─── Docker Service Management ──────────────────────────────────
docker_ensure() {
if ! check_command docker; then
error "Docker is required but not installed."
echo -e " Install: ${YELLOW}https://docs.docker.com/get-docker/${NC}"
return 1
fi
if ! docker info &>/dev/null 2>&1; then
error "Docker daemon is not running."
return 1
fi
}
service_is_running() {
docker ps --filter "name=^${1}$" --format '{{.Names}}' 2>/dev/null | grep -q "^${1}$"
}
service_exists() {
docker ps -a --filter "name=^${1}$" --format '{{.Names}}' 2>/dev/null | grep -q "^${1}$"
}
start_postgres() {
docker_ensure || return 1
if service_is_running "$POSTGRES_CONTAINER"; then
step "PostgreSQL already running"
return 0
fi
info "Starting PostgreSQL..."
if service_exists "$POSTGRES_CONTAINER"; then
docker start "$POSTGRES_CONTAINER" >/dev/null
else
docker run -d --name "$POSTGRES_CONTAINER" \
-p "${PG_PORT}:5432" \
-e POSTGRES_USER="$PG_USER" \
-e POSTGRES_PASSWORD="$PG_PASSWORD" \
-e POSTGRES_DB="$PG_DB" \
-v 4dp-postgres:/var/lib/postgresql/data \
postgres:16-alpine >/dev/null
fi
# Wait for ready (up to 30s)
local i=0
while ! docker exec "$POSTGRES_CONTAINER" pg_isready -U "$PG_USER" -d "$PG_DB" &>/dev/null && [ $i -lt 30 ]; do
sleep 1
i=$((i + 1))
done
if docker exec "$POSTGRES_CONTAINER" pg_isready -U "$PG_USER" -d "$PG_DB" &>/dev/null; then
step "PostgreSQL ready (port: $PG_PORT, db: $PG_DB)"
else
fail "PostgreSQL failed to start within 30s"
return 1
fi
}
start_meilisearch() {
docker_ensure || return 1
local meili_key="${FDP_SEARCH__MEILI_MASTER_KEY:-devkey123}"
if service_is_running "$MEILI_CONTAINER"; then
step "Meilisearch already running"
return 0
fi
info "Starting Meilisearch..."
if service_exists "$MEILI_CONTAINER"; then
docker start "$MEILI_CONTAINER" >/dev/null
else
docker run -d --name "$MEILI_CONTAINER" \
-p 7700:7700 \
-e MEILI_MASTER_KEY="$meili_key" \
-v 4dp-meili:/meili_data \
getmeili/meilisearch:v1.12 >/dev/null
fi
# Wait for healthy (up to 30s)
local i=0
while ! curl -sf http://localhost:7700/health &>/dev/null && [ $i -lt 30 ]; do
sleep 1
i=$((i + 1))
done
if curl -sf http://localhost:7700/health &>/dev/null; then
step "Meilisearch ready (port: 7700)"
else
fail "Meilisearch failed to start within 30s"
return 1
fi
}
start_chromadb() {
docker_ensure || return 1
if service_is_running "$CHROMA_CONTAINER"; then
step "ChromaDB already running"
return 0
fi
info "Starting ChromaDB..."
if service_exists "$CHROMA_CONTAINER"; then
docker start "$CHROMA_CONTAINER" >/dev/null
else
docker run -d --name "$CHROMA_CONTAINER" \
-p 8000:8000 \
-v 4dp-chroma:/chroma/chroma \
chromadb/chroma:latest >/dev/null
fi
sleep 3
if service_is_running "$CHROMA_CONTAINER"; then
step "ChromaDB ready (port: 8000)"
else
fail "ChromaDB failed to start"
return 1
fi
}
start_ollama() {
docker_ensure || return 1
# Prefer native Ollama if available
if check_command ollama; then
if curl -sf http://localhost:11434/api/tags &>/dev/null; then
step "Ollama running natively (skipping Docker)"
return 0
fi
fi
if service_is_running "$OLLAMA_CONTAINER"; then
step "Ollama already running"
return 0
fi
info "Starting Ollama..."
if service_exists "$OLLAMA_CONTAINER"; then
docker start "$OLLAMA_CONTAINER" >/dev/null
else
docker run -d --name "$OLLAMA_CONTAINER" \
-p 11434:11434 \
-v 4dp-ollama:/root/.ollama \
ollama/ollama:latest >/dev/null
fi
sleep 3
if service_is_running "$OLLAMA_CONTAINER"; then
step "Ollama ready (port: 11434)"
else
fail "Ollama failed to start"
return 1
fi
}
stop_docker_service() {
local name="$1"
if service_is_running "$name"; then
docker stop "$name" >/dev/null 2>&1
step "$name stopped"
else
echo -e " ${DIM}○${NC} $name (not running)"
fi
}
remove_docker_service() {
local name="$1"
if service_exists "$name"; then
docker rm -f "$name" >/dev/null 2>&1
step "$name removed"
fi
}
services_up() {
local services=("$@")
if [ ${#services[@]} -eq 0 ]; then
# Auto-detect from .env
load_env
local db_url="${FDP_DATABASE__URL:-sqlite:///./data/4dpocket.db}"
local search="${FDP_SEARCH__BACKEND:-sqlite}"
local ai="${FDP_AI__CHAT_PROVIDER:-ollama}"
if [[ "$db_url" == postgresql* ]]; then
start_postgres || true
fi
if [ "$search" = "meilisearch" ]; then
start_meilisearch || true
fi
if [ "$ai" = "ollama" ]; then
start_ollama || true
fi
return
fi
for svc in "${services[@]}"; do
case "$svc" in
postgres|pg|postgresql) start_postgres ;;
meilisearch|meili) start_meilisearch ;;
chromadb|chroma) start_chromadb ;;
ollama) start_ollama ;;
all)
start_postgres
start_meilisearch
start_chromadb
start_ollama
;;
*) warn "Unknown service: $svc (use: postgres, meilisearch, chromadb, ollama, all)" ;;
esac
done
}
services_down() {
local services=("$@")
if [ ${#services[@]} -eq 0 ]; then
services=(all)
fi
for svc in "${services[@]}"; do
case "$svc" in
postgres|pg|postgresql) stop_docker_service "$POSTGRES_CONTAINER" ;;
meilisearch|meili) stop_docker_service "$MEILI_CONTAINER" ;;
chromadb|chroma) stop_docker_service "$CHROMA_CONTAINER" ;;
ollama) stop_docker_service "$OLLAMA_CONTAINER" ;;
all)
stop_docker_service "$POSTGRES_CONTAINER"
stop_docker_service "$MEILI_CONTAINER"
stop_docker_service "$CHROMA_CONTAINER"
stop_docker_service "$OLLAMA_CONTAINER"
;;
esac
done
}
services_status() {
echo -e "${BOLD}Docker Services:${NC}"
local containers=("$POSTGRES_CONTAINER" "$MEILI_CONTAINER" "$CHROMA_CONTAINER" "$OLLAMA_CONTAINER")
local labels=("PostgreSQL" "Meilisearch" "ChromaDB" "Ollama")
for i in "${!containers[@]}"; do
local name="${containers[$i]}"
local label="${labels[$i]}"
if service_is_running "$name"; then
local port_info
port_info=$(docker port "$name" 2>/dev/null | head -1 | sed 's/.*://' || echo "?")
echo -e " ${GREEN}●${NC} $label ${DIM}($name, port: $port_info)${NC}"
elif service_exists "$name"; then
echo -e " ${YELLOW}●${NC} $label ${DIM}($name, stopped)${NC}"
else
echo -e " ${DIM}○${NC} $label ${DIM}(not created)${NC}"
fi
done
}
# ─── Setup ───────────────────────────────────────────────────────
do_setup() {
echo -e "${BOLD}${CYAN}╔══════════════════════════════════════════╗${NC}"
echo -e "${BOLD}${CYAN}║ $APP_NAME v$VERSION — Setup ║${NC}"
echo -e "${BOLD}${CYAN}╚══════════════════════════════════════════╝${NC}"
echo ""
local install_system=false
# Parse setup flags
for arg in "$@"; do
case "$arg" in
--deps|--install|-d) install_system=true ;;
esac
done
detect_os
info "Detected: $OS"
# Optionally install system deps
if $install_system; then
install_deps_for_os
echo ""
fi
# Check prerequisites
info "Checking prerequisites..."
if ! check_prerequisites; then
echo ""
error "Missing required tools. Run: ./app.sh setup --deps"
return 1
fi
echo ""
# Create directories
mkdir -p "$PID_DIR" "$LOG_DIR" "$SCRIPT_DIR/data"
step "Directories ready (logs/, data/)"
# Backend dependencies
info "Installing backend dependencies..."
cd "$SCRIPT_DIR"
uv sync --all-extras --quiet
step "Backend dependencies installed"
# Frontend dependencies
info "Installing frontend dependencies..."
cd "$SCRIPT_DIR/frontend"
pnpm install --silent
step "Frontend dependencies installed"
# Build frontend for production serving
info "Building frontend..."
cd "$SCRIPT_DIR/frontend"
pnpm build 2>&1 | tail -5
step "Frontend built (frontend/dist/)"
cd "$SCRIPT_DIR"
# Chrome extension (optional, but part of a full setup)
if [ -d "$SCRIPT_DIR/extension" ]; then
info "Installing extension dependencies..."
cd "$SCRIPT_DIR/extension"
pnpm install --silent
step "Extension dependencies installed"
info "Building Chrome extension..."
pnpm build 2>&1 | tail -5
step "Extension built (extension/dist/chrome-mv3/)"
cd "$SCRIPT_DIR"
fi
# Create .env if missing
if [ ! -f "$SCRIPT_DIR/.env" ]; then
cp "$SCRIPT_DIR/.env.example" "$SCRIPT_DIR/.env"
step "Created .env from .env.example"
echo ""
warn "Edit .env to configure your database, AI provider, etc."
else
step ".env already exists"
fi
echo ""
success "Setup complete!"
echo ""
echo -e " Quick start: ${YELLOW}./app.sh start --sqlite${NC} ${DIM}(zero-config, no Docker)${NC}"
echo -e " With Postgres: ${YELLOW}./app.sh start --postgres${NC} ${DIM}(auto-starts Docker services)${NC}"
echo -e " Full stack: ${YELLOW}./app.sh start --full${NC} ${DIM}(PostgreSQL + Meili + Chroma + Ollama)${NC}"
echo -e " From .env: ${YELLOW}./app.sh start${NC} ${DIM}(uses your .env configuration)${NC}"
}
# ─── Start All ───────────────────────────────────────────────────
do_start() {
local mode="dev"
local start_backend=true
local start_frontend=true
local start_worker=true
local profile=""
local custom_port=""
# Parse options
while [ $# -gt 0 ]; do
case "$1" in
--backend|-b) start_frontend=false; start_worker=false ;;
--frontend|-f) start_backend=false; start_worker=false ;;
--worker|-w) start_backend=false; start_frontend=false ;;
--no-worker) start_worker=false ;;
--no-frontend) start_frontend=false ;;
--dev) mode="dev" ;;
--prod) mode="prod" ;;
--sqlite) profile="sqlite" ;;
--postgres) profile="postgres" ;;
--full) profile="full" ;;
--port=*) custom_port="${1#*=}" ;;
*) warn "Unknown option: $1" ;;
esac
shift
done
load_env
# Apply custom port
if [ -n "$custom_port" ]; then
export FDP_SERVER__PORT="$custom_port"
fi
local port
port=$(get_backend_port)
echo -e "${BOLD}${CYAN}╔══════════════════════════════════════════╗${NC}"
echo -e "${BOLD}${CYAN}║ $APP_NAME v$VERSION ║${NC}"
echo -e "${BOLD}${CYAN}╚══════════════════════════════════════════╝${NC}"
echo ""
# Apply profile (sets env vars and starts Docker services)
case "$profile" in
sqlite)
export FDP_DATABASE__URL="sqlite:///./data/4dpocket.db"
export FDP_SEARCH__BACKEND="sqlite"
info "Profile: SQLite (zero-config)"
;;
postgres)
start_postgres || return 1
start_meilisearch || return 1
export FDP_DATABASE__URL="${FDP_DATABASE__URL:-postgresql://$PG_USER:$PG_PASSWORD@localhost:$PG_PORT/$PG_DB}"
export FDP_SEARCH__BACKEND="${FDP_SEARCH__BACKEND:-meilisearch}"
export FDP_SEARCH__MEILI_URL="${FDP_SEARCH__MEILI_URL:-http://localhost:7700}"
export FDP_SEARCH__MEILI_MASTER_KEY="${FDP_SEARCH__MEILI_MASTER_KEY:-devkey123}"
info "Profile: PostgreSQL + Meilisearch"
;;
full)
start_postgres || return 1
start_meilisearch || return 1
start_chromadb || return 1
start_ollama || return 1
export FDP_DATABASE__URL="${FDP_DATABASE__URL:-postgresql://$PG_USER:$PG_PASSWORD@localhost:$PG_PORT/$PG_DB}"
export FDP_SEARCH__BACKEND="${FDP_SEARCH__BACKEND:-meilisearch}"
export FDP_SEARCH__MEILI_URL="${FDP_SEARCH__MEILI_URL:-http://localhost:7700}"
export FDP_SEARCH__MEILI_MASTER_KEY="${FDP_SEARCH__MEILI_MASTER_KEY:-devkey123}"
export FDP_AI__CHAT_PROVIDER="ollama"
export FDP_AI__OLLAMA_URL="http://localhost:11434"
info "Profile: Full stack (PostgreSQL + Meilisearch + ChromaDB + Ollama)"
;;
"")
# No profile — auto-start Docker services that .env requires
local db_url="${FDP_DATABASE__URL:-sqlite:///./data/4dpocket.db}"
if [[ "$db_url" == postgresql* ]] && check_command docker; then
start_postgres || true
fi
if [ "${FDP_SEARCH__BACKEND:-sqlite}" = "meilisearch" ] && check_command docker; then
start_meilisearch || true
fi
;;
esac
echo ""
# Start application services
if $start_backend; then
start_backend "$mode" || return 1
fi
if $start_frontend; then
start_frontend "$mode" || return 1
fi
if $start_worker; then
start_worker || return 1
fi
# Print access info
local local_ip
local_ip=$(get_local_ip)
echo ""
success "$APP_NAME is running!"
echo ""
if [ "$mode" = "dev" ] && $start_frontend; then
echo -e " ${BOLD}Frontend:${NC} http://localhost:$FRONTEND_PORT"
echo -e " ${BOLD}Backend:${NC} http://localhost:$port"
echo -e " ${BOLD}API Docs:${NC} http://localhost:$port/docs"
echo -e " ${BOLD}Network:${NC} http://$local_ip:$FRONTEND_PORT"
else
echo -e " ${BOLD}App:${NC} http://localhost:$port"
echo -e " ${BOLD}API Docs:${NC} http://localhost:$port/docs"
echo -e " ${BOLD}Network:${NC} http://$local_ip:$port"
fi
echo ""
echo -e " ${DIM}Logs: ./app.sh logs [backend|frontend|worker|all]${NC}"
echo -e " ${DIM}Status: ./app.sh status${NC}"
echo -e " ${DIM}Stop: ./app.sh stop${NC}"
}
# ─── Status ──────────────────────────────────────────────────────
do_status() {
load_env
local port
port=$(get_backend_port)
echo -e "${BOLD}$APP_NAME v$VERSION${NC}"
echo ""
echo -e "${BOLD}Application:${NC}"
if is_running "$BACKEND_PID_FILE"; then
echo -e " ${GREEN}●${NC} Backend PID: $(cat "$BACKEND_PID_FILE") Port: $port"
else
echo -e " ${RED}●${NC} Backend not running"
fi
if is_running "$FRONTEND_PID_FILE"; then
echo -e " ${GREEN}●${NC} Frontend PID: $(cat "$FRONTEND_PID_FILE") Port: $FRONTEND_PORT"
else
echo -e " ${DIM}○${NC} Frontend not running"
fi
if is_running "$WORKER_PID_FILE"; then
echo -e " ${GREEN}●${NC} Worker PID: $(cat "$WORKER_PID_FILE")"
else
echo -e " ${DIM}○${NC} Worker not running"
fi
echo ""
if check_command docker && docker info &>/dev/null 2>&1; then
services_status
echo ""
fi
echo -e "${BOLD}Configuration (.env):${NC}"
local db_url="${FDP_DATABASE__URL:-sqlite:///./data/4dpocket.db}"
if [[ "$db_url" == postgresql* ]]; then
echo -e " Database: PostgreSQL"
else
echo -e " Database: SQLite"
fi
echo -e " Search: ${FDP_SEARCH__BACKEND:-sqlite}"
echo -e " AI: ${FDP_AI__CHAT_PROVIDER:-ollama}"
echo -e " Auth: ${FDP_AUTH__MODE:-single}"
}
# ─── Logs ────────────────────────────────────────────────────────
do_logs() {
local service="${1:-backend}"
case "$service" in
backend|b)
[ -f "$BACKEND_LOG" ] && tail -f "$BACKEND_LOG" || warn "No backend log at $BACKEND_LOG"
;;
frontend|f)
[ -f "$FRONTEND_LOG" ] && tail -f "$FRONTEND_LOG" || warn "No frontend log at $FRONTEND_LOG"
;;
worker|w)
[ -f "$WORKER_LOG" ] && tail -f "$WORKER_LOG" || warn "No worker log at $WORKER_LOG"
;;
all|a)
tail -f "$BACKEND_LOG" "$FRONTEND_LOG" "$WORKER_LOG" 2>/dev/null || warn "No logs found"
;;
*)
warn "Unknown log: $service (use: backend, frontend, worker, all)"
;;
esac
}
# ─── Build ───────────────────────────────────────────────────────
do_build() {
# Parse options — by default build both frontend + extension.
local build_frontend=true
local build_extension=true
for arg in "$@"; do
case "$arg" in
--frontend|-f) build_extension=false ;;
--extension|-e) build_frontend=false ;;
--all|-a) build_frontend=true; build_extension=true ;;
esac
done
if $build_frontend; then
info "Building frontend..."
cd "$SCRIPT_DIR/frontend"
if [ ! -d "node_modules" ]; then
info "Installing frontend dependencies first..."
pnpm install --silent
fi
pnpm build