atareao revisou este gist 2 weeks ago. Ir para a revisão
1 file changed, 109 insertions
api-endpoints.sh(arquivo criado)
| @@ -0,0 +1,109 @@ | |||
| 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}" | |
Próximo
Anterior