github.com/anacrolix/torrent@v1.61.0/tests/issue-930/server.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 "time" 7 8 "github.com/anacrolix/torrent/metainfo" 9 ) 10 11 func server() { 12 go func() { 13 for range time.Tick(time.Second * 5) { 14 for _, torrent := range client.Torrents() { 15 if torrent.Complete().Bool() { 16 fmt.Println("Dropping torrent", torrent.InfoHash().HexString()) 17 torrent.Drop() 18 } 19 } 20 } 21 }() 22 23 mux := http.NewServeMux() 24 mux.HandleFunc("/torrent", func(w http.ResponseWriter, r *http.Request) { 25 if index >= len(infoHashes) { 26 w.Write([]byte("No more torrents to add")) 27 return 28 } 29 30 infoHash := infoHashes[index] 31 fmt.Println("Adding torrent", infoHash) 32 33 t, _ := client.AddTorrentInfoHash(metainfo.NewHashFromHex(infoHash)) 34 go func() { 35 <-t.GotInfo() 36 fmt.Println("Download started for", infoHash) 37 t.DownloadAll() 38 }() 39 index++ 40 41 w.Write([]byte("OK")) 42 }) 43 44 if err := http.ListenAndServe(":8080", mux); err != nil { 45 panic(err) 46 } 47 }