Files
kandev/apps/backend/internal/system/updates/handler.go
T
José Almeida 041a19f060 feat: service-managed UI self-update (#1210)
* feat: add service-managed self-update

* chore: add temporary self-update test scripts

* chore: simplify temporary script markdown fences

* chore: run frontend in temporary self-update scripts

* fix: show self-update job progress after apply

* chore: add real npm self-update test script

* fix: isolate npm self-update prefix

* fix: detect npm service installs from bin shim

* chore: reset service before real npm update test

* chore: expand real npm status diagnostics

* fix: preserve service home during self-update

* fix(cli): tee self-update helper output to a log file

* fix(cli): avoid launchd bootstrap race when reloading the macOS service

* fix(backend): run macOS self-update helper once via transient launchd plist

* fix(web): show self-update progress and lock the apply button until the update lands

* fix(cli): reinstall via the kandev wrapper on Homebrew self-update

* chore: remove temp self-update test scripts

* fix(web): drop unused apply path from useUpdates hook

* fix(backend): snapshot install state once in apply preflight

* docs: align self-update docs with transient LaunchAgent flow

* chore(release): run cli bundle from apps workspace root

* fix(backend): guard against concurrent self-update applies

* fix(backend): check scheme and helper paths in self-update apply

* fix(backend): drop duplicate binary name from npx manual command

* fix(cli): allow --help for service self-update without --intent

* fix(cli): harden self-update dir perms and darwin uid resolution

* fix(web): set required POST fields last in applyUpdate

* docs: use JSON body notation for the apply endpoint

* fix(backend): expire self-update guard so a stuck helper cannot wedge apply

* refactor(cli): collapse redundant writeSync branch in self-update log
2026-06-01 17:49:15 +01:00

83 lines
2.3 KiB
Go

package updates
import (
"context"
"errors"
"math"
"net/http"
"github.com/gin-gonic/gin"
)
// HandleGet returns the cached kandev_meta view of latest version. It never
// hits GitHub. Errors from the meta read are surfaced as 500.
func HandleGet(svc *Service) gin.HandlerFunc {
return func(c *gin.Context) {
resp, err := svc.Get()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, resp)
}
}
type applyRequestBody struct {
Confirm string `json:"confirm"`
}
// HandleCheck triggers a synchronous GitHub poll. When the per-process
// limiter denies the request a 429 is returned with retry_after_seconds.
// Other errors are surfaced as 502 since the upstream is GitHub.
func HandleCheck(svc *Service) gin.HandlerFunc {
return func(c *gin.Context) {
resp, err := svc.Check(c.Request.Context())
if errors.Is(err, ErrRateLimited) {
retry := svc.RetryAfter()
seconds := int64(math.Ceil(retry.Seconds()))
if seconds < 1 {
seconds = 1
}
c.JSON(http.StatusTooManyRequests, gin.H{
"error": ErrRateLimited.Error(),
"retry_after_seconds": seconds,
})
return
}
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, resp)
}
}
// HandleApply queues a service-managed self-update. It is deliberately gated
// behind service metadata and a browser same-origin check because the helper
// mutates the local Kandev installation and restarts the service.
func HandleApply(svc *Service) gin.HandlerFunc {
return func(c *gin.Context) {
if !sameOriginOrNoOrigin(c.Request) {
c.JSON(http.StatusForbidden, gin.H{"error": "cross-origin update apply is not allowed"})
return
}
var req applyRequestBody
_ = c.ShouldBindJSON(&req)
jobID, err := svc.Apply(context.Background(), req.Confirm)
if errors.Is(err, ErrApplyConfirm) {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if errors.Is(err, ErrNoUpdateAvailable) || errors.Is(err, ErrApplyUnsupported) ||
errors.Is(err, ErrApplyInProgress) {
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
return
}
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusAccepted, ApplyResponse{JobID: jobID})
}
}