Use a slice instead for storing downloads

This commit is contained in:
Justin Hawkins 2021-09-21 17:26:16 +09:30
parent 5cc2e609ae
commit 1a5a7ae90c

16
main.go
View File

@ -33,7 +33,7 @@ type download struct {
Log []string `json:"log"` Log []string `json:"log"`
} }
var downloads map[int]*download var downloads []*download
var downloadId = 0 var downloadId = 0
var downloadPath = "./" var downloadPath = "./"
@ -53,8 +53,6 @@ func main() {
http.Handle("/", r) http.Handle("/", r)
downloads = make(map[int]*download)
srv := &http.Server{ srv := &http.Server{
Handler: r, Handler: r,
Addr: "127.0.0.1:8000", Addr: "127.0.0.1:8000",
@ -85,7 +83,7 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) {
} }
type Info struct { type Info struct {
Downloads map[int]*download Downloads []*download
BookmarkletURL template.URL BookmarkletURL template.URL
} }
@ -112,8 +110,13 @@ func FetchInfoHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
b, _ := json.Marshal(downloads[id]) for _, dl := range downloads {
if dl.Id == id {
b, _ := json.Marshal(dl)
w.Write(b) w.Write(b)
return
}
}
} else { } else {
http.NotFound(w, r) http.NotFound(w, r)
} }
@ -140,8 +143,9 @@ func FetchHandler(w http.ResponseWriter, r *http.Request) {
Percent: 0.0, Percent: 0.0,
Log: make([]string, 0, 1000), Log: make([]string, 0, 1000),
} }
downloads[downloadId] = &newDownload downloads = append(downloads, &newDownload)
// XXX atomic ^^ // XXX atomic ^^
newDownload.Log = append(newDownload.Log, "start of log...") newDownload.Log = append(newDownload.Log, "start of log...")
go func() { go func() {