#!/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}"