-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamrest.go
77 lines (64 loc) · 1.95 KB
/
streamrest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"flag"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/anacrolix/torrent"
"github.com/rs/cors"
)
var torrentCli *torrent.Client
var tcliConfs *torrent.ClientConfig
func main() {
dirPath := flag.String("dir", "srdir", "Set the download directory")
httpHost := flag.String("port", ":1010", "Set the listening port")
noUp := flag.Bool("noup", false, "Disable uploads")
flag.Parse()
tcliConfs = torrent.NewDefaultClientConfig()
tcliConfs.DataDir = filepath.Clean(*dirPath)
tcliConfs.NoUpload = *noUp
log.Printf("[INFO] Download directory is set to: %s\n", tcliConfs.DataDir)
_, isnoUp := os.LookupEnv("NOUP")
if isnoUp {
tcliConfs.NoUpload = true
}
if tcliConfs.NoUpload {
log.Println("[INFO] Upload is disabled")
}
var tCliErr error
torrentCli, tCliErr = torrent.NewClient(tcliConfs)
if tCliErr != nil {
log.Fatalf("[ERROR] Creation of BitTorrent client failed: %s\n", tCliErr)
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
log.Println("[INFO] Termination detected. Removing torrents")
for _, t := range torrentCli.Torrents() {
log.Printf("[INFO] Removing torrent: [%s]\n", t.Name())
t.Drop()
rmaErr := os.RemoveAll(filepath.Join(tcliConfs.DataDir, t.Name()))
if rmaErr != nil {
log.Printf("[ERROR] Failed to remove files of torrent: [%s]: %s\n", t.Name(), rmaErr)
}
}
os.Exit(0)
}()
mux := http.NewServeMux()
mux.HandleFunc("/api/addmagnet", addMagnet)
mux.HandleFunc("/api/stream", beginStream)
mux.HandleFunc("/api/removetorrent", removeTorrent)
mux.HandleFunc("/api/torrents", listTorrents)
mux.HandleFunc("/api/play", playMagnet)
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "DELETE"},
AllowCredentials: true,
})
log.Printf("[INFO] Listening on http://%s\n", *httpHost)
log.Fatalln(http.ListenAndServe(*httpHost, c.Handler(mux)))
}