tracker/http/routes.go

247 lines
5.9 KiB
Go
Raw Normal View History

2014-07-08 03:59:41 +02:00
// Copyright 2014 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package http
import (
"encoding/json"
2014-07-08 22:55:24 +02:00
"io/ioutil"
2014-07-08 03:59:41 +02:00
"net/http"
2014-07-16 11:46:33 +02:00
"net/url"
2014-07-08 03:59:41 +02:00
"github.com/julienschmidt/httprouter"
2014-07-22 02:24:51 +02:00
"github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker"
"github.com/chihaya/chihaya/tracker/models"
2014-07-08 03:59:41 +02:00
)
const jsonContentType = "application/json; charset=UTF-8"
func (s *Server) check(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
2014-07-23 06:12:29 +02:00
if _, err := w.Write([]byte("STILL-ALIVE")); err != nil {
2014-07-22 07:19:09 +02:00
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
func (s *Server) stats(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
2014-07-22 07:12:41 +02:00
w.Header().Set("Content-Type", jsonContentType)
e := json.NewEncoder(w)
err := e.Encode(stats.DefaultStats)
2014-07-16 11:44:26 +02:00
if err != nil {
return http.StatusInternalServerError, err
}
return http.StatusOK, nil
}
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
ann, err := NewAnnounce(s.config, r, p)
writer := &Writer{w}
if err == models.ErrMalformedRequest {
writer.WriteError(err)
return http.StatusOK, nil
} else if err != nil {
return http.StatusInternalServerError, err
}
if err = s.tracker.HandleAnnounce(ann, writer); err != nil {
return http.StatusInternalServerError, err
}
2014-07-22 02:24:51 +02:00
stats.RecordEvent(stats.Announce)
return http.StatusOK, nil
}
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
scrape, err := NewScrape(s.config, r, p)
writer := &Writer{w}
if err == models.ErrMalformedRequest {
writer.WriteError(err)
return http.StatusOK, nil
} else if err != nil {
return http.StatusInternalServerError, err
}
if err = s.tracker.HandleScrape(scrape, writer); err != nil {
return http.StatusInternalServerError, err
}
2014-07-22 02:24:51 +02:00
stats.RecordEvent(stats.Scrape)
return http.StatusOK, nil
}
func (s *Server) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := s.tracker.Pool.Get()
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-16 11:46:33 +02:00
infohash, err := url.QueryUnescape(p.ByName("infohash"))
if err != nil {
return http.StatusNotFound, err
}
torrent, err := conn.FindTorrent(infohash)
if err == tracker.ErrTorrentDNE {
2014-07-09 06:53:57 +02:00
return http.StatusNotFound, err
} else if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", jsonContentType)
e := json.NewEncoder(w)
err = e.Encode(torrent)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-09 06:53:57 +02:00
return http.StatusOK, nil
}
func (s *Server) putTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
2014-07-08 22:55:24 +02:00
body, err := ioutil.ReadAll(r.Body)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
2014-07-08 22:55:24 +02:00
}
var torrent models.Torrent
err = json.Unmarshal(body, &torrent)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusBadRequest, err
2014-07-08 22:55:24 +02:00
}
conn, err := s.tracker.Pool.Get()
2014-07-08 22:55:24 +02:00
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
2014-07-08 22:55:24 +02:00
}
err = conn.PutTorrent(&torrent)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
2014-07-08 22:55:24 +02:00
}
2014-07-09 06:53:57 +02:00
return http.StatusOK, nil
}
func (s *Server) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := s.tracker.Pool.Get()
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-16 11:46:33 +02:00
infohash, err := url.QueryUnescape(p.ByName("infohash"))
if err != nil {
return http.StatusNotFound, err
}
err = conn.DeleteTorrent(infohash)
if err == tracker.ErrTorrentDNE {
2014-07-09 06:53:57 +02:00
return http.StatusNotFound, err
} else if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-09 06:53:57 +02:00
return http.StatusOK, nil
}
func (s *Server) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := s.tracker.Pool.Get()
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
user, err := conn.FindUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE {
2014-07-09 06:53:57 +02:00
return http.StatusNotFound, err
} else if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", jsonContentType)
e := json.NewEncoder(w)
err = e.Encode(user)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-09 06:53:57 +02:00
return http.StatusOK, nil
}
func (s *Server) putUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
2014-07-08 22:55:24 +02:00
body, err := ioutil.ReadAll(r.Body)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
2014-07-08 22:55:24 +02:00
}
var user models.User
err = json.Unmarshal(body, &user)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusBadRequest, err
2014-07-08 22:55:24 +02:00
}
conn, err := s.tracker.Pool.Get()
2014-07-08 22:55:24 +02:00
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
2014-07-08 22:55:24 +02:00
}
err = conn.PutUser(&user)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
2014-07-08 22:55:24 +02:00
}
2014-07-09 06:53:57 +02:00
return http.StatusOK, nil
}
func (s *Server) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := s.tracker.Pool.Get()
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-08 11:58:00 +02:00
err = conn.DeleteUser(p.ByName("passkey"))
if err == tracker.ErrUserDNE {
2014-07-09 06:53:57 +02:00
return http.StatusNotFound, err
} else if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-09 06:53:57 +02:00
return http.StatusOK, nil
}
func (s *Server) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := s.tracker.Pool.Get()
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-08 11:58:00 +02:00
err = conn.PutClient(p.ByName("clientID"))
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-09 06:53:57 +02:00
return http.StatusOK, nil
}
func (s *Server) delClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
conn, err := s.tracker.Pool.Get()
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-08 11:58:00 +02:00
err = conn.DeleteClient(p.ByName("clientID"))
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
2014-07-08 03:59:41 +02:00
2014-07-09 06:53:57 +02:00
return http.StatusOK, nil
}