From a9cf795562bb2572102d980cbc1ce2ea154b1653 Mon Sep 17 00:00:00 2001 From: Bora Alper Date: Sun, 8 Jul 2018 12:08:24 +0300 Subject: [PATCH] magneticow api is now *completely* done! --- cmd/magneticow/api.go | 48 ++++++++++++++++++++++++++++++++++++ cmd/magneticow/main.go | 6 ++--- pkg/persistence/interface.go | 4 +-- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/cmd/magneticow/api.go b/cmd/magneticow/api.go index 8ecd8e4..5511448 100644 --- a/cmd/magneticow/api.go +++ b/cmd/magneticow/api.go @@ -1,6 +1,7 @@ package main import ( + "encoding/hex" "encoding/json" "fmt" "net/http" @@ -8,6 +9,7 @@ import ( "time" "github.com/boramalper/magnetico/pkg/persistence" + "github.com/gorilla/mux" "go.uber.org/zap" ) @@ -87,11 +89,57 @@ func apiTorrentsHandler(w http.ResponseWriter, r *http.Request) { } func apiTorrentsInfohashHandler(w http.ResponseWriter, r *http.Request) { + infohashHex := mux.Vars(r)["infohash"] + infohash, err := hex.DecodeString(infohashHex) + if err != nil { + respondError(w, 400, "couldn't decode infohash: %s", err.Error()) + return + } + + torrent, err := database.GetTorrent(infohash) + if err != nil { + respondError(w, 500, "couldn't get torrent: %s", err.Error()) + return + } + + // TODO: use plain Marshal + jm, err := json.MarshalIndent(torrent, "", " ") + if err != nil { + respondError(w, 500, "json marshalling error: %s", err.Error()) + return + } + + if _, err = w.Write(jm); err != nil { + zap.L().Warn("couldn't write http.ResponseWriter", zap.Error(err)) + } } func apiFilesInfohashHandler(w http.ResponseWriter, r *http.Request) { + infohashHex := mux.Vars(r)["infohash"] + infohash, err := hex.DecodeString(infohashHex) + if err != nil { + respondError(w, 400, "couldn't decode infohash: %s", err.Error()) + return + } + + files, err := database.GetFiles(infohash) + if err != nil { + respondError(w, 500, "couldn't get files: %s", err.Error()) + return + } + + // TODO: use plain Marshal + jm, err := json.MarshalIndent(files, "", " ") + if err != nil { + respondError(w, 500, "json marshalling error: %s", err.Error()) + return + } + + if _, err = w.Write(jm); err != nil { + zap.L().Warn("couldn't write http.ResponseWriter", zap.Error(err)) + } } func apiStatisticsHandler(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/magneticow/main.go b/cmd/magneticow/main.go index f448371..d92508c 100644 --- a/cmd/magneticow/main.go +++ b/cmd/magneticow/main.go @@ -57,12 +57,12 @@ func main() { router.HandleFunc("/", rootHandler) router.HandleFunc("/api/v0.1/torrents", apiTorrentsHandler) - router.HandleFunc("/api/v0.1/torrents/{infohash:[a-z0-9]{40}}", apiTorrentsInfohashHandler) - router.HandleFunc("/api/v0.1/files/{infohash:[a-z0-9]{40}}", apiFilesInfohashHandler) + router.HandleFunc("/api/v0.1/torrents/{infohash:[a-f0-9]{40}}", apiTorrentsInfohashHandler) + router.HandleFunc("/api/v0.1/files/{infohash:[a-f0-9]{40}}", apiFilesInfohashHandler) router.HandleFunc("/api/v0.1/statistics", apiStatisticsHandler) router.HandleFunc("/torrents", torrentsHandler) - router.HandleFunc("/torrents/{infohash:[a-z0-9]{40}}", torrentsInfohashHandler) + router.HandleFunc("/torrents/{infohash:[a-f0-9]{40}}", torrentsInfohashHandler) router.HandleFunc("/statistics", statisticsHandler) router.HandleFunc("/feed", feedHandler) diff --git a/pkg/persistence/interface.go b/pkg/persistence/interface.go index 4290120..1e8bd91 100644 --- a/pkg/persistence/interface.go +++ b/pkg/persistence/interface.go @@ -69,8 +69,8 @@ type Statistics struct { } type File struct { - Size int64 - Path string + Size int64 `json:"size"` + Path string `json:"path"` } type TorrentMetadata struct {