#!/bin/bash
# 실습 #12 ② — ACL 라우팅 (path / src / extension)
set -e

CFG="$(dirname "$0")/configs/02_haproxy_acl_routing.cfg"

# 1) 백엔드 준비 (각 ACL 대상 구분용 응답)
docker rm -f be1 be2 2>/dev/null || true
docker run -d --name be1 -p 8001:80 nginx
docker run -d --name be2 -p 8002:80 nginx
docker exec be1 sh -c "echo 'API server' > /usr/share/nginx/html/index.html"
docker exec be2 sh -c "echo 'Admin server' > /usr/share/nginx/html/index.html"

# 2) 설정 배포
sudo cp "$CFG" /etc/haproxy/haproxy.cfg
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
sudo systemctl reload haproxy

# 3) ACL 라우팅 검증
echo
echo "=== ACL 라우팅 검증 ==="
echo "→ /api/users → api_pool (be1)"
curl -s http://localhost/api/users
echo
echo "→ /admin/dashboard (127.0.0.1 = office 가정 → admin_pool be2)"
curl -s http://localhost/admin/dashboard
echo
echo "→ /assets/logo.png (확장자 매칭 → static_pool)"
curl -sI http://localhost/assets/logo.png | head -2
echo
echo "→ /anything (catch-all → web_pool roundrobin)"
for i in {1..4}; do curl -s http://localhost/page; done | sort | uniq -c
