mirror of
git://f0xx.org/ac/ac-ms-template
synced 2026-07-29 02:18:17 +03:00
56 lines
1.8 KiB
Bash
Executable File
56 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# End-to-end curl cookbook — SPEC appendix A.
|
|
# Usage:
|
|
# export BASE=https://s.f0xx.org
|
|
# export BEARER=your-trusted-token
|
|
# export SIGNER=optional-for-ttl0
|
|
# ./scripts/demo_curl.sh
|
|
set -euo pipefail
|
|
|
|
BASE="${BASE:-https://s.f0xx.org}"
|
|
BEARER="${BEARER:?set BEARER to a trusted API token}"
|
|
SIGNER="${SIGNER:-}"
|
|
TARGET="${TARGET:-https://defender.net/very/long/link?demo=1}"
|
|
TTL="${TTL:-3600}"
|
|
|
|
echo "==> 1) Health"
|
|
curl -fsS "$BASE/api/v1/health" | head -c 200
|
|
echo
|
|
|
|
echo "==> 2) Shorten (POST preferred)"
|
|
SHORT_JSON="$(curl -fsS -X POST "$BASE/api/v1/shorten" \
|
|
-H 'Content-Type: application/json' \
|
|
-d "$(jq -nc --arg url "$TARGET" --arg bearer "$BEARER" --argjson ttl "$TTL" \
|
|
'{url:$url,bearer:$bearer,ttl:$ttl}')")"
|
|
echo "$SHORT_JSON" | jq .
|
|
|
|
SHORT="$(echo "$SHORT_JSON" | jq -r '.u // empty')"
|
|
if [[ -z "$SHORT" ]]; then
|
|
echo "shorten failed (no .u in response)" >&2
|
|
exit 1
|
|
fi
|
|
SLUG="${SHORT##*/}"
|
|
|
|
echo "==> 3) Shorten cache hit (same bearer + url)"
|
|
curl -fsS -X POST "$BASE/api/v1/shorten" \
|
|
-H 'Content-Type: application/json' \
|
|
-d "$(jq -nc --arg url "$TARGET" --arg bearer "$BEARER" --argjson ttl "$TTL" \
|
|
'{url:$url,bearer:$bearer,ttl:$ttl}')" | jq .
|
|
|
|
echo "==> 4) Redirect (HEAD — no body)"
|
|
curl -sSI "$SHORT" | grep -E '^(HTTP|Location:)'
|
|
|
|
echo "==> 5) QR PNG"
|
|
curl -fsS -o "/tmp/url-shortener-qr-${SLUG}.png" "$BASE/api/v1/qr/${SLUG}.png?size=256"
|
|
file "/tmp/url-shortener-qr-${SLUG}.png"
|
|
|
|
if [[ -n "$SIGNER" ]]; then
|
|
echo "==> 6) Permanent link (ttl=0, signer required)"
|
|
curl -fsS -X POST "$BASE/api/v1/shorten" \
|
|
-H 'Content-Type: application/json' \
|
|
-d "$(jq -nc --arg url "$TARGET&perm=1" --arg bearer "$BEARER" --arg signer "$SIGNER" \
|
|
'{url:$url,bearer:$bearer,signer:$signer,ttl:0}')" | jq .
|
|
fi
|
|
|
|
echo "==> Done"
|