Files
skillshare/internal/server/handler_backup.go
T
Willie 092fb14f8a feat: add web dashboard with embedded React SPA (skillshare ui)
Single-binary web UI for visual skill management — React 19 + Vite
frontend embedded via go:embed, Go net/http REST API backend (30+
endpoints). Includes dashboard, skills browser, targets, sync, collect,
search, install, config editor, backup/restore, and git sync pages.

Also adds Bitbucket/GitLab URL parsing for install, git operations
library, skill version checking, CI frontend build steps, Docker
sandbox support, and website docs for the ui command.
2026-02-08 05:08:30 +08:00

162 lines
3.8 KiB
Go

package server
import (
"encoding/json"
"net/http"
"skillshare/internal/backup"
)
type backupInfoJSON struct {
Timestamp string `json:"timestamp"`
Path string `json:"path"`
Targets []string `json:"targets"`
Date string `json:"date"`
SizeMB float64 `json:"sizeMB"`
}
func toBackupJSON(b backup.BackupInfo) backupInfoJSON {
return backupInfoJSON{
Timestamp: b.Timestamp,
Path: b.Path,
Targets: b.Targets,
Date: b.Date.Format("2006-01-02T15:04:05Z07:00"),
SizeMB: float64(backup.Size(b.Path)) / 1024 / 1024,
}
}
// handleListBackups returns all backups
func (s *Server) handleListBackups(w http.ResponseWriter, r *http.Request) {
backups, err := backup.List()
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
items := make([]backupInfoJSON, 0, len(backups))
for _, b := range backups {
items = append(items, toBackupJSON(b))
}
total, _ := backup.TotalSize()
writeJSON(w, map[string]any{
"backups": items,
"totalSizeMB": float64(total) / 1024 / 1024,
})
}
// handleCreateBackup creates a backup of target(s)
func (s *Server) handleCreateBackup(w http.ResponseWriter, r *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()
var body struct {
Target string `json:"target"` // empty = all targets
}
json.NewDecoder(r.Body).Decode(&body)
targets := make(map[string]string)
if body.Target != "" {
t, ok := s.cfg.Targets[body.Target]
if !ok {
writeError(w, http.StatusBadRequest, "target not found: "+body.Target)
return
}
targets[body.Target] = t.Path
} else {
for name, t := range s.cfg.Targets {
targets[name] = t.Path
}
}
var created []string
for name, path := range targets {
bp, err := backup.Create(name, path)
if err != nil {
writeError(w, http.StatusInternalServerError, "backup failed for "+name+": "+err.Error())
return
}
if bp != "" {
created = append(created, name)
}
}
writeJSON(w, map[string]any{
"success": true,
"backedUpTargets": created,
})
}
// handleCleanupBackups removes old backups
func (s *Server) handleCleanupBackups(w http.ResponseWriter, r *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()
cfg := backup.DefaultCleanupConfig()
removed, err := backup.Cleanup(cfg)
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
writeJSON(w, map[string]any{
"success": true,
"removed": removed,
})
}
// handleRestore restores a backup to a target
func (s *Server) handleRestore(w http.ResponseWriter, r *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()
var body struct {
Timestamp string `json:"timestamp"`
Target string `json:"target"`
Force bool `json:"force"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeError(w, http.StatusBadRequest, "invalid JSON body")
return
}
if body.Timestamp == "" || body.Target == "" {
writeError(w, http.StatusBadRequest, "timestamp and target are required")
return
}
// Verify target exists in config
t, ok := s.cfg.Targets[body.Target]
if !ok {
writeError(w, http.StatusBadRequest, "target not found: "+body.Target)
return
}
// Find backup
bk, err := backup.GetBackupByTimestamp(body.Timestamp)
if err != nil {
writeError(w, http.StatusNotFound, "backup not found: "+err.Error())
return
}
opts := backup.RestoreOptions{Force: body.Force}
// Validate first
if err := backup.ValidateRestore(bk.Path, body.Target, t.Path, opts); err != nil {
writeError(w, http.StatusConflict, err.Error())
return
}
// Restore
if err := backup.RestoreToPath(bk.Path, body.Target, t.Path, opts); err != nil {
writeError(w, http.StatusInternalServerError, "restore failed: "+err.Error())
return
}
writeJSON(w, map[string]any{
"success": true,
"target": body.Target,
"timestamp": body.Timestamp,
})
}