mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-23 00:08:15 -07:00
api routes; status codes fixed
This commit is contained in:
+42
-42
@@ -15,156 +15,156 @@ import (
|
||||
"github.com/chihaya/chihaya/models"
|
||||
)
|
||||
|
||||
func (t *Tracker) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||
func (t *Tracker) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
conn, err := t.tp.Get()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
torrent, err := conn.FindTorrent(p.ByName("infohash"))
|
||||
if err == tracker.ErrTorrentDNE {
|
||||
return http.StatusNotFound
|
||||
return http.StatusNotFound, err
|
||||
} else if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
e := json.NewEncoder(w)
|
||||
err = e.Encode(torrent)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return http.StatusOK
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) putTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||
func (t *Tracker) putTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
var torrent models.Torrent
|
||||
err = json.Unmarshal(body, &torrent)
|
||||
if err != nil {
|
||||
return http.StatusBadRequest
|
||||
return http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
conn, err := t.tp.Get()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
err = conn.PutTorrent(&torrent)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return http.StatusOK
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||
func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
conn, err := t.tp.Get()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
err = conn.DeleteTorrent(p.ByName("infohash"))
|
||||
if err == tracker.ErrTorrentDNE {
|
||||
return http.StatusNotFound
|
||||
return http.StatusNotFound, err
|
||||
} else if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return http.StatusOK
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||
func (t *Tracker) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
conn, err := t.tp.Get()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
user, err := conn.FindUser(p.ByName("passkey"))
|
||||
if err == tracker.ErrUserDNE {
|
||||
return http.StatusNotFound
|
||||
return http.StatusNotFound, err
|
||||
} else if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
e := json.NewEncoder(w)
|
||||
err = e.Encode(user)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return http.StatusOK
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) putUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||
func (t *Tracker) putUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
var user models.User
|
||||
err = json.Unmarshal(body, &user)
|
||||
if err != nil {
|
||||
return http.StatusBadRequest
|
||||
return http.StatusBadRequest, err
|
||||
}
|
||||
|
||||
conn, err := t.tp.Get()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
err = conn.PutUser(&user)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return http.StatusOK
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||
func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
conn, err := t.tp.Get()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
err = conn.DeleteUser(p.ByName("passkey"))
|
||||
if err == tracker.ErrUserDNE {
|
||||
return http.StatusNotFound
|
||||
return http.StatusNotFound, err
|
||||
} else if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return http.StatusOK
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||
func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
conn, err := t.tp.Get()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
err = conn.PutClient(p.ByName("clientID"))
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return http.StatusOK
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (t *Tracker) delClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) int {
|
||||
func (t *Tracker) delClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
||||
conn, err := t.tp.Get()
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
err = conn.DeleteClient(p.ByName("clientID"))
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
return http.StatusOK
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user