-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint-cloud.sh
More file actions
executable file
·232 lines (175 loc) · 7.05 KB
/
Copy pathentrypoint-cloud.sh
File metadata and controls
executable file
·232 lines (175 loc) · 7.05 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
#!/bin/sh
set -e
# =============================================================================
# Aether Bank - Production Service Entry Point
# =============================================================================
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/go/bin:/go/bin:/root/go/bin:/root/.local/share/corepack"
export NODE_ENV="${NODE_ENV:-production}"
# =============================================================================
# Configuration
# =============================================================================
DB_HOST="${DB_HOST:-db}"
DB_PORT="${DB_PORT:-5432}"
DB_USER="${DB_USER:-aether}"
DB_NAME="${DB_NAME:-etheria_account}"
DB_PASSWORD="${DB_PASSWORD:-${POSTGRES_PASSWORD:-password}}"
FRONTEND_PORT="${FRONTEND_PORT:-3000}"
API_PORT="${API_PORT:-8080}"
# =============================================================================
# Logging Functions
# =============================================================================
log_info() {
echo "[INFO] $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_success() {
echo "[✓] $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_warn() {
echo "[!] $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_error() {
echo "[X] $(date '+%Y-%m-%d %H:%M:%S') - $1" >&2
}
# =============================================================================
# Header Display
# =============================================================================
display_header() {
echo ""
echo "╔══════════════════════════════════════════════════════════════════════╗"
echo "║ Aether Bank System ║"
echo "║ Enterprise Account Management ║"
echo "║ Version 1.0.0-production ║"
echo "╚══════════════════════════════════════════════════════════════════════╝"
echo ""
log_info "Frontend: http://localhost:${FRONTEND_PORT}"
log_info "API: http://localhost:${API_PORT}"
log_info "Database: ${DB_HOST}:${DB_PORT}/${DB_NAME}"
echo ""
}
# =============================================================================
# Database Setup (External PostgreSQL)
# =============================================================================
wait_for_database() {
log_info "Waiting for database to be ready..."
MAX_RETRIES=60
RETRY_COUNT=0
while ! PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres -c '\q' 2>/dev/null; do
RETRY_COUNT=$((RETRY_COUNT + 1))
if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then
log_error "Database not available after ${MAX_RETRIES} attempts"
return 1
fi
log_info "Waiting for database... (${RETRY_COUNT}/${MAX_RETRIES})"
sleep 2
done
log_success "Database connected"
return 0
}
run_migrations() {
log_info "Running database migrations..."
PRISMA_DIR="/app/server/prisma"
if [ -d "$PRISMA_DIR" ]; then
cd "$PRISMA_DIR"
if [ -f "schema.prisma" ]; then
log_info "Generating Prisma client..."
PGPASSWORD="$DB_PASSWORD" npx prisma generate 2>/dev/null || log_warn "Prisma generate failed"
log_info "Running database migrations..."
PGPASSWORD="$DB_PASSWORD" DATABASE_URL="postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}" \
npx prisma db push --accept-data-loss 2>/dev/null || log_warn "Prisma db push failed"
fi
log_success "Database migrations complete"
else
log_warn "Prisma directory not found"
fi
}
# =============================================================================
# Service Starters
# =============================================================================
start_frontend() {
log_info "Starting Next.js (production mode) on port ${FRONTEND_PORT}..."
cd /app/app
export PORT="$FRONTEND_PORT"
export NEXT_PUBLIC_BASE_PATH=""
export NEXT_TELEMETRY_DISABLED=1
node server.js &
NEXT_PID=$!
echo "$NEXT_PID" > /tmp/next.pid
log_info "Next.js started (PID: $NEXT_PID)"
sleep 3
if kill -0 "$NEXT_PID" 2>/dev/null; then
log_success "Next.js is ready"
else
log_error "Next.js failed to start"
return 1
fi
return 0
}
start_api() {
log_info "Starting Go API server on port ${API_PORT}..."
cd /app
export SERVER_PORT="$API_PORT"
export ENVIRONMENT="production"
./server/etheriatimes-api &
API_PID=$!
echo "$API_PID" > /tmp/api.pid
log_info "Go API server started (PID: $API_PID)"
sleep 3
if kill -0 "$API_PID" 2>/dev/null; then
log_success "Go API server is ready"
else
log_error "Go API server failed to start"
return 1
fi
return 0
}
# =============================================================================
# Service Monitor
# =============================================================================
monitor_services() {
log_success "All services started successfully!"
echo ""
echo "══════════════════════════════════════════════════════════════════════"
echo " Services are running. Press Ctrl+C to stop."
echo "══════════════════════════════════════════════════════════════════════"
echo ""
while true; do
if ! kill -0 "$NEXT_PID" 2>/dev/null || ! kill -0 "$API_PID" 2>/dev/null; then
log_error "A service has stopped unexpectedly!"
break
fi
sleep 5
done
}
# =============================================================================
# Cleanup Handler
# =============================================================================
cleanup() {
echo ""
log_info "Stopping services..."
if [ -f /tmp/next.pid ]; then
kill "$(cat /tmp/next.pid)" 2>/dev/null || true
rm -f /tmp/next.pid
fi
if [ -f /tmp/api.pid ]; then
kill "$(cat /tmp/api.pid)" 2>/dev/null || true
rm -f /tmp/api.pid
fi
log_info "All services stopped"
exit 0
}
# =============================================================================
# Main Execution
# =============================================================================
main() {
display_header
if wait_for_database; then
run_migrations
else
log_error "Database not available, starting without migrations..."
fi
start_frontend || log_error "Frontend failed to start"
start_api || log_error "API failed to start"
monitor_services
}
trap cleanup SIGINT SIGTERM
main