Files
pages/internal/server/gc_api.go
T
2026-11-15 05:47:14 +09:00

80 lines
2.4 KiB
Go

package server
import (
"encoding/json"
"net/http"
"strconv"
"pageswiki/internal/store"
)
func zombieAPIOptions(r *http.Request) (int64, int, error) {
grace := store.ZombieGCDefaultGraceSeconds
limit := store.ZombieGCDefaultLimit
if raw := r.URL.Query().Get("grace_seconds"); raw != "" {
n, err := strconv.ParseInt(raw, 10, 64)
if err != nil || n < 0 {
return 0, 0, store.ErrZombieGCOptions
}
grace = n
}
if raw := r.URL.Query().Get("limit"); raw != "" {
n, err := strconv.Atoi(raw)
if err != nil || n < 1 || n > store.ZombieGCMaxLimit {
return 0, 0, store.ErrZombieGCOptions
}
limit = n
}
return grace, limit, nil
}
func (s *Server) handleZombieListAPI(w http.ResponseWriter, r *http.Request) {
if !s.store.SupportsZombieGC() {
http.Error(w, store.ErrBackendFeatureUnavailable.Error(), http.StatusServiceUnavailable)
return
}
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
grace, limit, err := zombieAPIOptions(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
zombies, err := s.store.ListZombieObjectsInDomain(r.Context(), s.domainUUIDForRequest(r), grace, limit)
if err != nil {
http.Error(w, "unable to list zombie objects", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "private, no-store")
json.NewEncoder(w).Encode(map[string]any{"zombies": zombies, "count": len(zombies), "limit": limit})
}
func (s *Server) handleZombiePurgeAPI(w http.ResponseWriter, r *http.Request) {
if !s.store.SupportsZombieGC() {
http.Error(w, store.ErrBackendFeatureUnavailable.Error(), http.StatusServiceUnavailable)
return
}
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
grace, limit, err := zombieAPIOptions(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
deleted, err := s.store.PurgeZombieObjectsInDomain(r.Context(), s.domainUUIDForRequest(r), grace, limit)
if err != nil {
http.Error(w, "unable to purge zombie objects", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "private, no-store")
json.NewEncoder(w).Encode(map[string]any{"deleted": deleted})
}