#!/usr/bin/env bash
set -euo pipefail

BASE="${1:-http://localhost:8000}"
BASE="${BASE%/}"

fail() {
  echo "FAIL: $1"
  exit 1
}

code="$(curl -fsS -o /dev/null -w "%{http_code}" "$BASE/api/v1/health" || echo 000)"
[[ "$code" == "200" ]] || fail "health returned $code"

root_code="$(curl -fsS -o /dev/null -w "%{http_code}" "${BASE%/api/v1/health}/" 2>/dev/null || curl -fsS -o /dev/null -w "%{http_code}" "$(echo "$BASE" | sed 's|:8000||')/" || echo 000)"
echo "Landing/root HTTP: $root_code"

echo "OK: health check passed"
