remove config package

This commit is contained in:
Jimmy Zelinskie
2016-03-02 20:18:55 -05:00
parent 47f85ec961
commit 0dfc26caea
16 changed files with 129 additions and 123 deletions
+17 -19
View File
@@ -1,20 +1,17 @@
// Copyright 2016 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 middleware
// which can be found in the LICENSE file.
package tracker
import (
"github.com/chihaya/chihaya"
"github.com/chihaya/chihaya/config"
)
import "github.com/chihaya/chihaya"
// AnnounceHandler is a function that operates on an AnnounceResponse before it
// has been delivered to a client.
type AnnounceHandler func(*config.TrackerConfig, *chihaya.AnnounceRequest, *chihaya.AnnounceResponse) error
type AnnounceHandler func(*chihaya.TrackerConfig, *chihaya.AnnounceRequest, *chihaya.AnnounceResponse) error
// AnnounceMiddleware is higher-order AnnounceHandler used to implement modular
// behavior processing an announce.
// AnnounceMiddleware is a higher-order function used to implement the chaining
// of AnnounceHandlers.
type AnnounceMiddleware func(AnnounceHandler) AnnounceHandler
type announceChain struct{ mw []AnnounceMiddleware }
@@ -24,9 +21,10 @@ func (c *announceChain) Append(mw ...AnnounceMiddleware) {
}
func (c *announceChain) Handler() AnnounceHandler {
final := func(cfg *config.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
final := func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
return nil
}
for i := len(c.mw) - 1; i >= 0; i-- {
final = c.mw[i](final)
}
@@ -35,8 +33,8 @@ func (c *announceChain) Handler() AnnounceHandler {
var announceMiddleware = make(map[string]AnnounceMiddleware)
// RegisterAnnounceMiddleware makes a middleware available to the tracker under
// the provided named.
// RegisterAnnounceMiddleware makes a middleware globally available under the
// provided named.
//
// If this function is called twice with the same name or if the handler is nil,
// it panics.
@@ -52,12 +50,12 @@ func RegisterAnnounceMiddleware(name string, mw AnnounceMiddleware) {
announceMiddleware[name] = mw
}
// ScrapeHandler is a middleware function that operates on a ScrapeResponse
// before it has been delivered to a client.
type ScrapeHandler func(*config.TrackerConfig, *chihaya.ScrapeRequest, *chihaya.ScrapeResponse) error
// ScrapeHandler is a function that operates on a ScrapeResponse before it has
// been delivered to a client.
type ScrapeHandler func(*chihaya.TrackerConfig, *chihaya.ScrapeRequest, *chihaya.ScrapeResponse) error
// ScrapeMiddleware is higher-order ScrapeHandler used to implement modular
// behavior processing a scrape.
// ScrapeMiddleware is higher-order function used to implement the chaining of
// ScrapeHandlers.
type ScrapeMiddleware func(ScrapeHandler) ScrapeHandler
type scrapeChain struct{ mw []ScrapeMiddleware }
@@ -67,7 +65,7 @@ func (c *scrapeChain) Append(mw ...ScrapeMiddleware) {
}
func (c *scrapeChain) Handler() ScrapeHandler {
final := func(cfg *config.TrackerConfig, req *chihaya.ScrapeRequest, resp *chihaya.ScrapeResponse) error {
final := func(cfg *chihaya.TrackerConfig, req *chihaya.ScrapeRequest, resp *chihaya.ScrapeResponse) error {
return nil
}
for i := len(c.mw) - 1; i >= 0; i-- {
@@ -78,8 +76,8 @@ func (c *scrapeChain) Handler() ScrapeHandler {
var scrapeMiddleware = make(map[string]ScrapeMiddleware)
// RegisterScrapeMiddleware makes a middleware available to the tracker under
// the provided named.
// RegisterScrapeMiddleware makes a middleware globally available under the
// provided named.
//
// If this function is called twice with the same name or if the handler is nil,
// it panics.
+5 -6
View File
@@ -1,6 +1,6 @@
// Copyright 2016 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 middleware
// which can be found in the LICENSE file.
package tracker
@@ -10,11 +10,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/chihaya/chihaya"
"github.com/chihaya/chihaya/config"
)
func testAnnounceMW1(next AnnounceHandler) AnnounceHandler {
return func(cfg *config.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
resp.IPv4Peers = append(resp.IPv4Peers, chihaya.Peer{
Port: 1,
})
@@ -23,7 +22,7 @@ func testAnnounceMW1(next AnnounceHandler) AnnounceHandler {
}
func testAnnounceMW2(next AnnounceHandler) AnnounceHandler {
return func(cfg *config.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
resp.IPv4Peers = append(resp.IPv4Peers, chihaya.Peer{
Port: 2,
})
@@ -32,7 +31,7 @@ func testAnnounceMW2(next AnnounceHandler) AnnounceHandler {
}
func testAnnounceMW3(next AnnounceHandler) AnnounceHandler {
return func(cfg *config.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) error {
resp.IPv4Peers = append(resp.IPv4Peers, chihaya.Peer{
Port: 3,
})
@@ -49,5 +48,5 @@ func TestAnnounceChain(t *testing.T) {
resp := &chihaya.AnnounceResponse{}
err := handler(nil, &chihaya.AnnounceRequest{}, resp)
assert.Nil(t, err, "the handler should not return an error")
assert.Equal(t, resp.IPv4Peers, []chihaya.Peer{chihaya.Peer{Port: 1}, chihaya.Peer{Port: 2}, chihaya.Peer{Port: 3}}, "the list of peers added from the middleware should be in the same order.")
assert.Equal(t, resp.IPv4Peers, []chihaya.Peer{{Port: 1}, {Port: 2}, {Port: 3}}, "the list of peers added from the middleware should be in the same order.")
}
+14 -10
View File
@@ -1,31 +1,35 @@
// Copyright 2016 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 middleware
// which can be found in the LICENSE file.
// Package tracker implements a protocol-independent, middleware-composed
// BitTorrent tracker.
package tracker
import (
"errors"
"github.com/chihaya/chihaya"
"github.com/chihaya/chihaya/config"
)
// ClientError represents an error that should be exposed to the client over
// the BitTorrent protocol implementation.
type ClientError string
// Error implements the error interface for ClientError.
func (c ClientError) Error() string { return string(c) }
// Tracker represents a protocol independent, middleware-composed BitTorrent
// Tracker represents a protocol-independent, middleware-composed BitTorrent
// tracker.
type Tracker struct {
cfg *config.TrackerConfig
cfg *chihaya.TrackerConfig
handleAnnounce AnnounceHandler
handleScrape ScrapeHandler
}
// NewTracker parses a config and generates a Tracker composed by the middleware
// specified in the config.
func NewTracker(cfg *config.TrackerConfig) (*Tracker, error) {
// NewTracker constructs a newly allocated Tracker composed of the middleware
// in the provided configuration.
func NewTracker(cfg *chihaya.TrackerConfig) (*Tracker, error) {
var achain announceChain
for _, mwName := range cfg.AnnounceMiddleware {
mw, ok := announceMiddleware[mwName]
@@ -51,7 +55,7 @@ func NewTracker(cfg *config.TrackerConfig) (*Tracker, error) {
}, nil
}
// HandleAnnounce runs an AnnounceRequest through a Tracker's middleware and
// HandleAnnounce runs an AnnounceRequest through the Tracker's middleware and
// returns the result.
func (t *Tracker) HandleAnnounce(req *chihaya.AnnounceRequest) (*chihaya.AnnounceResponse, error) {
resp := &chihaya.AnnounceResponse{}
@@ -59,8 +63,8 @@ func (t *Tracker) HandleAnnounce(req *chihaya.AnnounceRequest) (*chihaya.Announc
return resp, err
}
// HandleScrape runs a ScrapeRequest through a Tracker's middleware and returns
// the result.
// HandleScrape runs a ScrapeRequest through the Tracker's middleware and
// returns the result.
func (t *Tracker) HandleScrape(req *chihaya.ScrapeRequest) (*chihaya.ScrapeResponse, error) {
resp := &chihaya.ScrapeResponse{}
err := t.handleScrape(t.cfg, req, resp)