1#!/bin/bash
2# thank you claude senpai *pleading emoji*
3set -e
4
5RED='\033[0;31m'
6GREEN='\033[0;32m'
7YELLOW='\033[1;33m'
8BLUE='\033[0;34m'
9NC='\033[0m' # No Color
10
11print_status() {
12 echo -e "${BLUE}==>${NC} $1"
13}
14
15print_success() {
16 echo -e "${GREEN}✓${NC} $1"
17}
18
19print_error() {
20 echo -e "${RED}✗${NC} $1"
21}
22
23print_warning() {
24 echo -e "${YELLOW}!${NC} $1"
25}
26
27cleanup() {
28 print_warning "Shutting down services..."
29
30 if [ ! -z "$DATABASE_PID" ]; then
31 kill $DATABASE_PID 2>/dev/null || true
32 fi
33 if [ ! -z "$FIREHOSE_PID" ]; then
34 kill $FIREHOSE_PID 2>/dev/null || true
35 fi
36 if [ ! -z "$INDEXER_PID" ]; then
37 kill $INDEXER_PID 2>/dev/null || true
38 fi
39 if [ ! -z "$CDN_PID" ]; then
40 kill $CDN_PID 2>/dev/null || true
41 fi
42
43 print_success "Services stopped"
44 exit 0
45}
46
47trap cleanup SIGINT SIGTERM
48
49print_status "Starting Docker Compose services (Zookeeper, Kafka, Cassandra)..."
50docker compose up -d
51
52if [ $? -ne 0 ]; then
53 print_error "Failed to start Docker Compose services"
54 exit 1
55fi
56
57print_success "Docker Compose services started"
58
59print_status "Waiting for Cassandra to be ready..."
60./scripts/setup-cassandra.sh
61
62if [ $? -ne 0 ]; then
63 print_error "Failed to setup Cassandra"
64 exit 1
65fi
66
67print_success "Cassandra is ready and initialized"
68
69print_status "Running database migrations..."
70just migrate-up
71
72if [ $? -ne 0 ]; then
73 print_error "Failed to run migrations"
74 exit 1
75fi
76
77print_success "Migrations completed"
78
79print_status "Starting database server on :9090..."
80go run ./cmd/database &
81DATABASE_PID=$!
82sleep 3
83
84if ! ps -p $DATABASE_PID > /dev/null; then
85 print_error "Database server failed to start"
86 exit 1
87fi
88
89print_success "Database server running (PID: $DATABASE_PID)"
90
91print_status "Starting firehose (connecting to Bluesky network)..."
92go run ./cmd/bus/firehose --desired-collections "app.vylet.*" --websocket-host "wss://bsky.network" --output-topic firehose-events-prod &
93FIREHOSE_PID=$!
94sleep 3
95
96if ! ps -p $FIREHOSE_PID > /dev/null; then
97 print_error "Firehose failed to start"
98 cleanup
99 exit 1
100fi
101
102print_success "Firehose running (PID: $FIREHOSE_PID)"
103
104print_status "Starting indexer (consuming from Kafka)..."
105go run ./cmd/indexer &
106INDEXER_PID=$!
107sleep 3
108
109if ! ps -p $INDEXER_PID > /dev/null; then
110 print_error "Indexer failed to start"
111 cleanup
112 exit 1
113fi
114
115print_success "Indexer running (PID: $INDEXER_PID)"
116
117print_status "Starting CDN (tracking blob references)..."
118go run ./cmd/cdn &
119CDN_PID=$!
120sleep 3
121
122if ! ps -p $CDN_PID > /dev/null; then
123 print_error "CDN failed to start"
124 cleanup
125 exit 1
126fi
127
128print_success "CDN running (PID: $CDN_PID)"
129
130echo ""
131print_success "Full stack is running!"
132echo ""
133echo "Services:"
134echo " - Zookeeper: localhost:2181"
135echo " - Kafka (broker 1): localhost:9092"
136echo " - Kafka (broker 2): localhost:9093"
137echo " - Kafka (broker 3): localhost:9094"
138echo " - Cassandra: localhost:9042"
139echo " - Database Server: localhost:9090"
140echo " - Firehose: PID $FIREHOSE_PID"
141echo " - Indexer: PID $INDEXER_PID"
142echo " - CDN: PID $CDN_PID"
143echo ""
144echo "Process IDs:"
145echo " - Database: $DATABASE_PID"
146echo " - Firehose: $FIREHOSE_PID"
147echo " - Indexer: $INDEXER_PID"
148echo " - CDN: $CDN_PID"
149echo ""
150print_warning "Press Ctrl+C to stop all services"
151echo ""
152
153wait