update.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // update.go — rutas /api/update/* (paridad src/routes/update.js, SPEC §2.18,
  2. // solo admin):
  3. // GET /api/update/status → estado actual
  4. // POST /api/update/check → fuerza un chequeo contra GitHub
  5. // POST /api/update/apply → aplica la actualización (202 | 409)
  6. package httpapi
  7. import (
  8. "context"
  9. "net/http"
  10. "time"
  11. "github.com/gnacho/netpulse/server-go/internal/auth"
  12. "github.com/gnacho/netpulse/server-go/internal/updater"
  13. )
  14. // registerUpdateRoutes registra las rutas /api/update/* (solo si hay updater).
  15. func (s *server) registerUpdateRoutes(mux *http.ServeMux, u *updater.Updater) {
  16. mux.Handle("GET /api/update/status", auth.RequireAdmin(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  17. writeJSON(w, http.StatusOK, u.Status())
  18. })))
  19. mux.Handle("POST /api/update/check", auth.RequireAdmin(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  20. ctx, cancel := context.WithTimeout(r.Context(), 12*time.Second)
  21. defer cancel()
  22. writeJSON(w, http.StatusOK, u.Check(ctx))
  23. })))
  24. mux.Handle("POST /api/update/apply", auth.RequireAdmin(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  25. if !u.Apply() {
  26. // already_updating (o update_copy_failed: mismo 409 que el JS,
  27. // que solo devuelve false en ambos casos)
  28. writeError(w, http.StatusConflict, "already_updating")
  29. return
  30. }
  31. writeJSON(w, http.StatusAccepted, u.Status())
  32. })))
  33. }