From 1a5a7ae90cc9fec8952a54e0b923de299f00451b Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Tue, 21 Sep 2021 17:26:16 +0930 Subject: [PATCH] Use a slice instead for storing downloads --- main.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 723968f..1adf5d1 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ type download struct { Log []string `json:"log"` } -var downloads map[int]*download +var downloads []*download var downloadId = 0 var downloadPath = "./" @@ -53,8 +53,6 @@ func main() { http.Handle("/", r) - downloads = make(map[int]*download) - srv := &http.Server{ Handler: r, Addr: "127.0.0.1:8000", @@ -85,7 +83,7 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) { } type Info struct { - Downloads map[int]*download + Downloads []*download BookmarkletURL template.URL } @@ -112,8 +110,13 @@ func FetchInfoHandler(w http.ResponseWriter, r *http.Request) { return } - b, _ := json.Marshal(downloads[id]) - w.Write(b) + for _, dl := range downloads { + if dl.Id == id { + b, _ := json.Marshal(dl) + w.Write(b) + return + } + } } else { http.NotFound(w, r) } @@ -140,8 +143,9 @@ func FetchHandler(w http.ResponseWriter, r *http.Request) { Percent: 0.0, Log: make([]string, 0, 1000), } - downloads[downloadId] = &newDownload + downloads = append(downloads, &newDownload) // XXX atomic ^^ + newDownload.Log = append(newDownload.Log, "start of log...") go func() {