-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-all-services.sh
More file actions
executable file
·237 lines (206 loc) · 6.75 KB
/
start-all-services.sh
File metadata and controls
executable file
·237 lines (206 loc) · 6.75 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
#!/bin/bash
# Start all services for the zero-trust auth demo
# This script starts services in the background with logging
set -e
# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo -e "${BLUE}🚀 Starting Zero-Trust Authentication Demo${NC}"
echo "=========================================="
echo ""
# Create logs directory
mkdir -p logs
# Check for router/.env file and prompt for credentials if missing
if [ ! -f "router/.env" ]; then
echo -e "${YELLOW}⚠ No router/.env file found${NC}"
echo ""
echo "The Apollo Router requires credentials to connect to Apollo GraphOS."
echo "You can find these in Apollo Studio at https://studio.apollographql.com"
echo ""
read -p "Would you like to configure credentials now? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ""
# Prompt for APOLLO_GRAPH_REF with default
read -p "Enter APOLLO_GRAPH_REF [router-playground@dev]: " GRAPH_REF
GRAPH_REF=${GRAPH_REF:-"router-playground@dev"}
# Prompt for APOLLO_KEY (required)
echo -n "Enter APOLLO_KEY: "
read -s APOLLO_KEY
echo ""
if [ -z "$APOLLO_KEY" ]; then
echo -e "${YELLOW}⚠ APOLLO_KEY is required. Please get your API key from Apollo Studio.${NC}"
exit 1
fi
# Create the .env file
cat > router/.env << EOF
export APOLLO_GRAPH_REF="$GRAPH_REF"
export APOLLO_KEY="$APOLLO_KEY"
EOF
echo -e "${GREEN}✓${NC} Created router/.env file"
echo ""
else
echo ""
echo "To configure manually, copy the sample file and edit it:"
echo " cp router/.env.sample router/.env"
echo " # Then edit router/.env with your credentials"
echo ""
exit 1
fi
fi
# Function to check if a port is in use
check_port() {
lsof -i :$1 > /dev/null 2>&1
return $?
}
# Function to wait for a service to be ready
wait_for_service() {
local url=$1
local name=$2
local max_attempts=30
local attempt=0
echo -n "Waiting for $name to be ready..."
while [ $attempt -lt $max_attempts ]; do
if curl -s $url > /dev/null 2>&1; then
echo -e " ${GREEN}✓${NC}"
return 0
fi
echo -n "."
sleep 1
attempt=$((attempt + 1))
done
echo -e " ${YELLOW}⚠ timeout${NC}"
return 1
}
# Check if services are already running
if check_port 3090; then
echo -e "${YELLOW}⚠ Port 3090 (OBO service) is already in use${NC}"
read -p "Kill existing process? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
lsof -ti:3090 | xargs kill -9 2>/dev/null || true
fi
fi
if check_port 3080; then
echo -e "${YELLOW}⚠ Port 3080 (Coprocessor) is already in use${NC}"
read -p "Kill existing process? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
lsof -ti:3080 | xargs kill -9 2>/dev/null || true
fi
fi
if check_port 4001; then
echo -e "${YELLOW}⚠ Port 4001 (Subgraphs) is already in use${NC}"
read -p "Kill existing process? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
lsof -ti:4001 | xargs kill -9 2>/dev/null || true
fi
fi
if check_port 4000; then
echo -e "${YELLOW}⚠ Port 4000 (Router) is already in use${NC}"
read -p "Kill existing process? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
lsof -ti:4000 | xargs kill -9 2>/dev/null || true
fi
fi
echo ""
# 1. Start OBO Token Service
echo -e "${BLUE}1. Starting OBO Token Service (port 3090)...${NC}"
cd obo-service
if [ ! -d "node_modules" ]; then
echo " Installing dependencies..."
npm install > ../logs/obo-install.log 2>&1
fi
npm start > ../logs/obo-service.log 2>&1 &
OBO_PID=$!
cd ..
echo -e " ${GREEN}✓${NC} Started (PID: $OBO_PID, logs: logs/obo-service.log)"
sleep 2
# 2. Start Coprocessor
echo -e "${BLUE}2. Starting Coprocessor (port 3080)...${NC}"
cd coprocessor
if [ ! -d "node_modules" ]; then
echo " Installing dependencies..."
npm install > ../logs/coprocessor-install.log 2>&1
fi
npm start > ../logs/coprocessor.log 2>&1 &
COPROC_PID=$!
cd ..
echo -e " ${GREEN}✓${NC} Started (PID: $COPROC_PID, logs: logs/coprocessor.log)"
sleep 2
# 3. Start Subgraphs
echo -e "${BLUE}3. Starting Subgraphs (port 4001)...${NC}"
cd subgraphs
if [ ! -d "node_modules" ]; then
echo " Installing dependencies..."
npm install > ../logs/subgraphs-install.log 2>&1
fi
npm start > ../logs/subgraphs.log 2>&1 &
SUBGRAPHS_PID=$!
cd ..
echo -e " ${GREEN}✓${NC} Started (PID: $SUBGRAPHS_PID, logs: logs/subgraphs.log)"
sleep 3
# 4. Start Router
echo -e "${BLUE}4. Starting Apollo Router (port 4000)...${NC}"
cd router
# Download router if not present
if [ ! -f "router" ]; then
echo " Downloading router..."
./download-router.sh > ../logs/router-download.log 2>&1
fi
# Compose supergraph if not present or outdated
if [ ! -f "supergraph.graphql" ] || [ "supergraph.yaml" -nt "supergraph.graphql" ]; then
echo " Composing supergraph..."
./compose.sh > ../logs/compose.log 2>&1
fi
# Start router
./start-router.sh > ../logs/router.log 2>&1 &
ROUTER_PID=$!
cd ..
echo -e " ${GREEN}✓${NC} Started (PID: $ROUTER_PID, logs: logs/router.log)"
echo ""
echo -e "${BLUE}Waiting for services to be ready...${NC}"
wait_for_service "http://localhost:3090/.well-known/jwks.json" "OBO Service"
wait_for_service "http://localhost:4001/products/graphql" "Subgraphs"
wait_for_service "http://localhost:4000/health" "Router" || wait_for_service "http://localhost:4000/" "Router"
echo ""
echo -e "${GREEN}✅ All services started successfully!${NC}"
echo ""
echo "Service URLs:"
echo ""
echo " 🚀 Apollo Router: http://localhost:4000"
echo ""
echo " 🔐 OBO Service (port 3090):"
echo " JWKS: http://localhost:3090/.well-known/jwks.json"
echo " Token: http://localhost:3090/token"
echo " OBO Exchange: http://localhost:3090/obo/exchange"
echo " Introspect: http://localhost:3090/introspect"
echo ""
echo " ⚙️ Coprocessor: http://localhost:3080 (POST only)"
echo ""
echo " 📦 Subgraphs (port 4001):"
echo " extra: http://localhost:4001/extra/graphql"
echo " products: http://localhost:4001/products/graphql"
echo " reviews: http://localhost:4001/reviews/graphql"
echo " transactions: http://localhost:4001/transactions/graphql"
echo " users: http://localhost:4001/users/graphql"
echo ""
echo "Log files:"
echo " OBO Service: logs/obo-service.log"
echo " Coprocessor: logs/coprocessor.log"
echo " Subgraphs: logs/subgraphs.log"
echo " Router: logs/router.log"
echo ""
echo -e "${YELLOW}To test the authentication flow, run:${NC}"
echo " ./test-auth-flow.sh"
echo ""
echo -e "${YELLOW}To stop all services, run:${NC}"
echo " kill $OBO_PID $COPROC_PID $SUBGRAPHS_PID $ROUTER_PID"
echo ""
echo "Or save PIDs to a file:"
echo "$OBO_PID $COPROC_PID $SUBGRAPHS_PID $ROUTER_PID" > logs/pids.txt
echo -e "${YELLOW}Saved to logs/pids.txt - stop with:${NC} kill \$(cat logs/pids.txt)"