tracker/http/routes.go

47 lines
1.2 KiB
Go
Raw Normal View History

2015-01-01 12:02:25 -05:00
// Copyright 2015 The Chihaya Authors. All rights reserved.
2014-07-07 21:59:41 -04:00
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package http
import (
"net/http"
"github.com/julienschmidt/httprouter"
2014-07-21 20:24:51 -04:00
"github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker/models"
2014-07-07 21:59:41 -04:00
)
func handleTorrentError(err error, w *Writer) (int, error) {
if err == nil {
return http.StatusOK, nil
2015-02-21 14:35:21 -05:00
} else if models.IsPublicError(err) {
w.WriteError(err)
stats.RecordEvent(stats.ClientError)
return http.StatusOK, nil
}
return http.StatusInternalServerError, err
}
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
writer := &Writer{w}
2015-02-20 02:06:44 -05:00
ann, err := s.newAnnounce(r, p)
if err != nil {
return handleTorrentError(err, writer)
}
return handleTorrentError(s.tracker.HandleAnnounce(ann, writer), writer)
}
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
writer := &Writer{w}
2015-02-20 02:06:44 -05:00
scrape, err := s.newScrape(r, p)
if err != nil {
return handleTorrentError(err, writer)
}
return handleTorrentError(s.tracker.HandleScrape(scrape, writer), writer)
}