-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.sh
71 lines (56 loc) · 1.3 KB
/
startup.sh
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
#!/bin/bash
# Strict error handling
set -euo pipefail
# Load environment variables
if [ -f .env ]; then
source .env
fi
# Validate required environment variables
if [ -z "${OPENAI_API_KEY}" ]; then
echo "Error: OPENAI_API_KEY environment variable is missing." >&2
exit 1
fi
if [ -z "${DATABASE_URL}" ]; then
echo "Error: DATABASE_URL environment variable is missing." >&2
exit 1
fi
# Define project variables
PROJECT_ROOT=$(pwd)
LOG_FILE="${PROJECT_ROOT}/logs/startup.log"
PID_FILE="${PROJECT_ROOT}/logs/app.pid"
# Utility functions
log_info() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - INFO: $*"
}
log_error() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - ERROR: $*" >&2
}
cleanup() {
if [ -f "${PID_FILE}" ]; then
kill $(cat "${PID_FILE}") 2>/dev/null
fi
rm -f "${PID_FILE}"
}
# Service management functions
start_database() {
log_info "Starting database..."
sqlite3 database.db < database/schema.sql
log_info "Database started."
}
start_backend() {
log_info "Starting backend server..."
uvicorn api.main:app --host 0.0.0.0 --port 8000 &
store_pid $!
log_info "Backend server started."
}
store_pid() {
echo "$1" > "${PID_FILE}"
}
# Trap signals
trap cleanup EXIT ERR
# Main execution
check_dependencies
start_database
start_backend
# Log startup completion
log_info "Startup complete."