-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
100 lines (86 loc) · 2.52 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
100 lines (86 loc) · 2.52 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
#!/bin/sh
set -e
# Function to log messages
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
# Function to handle signals
cleanup() {
log "Received termination signal, shutting down gracefully..."
if [ ! -z "$APP_PID" ]; then
kill -TERM "$APP_PID" 2>/dev/null || true
wait "$APP_PID" 2>/dev/null || true
fi
exit 0
}
# Set up signal handlers
trap cleanup TERM INT
# Ensure data directory exists
mkdir -p /app/data
# Check if database exists
DB_PATH="${CHATVAULT_DB_PATH:-/app/data/chat.db}"
# Function to run migrations
run_migrations() {
log "Running database migrations..."
./chatvault migrate || {
log "Failed to run migrations"
exit 1
}
log "Migrations completed successfully"
}
# Function to seed database
seed_database() {
log "Seeding database with sample data..."
./chatvault seed || {
log "Failed to seed database"
exit 1
}
log "Database seeded successfully"
}
# Handle different startup modes
case "$1" in
"migrate")
log "Migration mode: Running migrations only"
run_migrations
exit 0
;;
"seed")
log "Seed mode: Running migrations and seeding database"
run_migrations
seed_database
exit 0
;;
"server"|"")
log "Server mode: Starting ChatVault server"
# Check if database exists, if not run migrations
if [ ! -f "$DB_PATH" ]; then
log "Database not found, running initial setup..."
run_migrations
# Optionally seed with sample data if CHATVAULT_SEED is set
if [ "$CHATVAULT_SEED" = "true" ]; then
seed_database
fi
else
log "Database found at $DB_PATH"
# Check if we should always run migrations on startup
if [ "$CHATVAULT_AUTO_MIGRATE" = "true" ]; then
log "Auto-migration enabled, running migrations..."
run_migrations
fi
fi
log "Starting ChatVault server..."
# Start the server in the background so we can handle signals
./chatvault &
APP_PID=$!
# Wait for the server process
wait "$APP_PID"
;;
*)
log "Unknown command: $1"
log "Available commands: migrate, seed, server (default)"
log "Environment variables:"
log " CHATVAULT_SEED=true - Seed database on first run"
log " CHATVAULT_AUTO_MIGRATE=true - Run migrations on every startup"
exit 1
;;
esac