forked from connectrpc/connect-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflection-demo.sh
More file actions
executable file
·87 lines (74 loc) · 3.03 KB
/
Copy pathreflection-demo.sh
File metadata and controls
executable file
·87 lines (74 loc) · 3.03 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
#!/usr/bin/env bash
# Demonstrates gRPC server reflection against the multiservice example
# using `buf curl` as an independent, schema-free client.
#
# Every RPC below is resolved through the server's reflection service —
# no proto files or descriptor sets are passed to buf curl.
#
# The server supports two descriptor sources (see build_reflector() in
# src/bin/server.rs); exercise both:
# ./reflection-demo.sh # checked-in FileDescriptorSet
# REFLECTION_SOURCE=pool ./reflection-demo.sh # generated descriptor_pool()
#
# Requires: buf >= 1.30 (https://buf.build/docs/installation)
set -euo pipefail
SERVER_PID=""
ADDR="${ADDR:-127.0.0.1:8080}"
REFLECTION_SOURCE="${REFLECTION_SOURCE:-fds}"
echo "Reflection source: $REFLECTION_SOURCE"
BASE="http://$ADDR"
# gRPC needs HTTP/2; the example server speaks h2c via prior knowledge.
BUF_CURL=(buf curl --protocol grpc --http2-prior-knowledge)
if ! command -v buf >/dev/null 2>&1; then
echo "error: buf is not installed - see https://buf.build/docs/installation" >&2
exit 1
fi
cleanup() {
if [ -n "$SERVER_PID" ]; then
kill "$SERVER_PID" 2>/dev/null || true
wait "$SERVER_PID" 2>/dev/null || true
fi
}
trap cleanup EXIT
echo "Building multiservice example..."
cargo build -p multiservice-example --bin multiservice-server
echo "Starting multiservice-server on $ADDR..."
ADDR="$ADDR" REFLECTION_SOURCE="$REFLECTION_SOURCE" cargo run -p multiservice-example --bin multiservice-server &
SERVER_PID=$!
for _ in $(seq 1 30); do
if curl -s "$BASE/health" > /dev/null 2>&1; then
break
fi
if ! kill -0 "$SERVER_PID" 2>/dev/null; then
echo "Server process died unexpectedly." >&2
exit 1
fi
sleep 0.1
done
echo
echo "==> 1. List the server's services via the reflection RPC itself"
"${BUF_CURL[@]}" -d '{"listServices": ""}' \
"$BASE/grpc.reflection.v1.ServerReflection/ServerReflectionInfo"
echo
echo "==> 2. Fetch the file that declares GreetService (descriptor bytes elided)"
"${BUF_CURL[@]}" -d '{"fileContainingSymbol": "anthropic.connectrpc.greet.v1.GreetService"}' \
"$BASE/grpc.reflection.v1.ServerReflection/ServerReflectionInfo" \
| sed 's/"\([A-Za-z0-9+\/]\{60\}\)[A-Za-z0-9+\/=]*"/"\1..."/'
echo
echo "==> 3. Call GreetService/Greet with the schema resolved via reflection"
"${BUF_CURL[@]}" -d '{"name": "reflection"}' \
"$BASE/anthropic.connectrpc.greet.v1.GreetService/Greet"
echo
echo "==> 4. Call MathService/Add the same way"
"${BUF_CURL[@]}" -d '{"a": 19, "b": 23}' \
"$BASE/anthropic.connectrpc.math.v1.MathService/Add"
echo
echo "==> 5. Same call, resolving the schema over the legacy v1alpha protocol"
"${BUF_CURL[@]}" --reflect-protocol grpc-v1alpha -d '{"name": "v1alpha"}' \
"$BASE/anthropic.connectrpc.greet.v1.GreetService/Greet"
echo
echo "==> 6. Unknown symbols answer in-band with NOT_FOUND (error_code 5)"
"${BUF_CURL[@]}" -d '{"fileContainingSymbol": "no.such.Service"}' \
"$BASE/grpc.reflection.v1.ServerReflection/ServerReflectionInfo"
echo
echo "All reflection demo calls succeeded."