Files
spring-boot-demo/openfeature/scripts/http-demo.sh
T

67 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# End-to-end: a real flagd daemon, the Spring Boot app, curl, a live flag edit, then flagd killed.
# Writes output/09-http-demo.txt. Needs FLAGD_BIN (or flagd on PATH), JAVA_HOME on JDK 25, mvn.
set -eu
cd "$(dirname "$0")/.."
FLAGD="${FLAGD_BIN:-flagd}"
OUT=output/09-http-demo.txt
WORK="$(mktemp -d)"
cp flags/flags.json "$WORK/flags.json"
mkdir -p output
mvn -B -q -DskipTests package
"$FLAGD" start -f "file:$WORK/flags.json" > "$WORK/flagd.log" 2>&1 &
FLAGD_PID=$!
for i in $(seq 1 30); do (echo > /dev/tcp/localhost/8013) 2>/dev/null && break; sleep 1; done
java -jar target/openfeature-1.0.0.jar > "$WORK/app.log" 2>&1 &
APP_PID=$!
trap 'kill $APP_PID $FLAGD_PID 2>/dev/null || true' EXIT
for i in $(seq 1 60); do
curl -s -o /dev/null localhost:8080/checkout && break
sleep 1
done
sleep 2 # let the provider finish connecting
call() { # call <label> <header>...
local label="$1"; shift
local args=() shown=""
for h in "$@"; do args+=(-H "$h"); shown+="-H '$h' "; done
echo "\$ curl ${shown}localhost:8080/checkout # $label"
curl -s "${args[@]}" localhost:8080/checkout | python3 -c '
import json, sys
d = json.load(sys.stdin)
print(" page:", d["page"])
for k in ("newCheckout", "paymentFlowV2", "discountPercent", "banner"):
print(" %-16s %s" % (k + ":", json.dumps(d[k])))'
echo
}
{
echo "# 1. Three callers, one flag file"
call "anonymous" "X-User-Id: anon-1"
call "standard" "X-User-Id: u2" "X-Tier: standard"
call "premium" "X-User-Id: u3" "X-Tier: premium"
call "staff" "X-User-Id: u4" "X-Tier: standard" "X-Email: [email protected]"
echo "# 2. Edit the flag file: new-checkout defaultVariant off -> on (no restart, no deploy)"
sed -i '/"new-checkout"/,/"defaultVariant"/ s/"defaultVariant": "off"/"defaultVariant": "on"/' "$WORK/flags.json"
sleep 2
call "anonymous, after the edit" "X-User-Id: anon-1"
echo "# 3. Kill flagd"
kill "$FLAGD_PID"; wait "$FLAGD_PID" 2>/dev/null || true
sleep 2
call "premium, flagd gone" "X-User-Id: u3" "X-Tier: premium"
} > "$OUT" 2>&1
{
echo
echo "# Application log: provider events, then the first hook lines"
strip() { sed -E 's/^[^ ]+ +([A-Z]+) [0-9]+ --- \[[^]]*\] +/\1 /' | cut -c1-150; }
grep -E 'flag provider|flag configuration' "$WORK/app.log" | strip | head -8
echo "..."
grep -E ': flag=' "$WORK/app.log" | strip | head -4
} >> "$OUT"
echo "wrote $OUT"