tracker/http/routes.go

47 lines
1.2 KiB
Go
Raw Normal View History

2015-01-01 18:02:25 +01:00
// Copyright 2015 The Chihaya Authors. All rights reserved.
2014-07-08 03:59:41 +02: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-22 02:24:51 +02:00
"github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker/models"
2014-07-08 03:59:41 +02:00
)
func handleTorrentError(err error, w *Writer) (int, error) {
if err == nil {
return http.StatusOK, nil
2015-02-21 20:35:21 +01: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 08:06:44 +01: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 08:06:44 +01:00
scrape, err := s.newScrape(r, p)
if err != nil {
return handleTorrentError(err, writer)
}
return handleTorrentError(s.tracker.HandleScrape(scrape, writer), writer)
}