api-endpoints.sh
· 7.1 KiB · Bash
Исходник
#!/usr/bin/env bash
# ═══════════════════════════════════════════════════════════════════════════
# api-endpoints.sh — Demo de endpoints REST/SSE de alloy
# Para el Episodio #834 — atareao con Linux
# ═══════════════════════════════════════════════════════════════════════════
# Uso:
# ./api-endpoints.sh # Modo simulado (sin servidor)
# ALLOY_URL=http://localhost:3066 ./api-endpoints.sh # Modo real
# ═══════════════════════════════════════════════════════════════════════════
set -e
VERDE='\033[0;32m'
AMARILLO='\033[1;33m'
AZUL='\033[0;34m'
GRIS='\033[0;90m'
NC='\033[0m'
BASE_URL="${ALLOY_URL:-http://localhost:3066}"
TOKEN="${ALLOY_TOKEN:-demo-token}"
MODE="${ALLOY_MODE:-simulado}"
echo -e "${AZUL}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${AZUL}║ 🔌 alloy — Demo de API REST + SSE ║${NC}"
echo -e "${AZUL}║ Modo: ${MODE} ║${NC}"
echo -e "${AZUL}╚══════════════════════════════════════════════════════════════╝${NC}"
echo ""
demo_endpoint() {
local method="$1"
local endpoint="$2"
local description="$3"
local data="$4"
echo -e "${AMARILLO}─── ${description} ───${NC}"
echo -e "${GRIS}${method} ${BASE_URL}${endpoint}${NC}"
if [ "$MODE" = "real" ]; then
if [ -n "$data" ]; then
curl -s -X "$method" "${BASE_URL}${endpoint}" \
-H "Cookie: session=${TOKEN}" \
-H "Content-Type: application/json" \
-d "$data" | python3 -m json.tool 2>/dev/null || echo "(raw output)"
else
curl -s -X "$method" "${BASE_URL}${endpoint}" \
-H "Cookie: session=${TOKEN}" | python3 -m json.tool 2>/dev/null || echo "(raw output)"
fi
else
echo -e " ${GRIS}[modo simulado — conecta a alloy real para ver output]${NC}"
echo " → curl -X $method ${BASE_URL}${endpoint}"
fi
echo ""
}
# ── Health check ──────────────────────────────────────────────────────────
echo -e "${VERDE}1. Health check${NC}"
demo_endpoint "GET" "/api/health" "Health check (público, no requiere auth)"
# ── Contenedores ──────────────────────────────────────────────────────────
echo -e "${VERDE}2. Contenedores${NC}"
demo_endpoint "GET" "/api/containers" "Listar todos los contenedores"
demo_endpoint "GET" "/api/containers/nginx/inspect" "Inspeccionar contenedor 'nginx'"
demo_endpoint "POST" "/api/containers/nginx/start" "Iniciar contenedor 'nginx'"
demo_endpoint "POST" "/api/containers/nginx/stop" "Parar contenedor 'nginx'"
demo_endpoint "POST" "/api/containers/nginx/restart" "Reiniciar contenedor 'nginx'"
# ── Stacks ────────────────────────────────────────────────────────────────
echo -e "${VERDE}3. Stacks (Docker Compose)${NC}"
demo_endpoint "GET" "/api/stacks" "Listar proyectos compose"
demo_endpoint "POST" "/api/stacks/media/update" "Actualizar stack 'media'"
# ── Actualizaciones ───────────────────────────────────────────────────────
echo -e "${VERDE}4. Actualizaciones${NC}"
demo_endpoint "POST" "/api/check-update/nginx" "Comprobar actualización de 'nginx'"
demo_endpoint "POST" "/api/update/nginx" "Actualizar contenedor 'nginx'"
demo_endpoint "POST" "/api/check-all" "Check All — comparar todos contra registry"
# ── Admin ─────────────────────────────────────────────────────────────────
echo -e "${VERDE}5. Admin${NC}"
demo_endpoint "GET" "/api/admin/settings" "Ver settings globales"
demo_endpoint "GET" "/api/admin/alerts" "Listar alertas configuradas"
demo_endpoint "POST" "/api/admin/alerts" "Crear alerta" \
'{"container":"nextcloud","notify_events":true}'
# ── Tareas programadas ────────────────────────────────────────────────────
echo -e "${VERDE}6. Tareas programadas${NC}"
demo_endpoint "GET" "/api/schedule" "Listar tareas cron"
demo_endpoint "POST" "/api/schedule" "Crear tarea: reiniciar nextcloud a las 6 AM" \
'{"container":"nextcloud","action":"restart","cron":"0 6 * * *","enabled":true}'
# ── Historial ─────────────────────────────────────────────────────────────
echo -e "${VERDE}7. Historial${NC}"
demo_endpoint "GET" "/api/history" "Ver historial de actualizaciones"
# ── Auth ──────────────────────────────────────────────────────────────────
echo -e "${VERDE}8. Autenticación${NC}"
demo_endpoint "GET" "/api/auth/me" "Ver sesión actual"
# ── Config ────────────────────────────────────────────────────────────────
echo -e "${VERDE}9. Configuración pública${NC}"
demo_endpoint "GET" "/api/config" "Config pública (sin secretos)"
echo -e "${AZUL}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${AZUL}║ ✅ Demo completada ║${NC}"
echo -e "${AZUL}║ ║${NC}"
echo -e "${AZUL}║ Para ver SSE en tiempo real: ║${NC}"
echo -e "${AZUL}║ curl -N ${BASE_URL}/api/events -H \"Cookie: session=...\" ║${NC}"
echo -e "${AZUL}║ curl -N ${BASE_URL}/api/updates -H \"Cookie: session=...\" ║${NC}"
echo -e "${AZUL}╚══════════════════════════════════════════════════════════════╝${NC}"
| 1 | #!/usr/bin/env bash |
| 2 | # ═══════════════════════════════════════════════════════════════════════════ |
| 3 | # api-endpoints.sh — Demo de endpoints REST/SSE de alloy |
| 4 | # Para el Episodio #834 — atareao con Linux |
| 5 | # ═══════════════════════════════════════════════════════════════════════════ |
| 6 | # Uso: |
| 7 | # ./api-endpoints.sh # Modo simulado (sin servidor) |
| 8 | # ALLOY_URL=http://localhost:3066 ./api-endpoints.sh # Modo real |
| 9 | # ═══════════════════════════════════════════════════════════════════════════ |
| 10 | |
| 11 | set -e |
| 12 | |
| 13 | VERDE='\033[0;32m' |
| 14 | AMARILLO='\033[1;33m' |
| 15 | AZUL='\033[0;34m' |
| 16 | GRIS='\033[0;90m' |
| 17 | NC='\033[0m' |
| 18 | |
| 19 | BASE_URL="${ALLOY_URL:-http://localhost:3066}" |
| 20 | TOKEN="${ALLOY_TOKEN:-demo-token}" |
| 21 | MODE="${ALLOY_MODE:-simulado}" |
| 22 | |
| 23 | echo -e "${AZUL}╔══════════════════════════════════════════════════════════════╗${NC}" |
| 24 | echo -e "${AZUL}║ 🔌 alloy — Demo de API REST + SSE ║${NC}" |
| 25 | echo -e "${AZUL}║ Modo: ${MODE} ║${NC}" |
| 26 | echo -e "${AZUL}╚══════════════════════════════════════════════════════════════╝${NC}" |
| 27 | echo "" |
| 28 | |
| 29 | demo_endpoint() { |
| 30 | local method="$1" |
| 31 | local endpoint="$2" |
| 32 | local description="$3" |
| 33 | local data="$4" |
| 34 | |
| 35 | echo -e "${AMARILLO}─── ${description} ───${NC}" |
| 36 | echo -e "${GRIS}${method} ${BASE_URL}${endpoint}${NC}" |
| 37 | |
| 38 | if [ "$MODE" = "real" ]; then |
| 39 | if [ -n "$data" ]; then |
| 40 | curl -s -X "$method" "${BASE_URL}${endpoint}" \ |
| 41 | -H "Cookie: session=${TOKEN}" \ |
| 42 | -H "Content-Type: application/json" \ |
| 43 | -d "$data" | python3 -m json.tool 2>/dev/null || echo "(raw output)" |
| 44 | else |
| 45 | curl -s -X "$method" "${BASE_URL}${endpoint}" \ |
| 46 | -H "Cookie: session=${TOKEN}" | python3 -m json.tool 2>/dev/null || echo "(raw output)" |
| 47 | fi |
| 48 | else |
| 49 | echo -e " ${GRIS}[modo simulado — conecta a alloy real para ver output]${NC}" |
| 50 | echo " → curl -X $method ${BASE_URL}${endpoint}" |
| 51 | fi |
| 52 | echo "" |
| 53 | } |
| 54 | |
| 55 | # ── Health check ────────────────────────────────────────────────────────── |
| 56 | echo -e "${VERDE}1. Health check${NC}" |
| 57 | demo_endpoint "GET" "/api/health" "Health check (público, no requiere auth)" |
| 58 | |
| 59 | # ── Contenedores ────────────────────────────────────────────────────────── |
| 60 | echo -e "${VERDE}2. Contenedores${NC}" |
| 61 | demo_endpoint "GET" "/api/containers" "Listar todos los contenedores" |
| 62 | demo_endpoint "GET" "/api/containers/nginx/inspect" "Inspeccionar contenedor 'nginx'" |
| 63 | demo_endpoint "POST" "/api/containers/nginx/start" "Iniciar contenedor 'nginx'" |
| 64 | demo_endpoint "POST" "/api/containers/nginx/stop" "Parar contenedor 'nginx'" |
| 65 | demo_endpoint "POST" "/api/containers/nginx/restart" "Reiniciar contenedor 'nginx'" |
| 66 | |
| 67 | # ── Stacks ──────────────────────────────────────────────────────────────── |
| 68 | echo -e "${VERDE}3. Stacks (Docker Compose)${NC}" |
| 69 | demo_endpoint "GET" "/api/stacks" "Listar proyectos compose" |
| 70 | demo_endpoint "POST" "/api/stacks/media/update" "Actualizar stack 'media'" |
| 71 | |
| 72 | # ── Actualizaciones ─────────────────────────────────────────────────────── |
| 73 | echo -e "${VERDE}4. Actualizaciones${NC}" |
| 74 | demo_endpoint "POST" "/api/check-update/nginx" "Comprobar actualización de 'nginx'" |
| 75 | demo_endpoint "POST" "/api/update/nginx" "Actualizar contenedor 'nginx'" |
| 76 | demo_endpoint "POST" "/api/check-all" "Check All — comparar todos contra registry" |
| 77 | |
| 78 | # ── Admin ───────────────────────────────────────────────────────────────── |
| 79 | echo -e "${VERDE}5. Admin${NC}" |
| 80 | demo_endpoint "GET" "/api/admin/settings" "Ver settings globales" |
| 81 | demo_endpoint "GET" "/api/admin/alerts" "Listar alertas configuradas" |
| 82 | demo_endpoint "POST" "/api/admin/alerts" "Crear alerta" \ |
| 83 | '{"container":"nextcloud","notify_events":true}' |
| 84 | |
| 85 | # ── Tareas programadas ──────────────────────────────────────────────────── |
| 86 | echo -e "${VERDE}6. Tareas programadas${NC}" |
| 87 | demo_endpoint "GET" "/api/schedule" "Listar tareas cron" |
| 88 | demo_endpoint "POST" "/api/schedule" "Crear tarea: reiniciar nextcloud a las 6 AM" \ |
| 89 | '{"container":"nextcloud","action":"restart","cron":"0 6 * * *","enabled":true}' |
| 90 | |
| 91 | # ── Historial ───────────────────────────────────────────────────────────── |
| 92 | echo -e "${VERDE}7. Historial${NC}" |
| 93 | demo_endpoint "GET" "/api/history" "Ver historial de actualizaciones" |
| 94 | |
| 95 | # ── Auth ────────────────────────────────────────────────────────────────── |
| 96 | echo -e "${VERDE}8. Autenticación${NC}" |
| 97 | demo_endpoint "GET" "/api/auth/me" "Ver sesión actual" |
| 98 | |
| 99 | # ── Config ──────────────────────────────────────────────────────────────── |
| 100 | echo -e "${VERDE}9. Configuración pública${NC}" |
| 101 | demo_endpoint "GET" "/api/config" "Config pública (sin secretos)" |
| 102 | |
| 103 | echo -e "${AZUL}╔══════════════════════════════════════════════════════════════╗${NC}" |
| 104 | echo -e "${AZUL}║ ✅ Demo completada ║${NC}" |
| 105 | echo -e "${AZUL}║ ║${NC}" |
| 106 | echo -e "${AZUL}║ Para ver SSE en tiempo real: ║${NC}" |
| 107 | echo -e "${AZUL}║ curl -N ${BASE_URL}/api/events -H \"Cookie: session=...\" ║${NC}" |
| 108 | echo -e "${AZUL}║ curl -N ${BASE_URL}/api/updates -H \"Cookie: session=...\" ║${NC}" |
| 109 | echo -e "${AZUL}╚══════════════════════════════════════════════════════════════╝${NC}" |