forked from glblduh/torrenttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorrenttp.go
56 lines (44 loc) · 1.64 KB
/
torrenttp.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
package main
import (
"flag"
"net/http"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
func main() {
/* Argument flags */
dirFlag := flag.String("dir", "torrenttpdl", "Download directory path")
portFlag := flag.String("port", ":1010", "HTTP server listening port")
noupFlag := flag.Bool("noup", false, "Disables BT client upload")
flag.Parse()
// Creates the BitTorrent client with user args
btEngine.initialize(newBtCliConfs(*dirFlag, *noupFlag))
/* Initilize DB and load persistent specs */
dberr := createSpecBucket()
if dberr != nil {
Error.Fatalf("Cannot initialize DB: %s\n", dberr)
}
go loadPersist()
/* Initialize endpoints and HTTP server */
r := mux.NewRouter().StrictSlash(true)
/* Handlers for endpoints */
/* POST */
r.HandleFunc("/api/addtorrent", apiAddTorrent).Methods("POST")
r.HandleFunc("/api/selectfile", apiTorrentSelectFile).Methods("POST")
r.HandleFunc("/api/addtorrentfile", apiAddTorrentFile).Methods("POST")
/* DELETE */
r.HandleFunc("/api/removetorrent", apiRemoveTorrent).Methods("DELETE")
/* GET */
r.HandleFunc("/api/stream/{infohash}/{file:.*}", apiStreamTorrentFile).Methods("GET")
r.HandleFunc("/api/file/{infohash}/{file:.*}", apiDownloadFile).Methods("GET")
r.HandleFunc("/api/torrents", apiTorrentStats).Methods("GET")
r.HandleFunc("/api/torrents/{infohash}", apiTorrentStats).Methods("GET")
/* CORS middleware */
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "DELETE"},
AllowCredentials: true,
}).Handler(r)
Info.Printf("Starting HTTP server on port: http://%s", *portFlag)
Error.Fatalln(http.ListenAndServe(*portFlag, c))
}