From 9e946433794f778e3bcd2c63fd609d811ed0e0a3 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Mon, 7 Jul 2014 22:23:01 -0400 Subject: [PATCH] implemented some API funcs, added whitelist API --- http/api.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 6 deletions(-) diff --git a/http/api.go b/http/api.go index 9cdb0c4..892071f 100644 --- a/http/api.go +++ b/http/api.go @@ -5,15 +5,98 @@ package http import ( + "encoding/json" "net/http" "github.com/julienschmidt/httprouter" + + "github.com/chihaya/chihaya/drivers/tracker" ) -func (t *Tracker) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {} -func (t *Tracker) putTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {} -func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {} +func (t *Tracker) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { + conn, err := t.tp.Get() + if err != nil { + return http.StatusInternalServerError + } -func (t *Tracker) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {} -func (t *Tracker) putUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {} -func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {} + torrent, err := conn.FindTorrent(p.ByName("infohash")) + if err == tracker.ErrTorrentDNE { + return http.StatusNotFound + } else if err != nil { + return http.StatusInternalServerError + } + + e := json.NewEncoder(w) + err = e.Encode(torrent) + if err != nil { + return http.StatusInternalServerError + } + + return http.StatusOK +} + +func (t *Tracker) putTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { + return http.StatusOK +} + +func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { + return http.StatusOK +} + +func (t *Tracker) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { + conn, err := t.tp.Get() + if err != nil { + return http.StatusInternalServerError + } + + user, err := conn.FindUser(p.ByName("passkey")) + if err == tracker.ErrUserDNE { + return http.StatusNotFound + } else if err != nil { + return http.StatusInternalServerError + } + + e := json.NewEncoder(w) + err = e.Encode(user) + if err != nil { + return http.StatusInternalServerError + } + + return http.StatusOK +} + +func (t *Tracker) putUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { + return http.StatusOK +} + +func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { + return http.StatusOK +} + +func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { + conn, err := t.tp.Get() + if err != nil { + return http.StatusInternalServerError + } + + err = conn.WhitelistClient(p.ByName("clientID")) + if err != nil { + return http.StatusInternalServerError + } + + return http.StatusOK +} + +func (t *Tracker) delClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) int { + conn, err := t.tp.Get() + if err != nil { + return http.StatusInternalServerError + } + + err = conn.UnWhitelistClient(p.ByName("clientID")) + if err != nil { + return http.StatusInternalServerError + } + + return http.StatusOK +}