tracker/http/http.go

103 lines
2.3 KiB
Go
Raw Normal View History

// Copyright 2014 The Chihaya Authors. All rights reserved.
2014-07-02 03:40:29 +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 (
"fmt"
"net/http"
"time"
"github.com/golang/glog"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/graceful"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/drivers/backend"
"github.com/chihaya/chihaya/drivers/tracker"
)
type Tracker struct {
cfg *config.Config
tp tracker.Pool
bc backend.Conn
}
func NewTracker(cfg *config.Config) (*Tracker, error) {
tp, err := tracker.Open(&cfg.Tracker)
if err != nil {
return nil, err
}
bc, err := backend.Open(&cfg.Backend)
if err != nil {
return nil, err
}
return &Tracker{
cfg: cfg,
tp: tp,
bc: bc,
}, nil
}
2014-07-09 06:53:57 +02:00
type ResponseHandler func(http.ResponseWriter, *http.Request, httprouter.Params) (int, error)
2014-07-02 03:40:29 +02:00
func makeHandler(handler ResponseHandler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
start := time.Now()
2014-07-09 06:53:57 +02:00
code, err := handler(w, r, p)
if err != nil {
http.Error(w, err.Error(), code)
}
2014-07-02 03:40:29 +02:00
glog.Infof(
"Completed %v %s %s in %v",
code,
http.StatusText(code),
r.URL.Path,
time.Since(start),
)
}
}
func setupRoutes(t *Tracker, cfg *config.Config) *httprouter.Router {
r := httprouter.New()
2014-07-09 06:53:57 +02:00
r.GET("/torrents/:infohash", makeHandler(t.getTorrent))
r.PUT("/torrents/:infohash", makeHandler(t.putTorrent))
r.DELETE("/torrents/:infohash", makeHandler(t.delTorrent))
2014-07-02 03:40:29 +02:00
if cfg.Private {
r.GET("/:passkey/announce", makeHandler(t.ServeAnnounce))
r.GET("/:passkey/scrape", makeHandler(t.ServeScrape))
2014-07-09 06:53:57 +02:00
r.PUT("/users/:passkey", makeHandler(t.putUser))
r.DELETE("/users/:passkey", makeHandler(t.delUser))
2014-07-02 03:40:29 +02:00
} else {
r.GET("/announce", makeHandler(t.ServeAnnounce))
r.GET("/scrape", makeHandler(t.ServeScrape))
}
2014-07-09 06:53:57 +02:00
if cfg.Whitelist {
r.PUT("/clients/:clientID", makeHandler(t.putClient))
r.DELETE("/clients/:clientID", makeHandler(t.delClient))
}
2014-07-02 03:40:29 +02:00
return r
}
func Serve(cfg *config.Config) {
t, err := NewTracker(cfg)
if err != nil {
glog.Fatal("New: ", err)
}
graceful.Run(cfg.Addr, cfg.RequestTimeout.Duration, setupRoutes(t, cfg))
}
func fail(w http.ResponseWriter, r *http.Request, err error) {
errmsg := err.Error()
fmt.Fprintf(w, "d14:failure reason%d:%se", len(errmsg), errmsg)
}