Ultima attività 2 weeks ago Unlisted

Demo de endpoints REST/SSE de alloy (Episodio #834)

api-endpoints.sh Raw
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
11set -e
12
13VERDE='\033[0;32m'
14AMARILLO='\033[1;33m'
15AZUL='\033[0;34m'
16GRIS='\033[0;90m'
17NC='\033[0m'
18
19BASE_URL="${ALLOY_URL:-http://localhost:3066}"
20TOKEN="${ALLOY_TOKEN:-demo-token}"
21MODE="${ALLOY_MODE:-simulado}"
22
23echo -e "${AZUL}╔══════════════════════════════════════════════════════════════╗${NC}"
24echo -e "${AZUL}║ 🔌 alloy — Demo de API REST + SSE ║${NC}"
25echo -e "${AZUL}║ Modo: ${MODE}${NC}"
26echo -e "${AZUL}╚══════════════════════════════════════════════════════════════╝${NC}"
27echo ""
28
29demo_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 ──────────────────────────────────────────────────────────
56echo -e "${VERDE}1. Health check${NC}"
57demo_endpoint "GET" "/api/health" "Health check (público, no requiere auth)"
58
59# ── Contenedores ──────────────────────────────────────────────────────────
60echo -e "${VERDE}2. Contenedores${NC}"
61demo_endpoint "GET" "/api/containers" "Listar todos los contenedores"
62demo_endpoint "GET" "/api/containers/nginx/inspect" "Inspeccionar contenedor 'nginx'"
63demo_endpoint "POST" "/api/containers/nginx/start" "Iniciar contenedor 'nginx'"
64demo_endpoint "POST" "/api/containers/nginx/stop" "Parar contenedor 'nginx'"
65demo_endpoint "POST" "/api/containers/nginx/restart" "Reiniciar contenedor 'nginx'"
66
67# ── Stacks ────────────────────────────────────────────────────────────────
68echo -e "${VERDE}3. Stacks (Docker Compose)${NC}"
69demo_endpoint "GET" "/api/stacks" "Listar proyectos compose"
70demo_endpoint "POST" "/api/stacks/media/update" "Actualizar stack 'media'"
71
72# ── Actualizaciones ───────────────────────────────────────────────────────
73echo -e "${VERDE}4. Actualizaciones${NC}"
74demo_endpoint "POST" "/api/check-update/nginx" "Comprobar actualización de 'nginx'"
75demo_endpoint "POST" "/api/update/nginx" "Actualizar contenedor 'nginx'"
76demo_endpoint "POST" "/api/check-all" "Check All — comparar todos contra registry"
77
78# ── Admin ─────────────────────────────────────────────────────────────────
79echo -e "${VERDE}5. Admin${NC}"
80demo_endpoint "GET" "/api/admin/settings" "Ver settings globales"
81demo_endpoint "GET" "/api/admin/alerts" "Listar alertas configuradas"
82demo_endpoint "POST" "/api/admin/alerts" "Crear alerta" \
83 '{"container":"nextcloud","notify_events":true}'
84
85# ── Tareas programadas ────────────────────────────────────────────────────
86echo -e "${VERDE}6. Tareas programadas${NC}"
87demo_endpoint "GET" "/api/schedule" "Listar tareas cron"
88demo_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 ─────────────────────────────────────────────────────────────
92echo -e "${VERDE}7. Historial${NC}"
93demo_endpoint "GET" "/api/history" "Ver historial de actualizaciones"
94
95# ── Auth ──────────────────────────────────────────────────────────────────
96echo -e "${VERDE}8. Autenticación${NC}"
97demo_endpoint "GET" "/api/auth/me" "Ver sesión actual"
98
99# ── Config ────────────────────────────────────────────────────────────────
100echo -e "${VERDE}9. Configuración pública${NC}"
101demo_endpoint "GET" "/api/config" "Config pública (sin secretos)"
102
103echo -e "${AZUL}╔══════════════════════════════════════════════════════════════╗${NC}"
104echo -e "${AZUL}║ ✅ Demo completada ║${NC}"
105echo -e "${AZUL}║ ║${NC}"
106echo -e "${AZUL}║ Para ver SSE en tiempo real: ║${NC}"
107echo -e "${AZUL}║ curl -N ${BASE_URL}/api/events -H \"Cookie: session=...\" ║${NC}"
108echo -e "${AZUL}║ curl -N ${BASE_URL}/api/updates -H \"Cookie: session=...\" ║${NC}"
109echo -e "${AZUL}╚══════════════════════════════════════════════════════════════╝${NC}"