diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-10-27 20:35:38 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-10-27 20:35:38 -0400 |
| commit | 8fa07407a5217695843098aa689f1555eb38ba95 (patch) | |
| tree | fab9de2ce8eb825d9ebbee1fe8376172ad0e3844 /cmd | |
| parent | b1d8c9266d48a59e9c666b203ae9f77e8f24d23d (diff) | |
| download | x-8fa07407a5217695843098aa689f1555eb38ba95.tar.xz x-8fa07407a5217695843098aa689f1555eb38ba95.zip | |
cmd/xedn: generation maintenance
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/xedn/internal/xesite/xesite.go | 95 | ||||
| -rw-r--r-- | cmd/xedn/main.go | 2 |
2 files changed, 96 insertions, 1 deletions
diff --git a/cmd/xedn/internal/xesite/xesite.go b/cmd/xedn/internal/xesite/xesite.go index 13e5f62..c36542f 100644 --- a/cmd/xedn/internal/xesite/xesite.go +++ b/cmd/xedn/internal/xesite/xesite.go @@ -3,6 +3,7 @@ package xesite import ( "archive/zip" "compress/gzip" + "encoding/json" "fmt" "io" "log/slog" @@ -52,6 +53,42 @@ func NewZipServer(zipPath, dir string) (*ZipServer, error) { return result, nil } +func (zs *ZipServer) NukeGeneration(w http.ResponseWriter, r *http.Request) { + gen := r.URL.Query().Get("gen") + + os.Remove(filepath.Join(zs.dir, "xesite", gen)) + + fmt.Fprintln(w, "removed", gen) +} + +func (zs *ZipServer) ListGenerations(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + // list files in zs.dir/xesite + var files []string + + dirEntries, err := os.ReadDir(filepath.Join(zs.dir, "xesite")) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + slog.Error("can't read dir", "err", err, "dir", filepath.Join(zs.dir, "xesite")) + return + } + + for _, dirEntry := range dirEntries { + if dirEntry.IsDir() { + continue + } + + if dirEntry.Name() == "latest.zip" { + continue + } + + files = append(files, dirEntry.Name()) + } + + json.NewEncoder(w).Encode(files) +} + func (zs *ZipServer) UploadNewZip(w http.ResponseWriter, r *http.Request) { fname := fmt.Sprintf("xesite-%s.zip", time.Now().Format("2006-01-02T15-04-05")) fpath := filepath.Join(zs.dir, "xesite", fname) @@ -80,6 +117,13 @@ func (zs *ZipServer) UploadNewZip(w http.ResponseWriter, r *http.Request) { slog.Error("can't update zip", "err", err, "fpath", fpath) return } + + os.Link(fpath, filepath.Join(zs.dir, "xesite", "latest.zip")) + if err := deleteOldestFileInFolder(filepath.Join(zs.dir, "xesite")); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + slog.Error("can't delete oldest file", "err", err) + return + } } func (zs *ZipServer) Update(fname string) error { @@ -95,7 +139,11 @@ func (zs *ZipServer) Update(fname string) error { zs.zip = file - old.Close() + if old != nil { + old.Close() + } + + slog.Info("activated new generation", "fname", fname) return nil } @@ -110,3 +158,48 @@ func (zs *ZipServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.FileServer(http.FS(zs.zip)).ServeHTTP(w, r) } + +func deleteOldestFileInFolder(folderPath string) error { + // Read the files in the folder + files, err := os.ReadDir(folderPath) + if err != nil { + return err + } + + if len(files) == 0 { + slog.Debug("no files in the folder") + return nil + } + + if len(files) < 5 { + slog.Debug("less than 5 files in the folder") + return nil + } + + // Initialize variables to keep track of the oldest file + var oldestFile os.DirEntry + var oldestTime time.Time + + // Find the oldest file in the folder + for _, file := range files { + fileInfo, err := file.Info() + if err != nil { + return err + } + if oldestFile == nil || fileInfo.ModTime().Before(oldestTime) { + oldestFile = file + oldestTime = fileInfo.ModTime() + } + } + + // Delete the oldest file + oldestFilePath := filepath.Join(folderPath, oldestFile.Name()) + err = os.Remove(oldestFilePath) + if err != nil { + return err + } + + slog.Info("deleted oldest generation", "path", oldestFilePath) + + return nil +} diff --git a/cmd/xedn/main.go b/cmd/xedn/main.go index 2305938..3ad0624 100644 --- a/cmd/xedn/main.go +++ b/cmd/xedn/main.go @@ -141,6 +141,8 @@ func main() { http.DefaultServeMux.HandleFunc("/debug/varz", tsweb.VarzHandler) http.DefaultServeMux.HandleFunc("/xedn/files", dc.ListFiles) http.DefaultServeMux.HandleFunc("/xedn/purge", dc.Purge) + http.DefaultServeMux.HandleFunc("/xesite/generations", zs.ListGenerations) + http.DefaultServeMux.HandleFunc("/xesite/nuke", zs.NukeGeneration) http.DefaultServeMux.HandleFunc("/xesite/upload", zs.UploadNewZip) http.DefaultServeMux.HandleFunc("/sticker/files", ois.ListFiles) http.DefaultServeMux.HandleFunc("/sticker/purge", ois.Purge) |
