diff --git a/config.yaml b/config.yaml index 354530b..650a80b 100644 --- a/config.yaml +++ b/config.yaml @@ -5,4 +5,4 @@ MySQLPass: testuser MySQLDB: testdb RedisServer: localhost RedisPort: 6379 -Whitelist: true +Whitelist: false diff --git a/database/database.go b/database/database.go index 80a9ad4..fb289e6 100644 --- a/database/database.go +++ b/database/database.go @@ -137,7 +137,22 @@ func GetWhitelistedTorrents() (x *sql.Rows, err error) { } // ScrapeTorrent supports the Scrape convention -func ScrapeTorrent(db *gorm.DB, infoHash string) interface{} { - var torrent Torrent - return db.Where("infoHash = ?", infoHash).First(&torrent) +func ScrapeTorrentFromInfoHash(db *gorm.DB, infoHash string) (values string, err error) { + torrent := &Torrent{} + + x := db.Where("info_hash = ?", infoHash).First(&torrent) + if x.Error != nil { + err = x.Error + } + + fmt.Println(x.Value) + + return +} + +func ScrapeTorrent(db *gorm.DB) string { + torrent := &Torrent{} + db.Find(&torrent) + + return "" } diff --git a/server/server.go b/server/server.go index d1cd657..453d78c 100644 --- a/server/server.go +++ b/server/server.go @@ -23,7 +23,6 @@ func worker(data *announceData) []string { CreateNewTorrentKey(data.requestContext.redisClient, data.info_hash) return worker(data) } - func (app *applicationContext) handleStatsTracking(data *announceData) { if app.trackerLevel > RATIOLESS { db.UpdatePeerStats(data.uploaded, data.downloaded, data.ip) @@ -74,24 +73,50 @@ func (app *applicationContext) requestHandler(w http.ResponseWriter, req *http.R w.Write([]byte(response)) } else { - failMsg := fmt.Sprintf("No peers for torrent %s\n", data.info_hash) - w.Write([]byte(createFailureMessage(failMsg))) + failMsg := fmt.Sprintf("No peers for torrent %s\n", + data.info_hash) + writeErrorResponse(w, failMsg) } } app.handleStatsTracking(data) } -func scrapeHandler(w http.ResponseWriter, req *http.Request) interface{} { - //query := req.URL.Query() - //infoHash := ParseInfoHash(query.Get("info_hash")) +func scrapeHandler(w http.ResponseWriter, req *http.Request) { + query := req.URL.Query() + dbConn, err := db.OpenConnection() + if err != nil { + panic(err) + } + + infoHash := query.Get("info_hash") + if infoHash != "" { + values, err := db.ScrapeTorrentFromInfoHash( + dbConn, + ParseInfoHash(infoHash)) + if err != nil { + failMsg := fmt.Sprintf("Torrent not found.") + writeErrorResponse(w, failMsg) + } + + writeResponse(w, req, values) + } else { + writeResponse(w, req, db.ScrapeTorrent(dbConn)) + } + + return +} + +func writeErrorResponse(w http.ResponseWriter, failMsg string) { + w.Write([]byte(createFailureMessage(failMsg))) +} - //data := db.ScrapeTorrent(db.OpenConnection(), infoHash) - //return data - return "TODO" +func writeResponse(w http.ResponseWriter, req *http.Request, values string) { + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte(values)) } -// RunServer spins up the server and muxes the url +// RunServer spins up the server and muxes the routes. func RunServer() { app := applicationContext{ config: config.LoadConfig(), @@ -101,6 +126,6 @@ func RunServer() { mux := http.NewServeMux() mux.HandleFunc("/announce", app.requestHandler) - //mux.HandleFunc("/scrape", scrapeHandler) + mux.HandleFunc("/scrape", scrapeHandler) http.ListenAndServe(":3000", mux) }