remove config package
This commit is contained in:
parent
47f85ec961
commit
0dfc26caea
16 changed files with 129 additions and 123 deletions
|
@ -11,7 +11,7 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya/server"
|
"github.com/chihaya/chihaya/server"
|
||||||
"github.com/chihaya/chihaya/tracker"
|
"github.com/chihaya/chihaya/tracker"
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ func init() {
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
cfg, err := config.Open(configPath)
|
cfg, err := chihaya.OpenConfigFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("failed to load config: " + err.Error())
|
log.Fatal("failed to load config: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,7 @@
|
||||||
// Use of this source code is governed by the BSD 2-Clause license,
|
// Use of this source code is governed by the BSD 2-Clause license,
|
||||||
// which can be found in the LICENSE file.
|
// which can be found in the LICENSE file.
|
||||||
|
|
||||||
// Package config implements the opening and parsing of a chihaya configuration.
|
package chihaya
|
||||||
package config
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
@ -31,8 +30,8 @@ type Config struct {
|
||||||
Servers []ServerConfig `yaml:"servers"`
|
Servers []ServerConfig `yaml:"servers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TrackerConfig represents the configuration of the BitTorrent tracker used by
|
// TrackerConfig represents the configuration of protocol-agnostic BitTorrent
|
||||||
// chihaya.
|
// Tracker used by Servers started by chihaya.
|
||||||
type TrackerConfig struct {
|
type TrackerConfig struct {
|
||||||
AnnounceInterval time.Duration `yaml:"announce"`
|
AnnounceInterval time.Duration `yaml:"announce"`
|
||||||
MinAnnounceInterval time.Duration `yaml:"min_announce"`
|
MinAnnounceInterval time.Duration `yaml:"min_announce"`
|
||||||
|
@ -40,15 +39,39 @@ type TrackerConfig struct {
|
||||||
ScrapeMiddleware []string `yaml:"scrape_middleware"`
|
ScrapeMiddleware []string `yaml:"scrape_middleware"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServerConfig represents the configuration of the servers started by chihaya.
|
// ServerConfig represents the configuration of the Servers started by chihaya.
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
Config interface{} `yaml:"config"`
|
Config interface{} `yaml:"config"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open is a shortcut to open a file, read it, and allocates a new Config.
|
// ConfigFile represents a YAML configuration file that namespaces all chihaya
|
||||||
// It supports relative and absolute paths. Given "", it returns DefaultConfig.
|
// configuration under the "chihaya" namespace.
|
||||||
func Open(path string) (*Config, error) {
|
type ConfigFile struct {
|
||||||
|
Chihaya Config `yaml:"chihaya"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeConfigFile unmarshals an io.Reader into a new Config.
|
||||||
|
func DecodeConfigFile(r io.Reader) (*Config, error) {
|
||||||
|
contents, err := ioutil.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cfgFile := &ConfigFile{}
|
||||||
|
err = yaml.Unmarshal(contents, cfgFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &cfgFile.Chihaya, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenConfigFile returns a new Config given the path to a YAML configuration
|
||||||
|
// file.
|
||||||
|
// It supports relative and absolute paths and environment variables.
|
||||||
|
// Given "", it returns DefaultConfig.
|
||||||
|
func OpenConfigFile(path string) (*Config, error) {
|
||||||
if path == "" {
|
if path == "" {
|
||||||
return &DefaultConfig, nil
|
return &DefaultConfig, nil
|
||||||
}
|
}
|
||||||
|
@ -59,23 +82,7 @@ func Open(path string) (*Config, error) {
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
cfg, err := Decode(f)
|
cfg, err := DecodeConfigFile(f)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return cfg, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode unmarshals an io.Reader into a newly allocated *Config.
|
|
||||||
func Decode(r io.Reader) (*Config, error) {
|
|
||||||
contents, err := ioutil.ReadAll(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg := &Config{}
|
|
||||||
err = yaml.Unmarshal(contents, cfg)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
|
@ -1,39 +0,0 @@
|
||||||
# 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.
|
|
||||||
|
|
||||||
tracker:
|
|
||||||
announce: 10m
|
|
||||||
min_announce: 5m
|
|
||||||
announce_middleware:
|
|
||||||
- prometheus
|
|
||||||
- store_client_validation
|
|
||||||
- store_create_on_announce
|
|
||||||
scrape_middleware:
|
|
||||||
- prometheus
|
|
||||||
- store_client_validation
|
|
||||||
|
|
||||||
servers:
|
|
||||||
- name: store
|
|
||||||
config:
|
|
||||||
addr: localhost:6880
|
|
||||||
request_timeout: 10s
|
|
||||||
read_timeout: 10s
|
|
||||||
write_timeout: 10s
|
|
||||||
client_store: memory
|
|
||||||
ip_store: memory
|
|
||||||
peer_store: memory
|
|
||||||
peer_store_config:
|
|
||||||
gcAfter: 30m
|
|
||||||
shards: 1
|
|
||||||
|
|
||||||
- name: http
|
|
||||||
config:
|
|
||||||
addr: localhost:6881
|
|
||||||
request_timeout: 10s
|
|
||||||
read_timeout: 10s
|
|
||||||
write_timeout: 10s
|
|
||||||
|
|
||||||
- name: udp
|
|
||||||
config:
|
|
||||||
addr: localhost:6882
|
|
40
config_example.yaml
Normal file
40
config_example.yaml
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
chihaya:
|
||||||
|
tracker:
|
||||||
|
announce: 10m
|
||||||
|
min_announce: 5m
|
||||||
|
announce_middleware:
|
||||||
|
- prometheus
|
||||||
|
- store_client_validation
|
||||||
|
- store_create_on_announce
|
||||||
|
scrape_middleware:
|
||||||
|
- prometheus
|
||||||
|
- store_client_validation
|
||||||
|
|
||||||
|
servers:
|
||||||
|
- name: store
|
||||||
|
config:
|
||||||
|
addr: localhost:6880
|
||||||
|
request_timeout: 10s
|
||||||
|
read_timeout: 10s
|
||||||
|
write_timeout: 10s
|
||||||
|
client_store: memory
|
||||||
|
ip_store: memory
|
||||||
|
peer_store: memory
|
||||||
|
peer_store_config:
|
||||||
|
gcAfter: 30m
|
||||||
|
shards: 1
|
||||||
|
|
||||||
|
- name: http
|
||||||
|
config:
|
||||||
|
addr: localhost:6881
|
||||||
|
request_timeout: 10s
|
||||||
|
read_timeout: 10s
|
||||||
|
write_timeout: 10s
|
||||||
|
|
||||||
|
- name: udp
|
||||||
|
config:
|
||||||
|
addr: localhost:6882
|
|
@ -7,8 +7,9 @@ package http
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"github.com/chihaya/chihaya"
|
||||||
)
|
)
|
||||||
|
|
||||||
type httpConfig struct {
|
type httpConfig struct {
|
||||||
|
@ -21,7 +22,7 @@ type httpConfig struct {
|
||||||
RealIPHeader string `yaml:"real_ip_header"`
|
RealIPHeader string `yaml:"real_ip_header"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHTTPConfig(srvcfg *config.ServerConfig) (*httpConfig, error) {
|
func newHTTPConfig(srvcfg *chihaya.ServerConfig) (*httpConfig, error) {
|
||||||
bytes, err := yaml.Marshal(srvcfg.Config)
|
bytes, err := yaml.Marshal(srvcfg.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"github.com/tylerb/graceful"
|
"github.com/tylerb/graceful"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya/server"
|
"github.com/chihaya/chihaya/server"
|
||||||
"github.com/chihaya/chihaya/tracker"
|
"github.com/chihaya/chihaya/tracker"
|
||||||
)
|
)
|
||||||
|
@ -22,7 +22,7 @@ func init() {
|
||||||
server.Register("http", constructor)
|
server.Register("http", constructor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func constructor(srvcfg *config.ServerConfig, tkr *tracker.Tracker) (server.Server, error) {
|
func constructor(srvcfg *chihaya.ServerConfig, tkr *tracker.Tracker) (server.Server, error) {
|
||||||
cfg, err := newHTTPConfig(srvcfg)
|
cfg, err := newHTTPConfig(srvcfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("http: invalid config: " + err.Error())
|
return nil, errors.New("http: invalid config: " + err.Error())
|
||||||
|
|
|
@ -7,7 +7,7 @@ package server
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya/tracker"
|
"github.com/chihaya/chihaya/tracker"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,9 +17,9 @@ type Pool struct {
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartPool creates a new pool of servers specified by the provided config and
|
// StartPool creates a new pool of servers specified by the provided
|
||||||
// runs them.
|
// configuration and runs them.
|
||||||
func StartPool(cfgs []config.ServerConfig, tkr *tracker.Tracker) (*Pool, error) {
|
func StartPool(cfgs []chihaya.ServerConfig, tkr *tracker.Tracker) (*Pool, error) {
|
||||||
var toReturn Pool
|
var toReturn Pool
|
||||||
|
|
||||||
for _, cfg := range cfgs {
|
for _, cfg := range cfgs {
|
||||||
|
|
|
@ -12,14 +12,14 @@ package server
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya/tracker"
|
"github.com/chihaya/chihaya/tracker"
|
||||||
)
|
)
|
||||||
|
|
||||||
var constructors = make(map[string]Constructor)
|
var constructors = make(map[string]Constructor)
|
||||||
|
|
||||||
// Constructor is a function that creates a new Server.
|
// Constructor is a function that creates a new Server.
|
||||||
type Constructor func(*config.ServerConfig, *tracker.Tracker) (Server, error)
|
type Constructor func(*chihaya.ServerConfig, *tracker.Tracker) (Server, error)
|
||||||
|
|
||||||
// Register makes a Constructor available by the provided name.
|
// Register makes a Constructor available by the provided name.
|
||||||
//
|
//
|
||||||
|
@ -36,7 +36,7 @@ func Register(name string, con Constructor) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a Server specified by a configuration.
|
// New creates a Server specified by a configuration.
|
||||||
func New(cfg *config.ServerConfig, tkr *tracker.Tracker) (Server, error) {
|
func New(cfg *chihaya.ServerConfig, tkr *tracker.Tracker) (Server, error) {
|
||||||
con, ok := constructors[cfg.Name]
|
con, ok := constructors[cfg.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
|
|
|
@ -6,7 +6,6 @@ package ip
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/chihaya/chihaya"
|
"github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya/config"
|
|
||||||
"github.com/chihaya/chihaya/server/store"
|
"github.com/chihaya/chihaya/server/store"
|
||||||
"github.com/chihaya/chihaya/tracker"
|
"github.com/chihaya/chihaya/tracker"
|
||||||
)
|
)
|
||||||
|
@ -22,7 +21,7 @@ var ErrBlockedClient = tracker.ClientError("disallowed client")
|
||||||
// blacklistAnnounceClient provides a middleware that only allows Clients to
|
// blacklistAnnounceClient provides a middleware that only allows Clients to
|
||||||
// announce that are not stored in a ClientStore.
|
// announce that are not stored in a ClientStore.
|
||||||
func blacklistAnnounceClient(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
func blacklistAnnounceClient(next tracker.AnnounceHandler) tracker.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 {
|
||||||
blacklisted, err := store.MustGetStore().FindClient(req.PeerID)
|
blacklisted, err := store.MustGetStore().FindClient(req.PeerID)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -6,7 +6,6 @@ package ip
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/chihaya/chihaya"
|
"github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya/config"
|
|
||||||
"github.com/chihaya/chihaya/server/store"
|
"github.com/chihaya/chihaya/server/store"
|
||||||
"github.com/chihaya/chihaya/tracker"
|
"github.com/chihaya/chihaya/tracker"
|
||||||
)
|
)
|
||||||
|
@ -18,7 +17,7 @@ func init() {
|
||||||
// whitelistAnnounceClient provides a middleware that only allows Clients to
|
// whitelistAnnounceClient provides a middleware that only allows Clients to
|
||||||
// announce that are stored in a ClientStore.
|
// announce that are stored in a ClientStore.
|
||||||
func whitelistAnnounceClient(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
func whitelistAnnounceClient(next tracker.AnnounceHandler) tracker.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 {
|
||||||
whitelisted, err := store.MustGetStore().FindClient(req.PeerID)
|
whitelisted, err := store.MustGetStore().FindClient(req.PeerID)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya"
|
"github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya/config"
|
|
||||||
"github.com/chihaya/chihaya/server/store"
|
"github.com/chihaya/chihaya/server/store"
|
||||||
"github.com/chihaya/chihaya/tracker"
|
"github.com/chihaya/chihaya/tracker"
|
||||||
)
|
)
|
||||||
|
@ -24,7 +23,7 @@ var ErrBlockedIP = tracker.ClientError("disallowed IP address")
|
||||||
// blacklistAnnounceIP provides a middleware that only allows IPs to announce
|
// blacklistAnnounceIP provides a middleware that only allows IPs to announce
|
||||||
// that are not stored in an IPStore.
|
// that are not stored in an IPStore.
|
||||||
func blacklistAnnounceIP(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
func blacklistAnnounceIP(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
||||||
return func(cfg *config.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
|
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
|
||||||
blacklisted := false
|
blacklisted := false
|
||||||
storage := store.MustGetStore()
|
storage := store.MustGetStore()
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya"
|
"github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya/config"
|
|
||||||
"github.com/chihaya/chihaya/server/store"
|
"github.com/chihaya/chihaya/server/store"
|
||||||
"github.com/chihaya/chihaya/tracker"
|
"github.com/chihaya/chihaya/tracker"
|
||||||
)
|
)
|
||||||
|
@ -20,7 +19,7 @@ func init() {
|
||||||
// whitelistAnnounceIP provides a middleware that only allows IPs to announce
|
// whitelistAnnounceIP provides a middleware that only allows IPs to announce
|
||||||
// that are stored in an IPStore.
|
// that are stored in an IPStore.
|
||||||
func whitelistAnnounceIP(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
func whitelistAnnounceIP(next tracker.AnnounceHandler) tracker.AnnounceHandler {
|
||||||
return func(cfg *config.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
|
return func(cfg *chihaya.TrackerConfig, req *chihaya.AnnounceRequest, resp *chihaya.AnnounceResponse) (err error) {
|
||||||
whitelisted := false
|
whitelisted := false
|
||||||
storage := store.MustGetStore()
|
storage := store.MustGetStore()
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya/config"
|
"github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya/server"
|
"github.com/chihaya/chihaya/server"
|
||||||
"github.com/chihaya/chihaya/tracker"
|
"github.com/chihaya/chihaya/tracker"
|
||||||
)
|
)
|
||||||
|
@ -23,7 +23,7 @@ func init() {
|
||||||
server.Register("store", constructor)
|
server.Register("store", constructor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func constructor(srvcfg *config.ServerConfig, tkr *tracker.Tracker) (server.Server, error) {
|
func constructor(srvcfg *chihaya.ServerConfig, tkr *tracker.Tracker) (server.Server, error) {
|
||||||
if theStore == nil {
|
if theStore == nil {
|
||||||
cfg, err := newConfig(srvcfg)
|
cfg, err := newConfig(srvcfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -71,7 +71,7 @@ type Config struct {
|
||||||
IPStoreConfig interface{} `yaml:"ip_store_config"`
|
IPStoreConfig interface{} `yaml:"ip_store_config"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func newConfig(srvcfg *config.ServerConfig) (*Config, error) {
|
func newConfig(srvcfg *chihaya.ServerConfig) (*Config, error) {
|
||||||
bytes, err := yaml.Marshal(srvcfg.Config)
|
bytes, err := yaml.Marshal(srvcfg.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -1,20 +1,17 @@
|
||||||
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
||||||
// Use of this source code is governed by the BSD 2-Clause license,
|
// 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
|
package tracker
|
||||||
|
|
||||||
import (
|
import "github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya"
|
|
||||||
"github.com/chihaya/chihaya/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AnnounceHandler is a function that operates on an AnnounceResponse before it
|
// AnnounceHandler is a function that operates on an AnnounceResponse before it
|
||||||
// has been delivered to a client.
|
// 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
|
// AnnounceMiddleware is a higher-order function used to implement the chaining
|
||||||
// behavior processing an announce.
|
// of AnnounceHandlers.
|
||||||
type AnnounceMiddleware func(AnnounceHandler) AnnounceHandler
|
type AnnounceMiddleware func(AnnounceHandler) AnnounceHandler
|
||||||
|
|
||||||
type announceChain struct{ mw []AnnounceMiddleware }
|
type announceChain struct{ mw []AnnounceMiddleware }
|
||||||
|
@ -24,9 +21,10 @@ func (c *announceChain) Append(mw ...AnnounceMiddleware) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *announceChain) Handler() AnnounceHandler {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := len(c.mw) - 1; i >= 0; i-- {
|
for i := len(c.mw) - 1; i >= 0; i-- {
|
||||||
final = c.mw[i](final)
|
final = c.mw[i](final)
|
||||||
}
|
}
|
||||||
|
@ -35,8 +33,8 @@ func (c *announceChain) Handler() AnnounceHandler {
|
||||||
|
|
||||||
var announceMiddleware = make(map[string]AnnounceMiddleware)
|
var announceMiddleware = make(map[string]AnnounceMiddleware)
|
||||||
|
|
||||||
// RegisterAnnounceMiddleware makes a middleware available to the tracker under
|
// RegisterAnnounceMiddleware makes a middleware globally available under the
|
||||||
// the provided named.
|
// provided named.
|
||||||
//
|
//
|
||||||
// If this function is called twice with the same name or if the handler is nil,
|
// If this function is called twice with the same name or if the handler is nil,
|
||||||
// it panics.
|
// it panics.
|
||||||
|
@ -52,12 +50,12 @@ func RegisterAnnounceMiddleware(name string, mw AnnounceMiddleware) {
|
||||||
announceMiddleware[name] = mw
|
announceMiddleware[name] = mw
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScrapeHandler is a middleware function that operates on a ScrapeResponse
|
// ScrapeHandler is a function that operates on a ScrapeResponse before it has
|
||||||
// before it has been delivered to a client.
|
// been delivered to a client.
|
||||||
type ScrapeHandler func(*config.TrackerConfig, *chihaya.ScrapeRequest, *chihaya.ScrapeResponse) error
|
type ScrapeHandler func(*chihaya.TrackerConfig, *chihaya.ScrapeRequest, *chihaya.ScrapeResponse) error
|
||||||
|
|
||||||
// ScrapeMiddleware is higher-order ScrapeHandler used to implement modular
|
// ScrapeMiddleware is higher-order function used to implement the chaining of
|
||||||
// behavior processing a scrape.
|
// ScrapeHandlers.
|
||||||
type ScrapeMiddleware func(ScrapeHandler) ScrapeHandler
|
type ScrapeMiddleware func(ScrapeHandler) ScrapeHandler
|
||||||
|
|
||||||
type scrapeChain struct{ mw []ScrapeMiddleware }
|
type scrapeChain struct{ mw []ScrapeMiddleware }
|
||||||
|
@ -67,7 +65,7 @@ func (c *scrapeChain) Append(mw ...ScrapeMiddleware) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *scrapeChain) Handler() ScrapeHandler {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
for i := len(c.mw) - 1; i >= 0; i-- {
|
for i := len(c.mw) - 1; i >= 0; i-- {
|
||||||
|
@ -78,8 +76,8 @@ func (c *scrapeChain) Handler() ScrapeHandler {
|
||||||
|
|
||||||
var scrapeMiddleware = make(map[string]ScrapeMiddleware)
|
var scrapeMiddleware = make(map[string]ScrapeMiddleware)
|
||||||
|
|
||||||
// RegisterScrapeMiddleware makes a middleware available to the tracker under
|
// RegisterScrapeMiddleware makes a middleware globally available under the
|
||||||
// the provided named.
|
// provided named.
|
||||||
//
|
//
|
||||||
// If this function is called twice with the same name or if the handler is nil,
|
// If this function is called twice with the same name or if the handler is nil,
|
||||||
// it panics.
|
// it panics.
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
||||||
// Use of this source code is governed by the BSD 2-Clause license,
|
// 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
|
package tracker
|
||||||
|
|
||||||
|
@ -10,11 +10,10 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya"
|
"github.com/chihaya/chihaya"
|
||||||
"github.com/chihaya/chihaya/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func testAnnounceMW1(next AnnounceHandler) AnnounceHandler {
|
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{
|
resp.IPv4Peers = append(resp.IPv4Peers, chihaya.Peer{
|
||||||
Port: 1,
|
Port: 1,
|
||||||
})
|
})
|
||||||
|
@ -23,7 +22,7 @@ func testAnnounceMW1(next AnnounceHandler) AnnounceHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAnnounceMW2(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{
|
resp.IPv4Peers = append(resp.IPv4Peers, chihaya.Peer{
|
||||||
Port: 2,
|
Port: 2,
|
||||||
})
|
})
|
||||||
|
@ -32,7 +31,7 @@ func testAnnounceMW2(next AnnounceHandler) AnnounceHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAnnounceMW3(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{
|
resp.IPv4Peers = append(resp.IPv4Peers, chihaya.Peer{
|
||||||
Port: 3,
|
Port: 3,
|
||||||
})
|
})
|
||||||
|
@ -49,5 +48,5 @@ func TestAnnounceChain(t *testing.T) {
|
||||||
resp := &chihaya.AnnounceResponse{}
|
resp := &chihaya.AnnounceResponse{}
|
||||||
err := handler(nil, &chihaya.AnnounceRequest{}, resp)
|
err := handler(nil, &chihaya.AnnounceRequest{}, resp)
|
||||||
assert.Nil(t, err, "the handler should not return an error")
|
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.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +1,35 @@
|
||||||
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
||||||
// Use of this source code is governed by the BSD 2-Clause license,
|
// 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
|
package tracker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/chihaya/chihaya"
|
"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
|
type ClientError string
|
||||||
|
|
||||||
|
// Error implements the error interface for ClientError.
|
||||||
func (c ClientError) Error() string { return string(c) }
|
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.
|
// tracker.
|
||||||
type Tracker struct {
|
type Tracker struct {
|
||||||
cfg *config.TrackerConfig
|
cfg *chihaya.TrackerConfig
|
||||||
handleAnnounce AnnounceHandler
|
handleAnnounce AnnounceHandler
|
||||||
handleScrape ScrapeHandler
|
handleScrape ScrapeHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTracker parses a config and generates a Tracker composed by the middleware
|
// NewTracker constructs a newly allocated Tracker composed of the middleware
|
||||||
// specified in the config.
|
// in the provided configuration.
|
||||||
func NewTracker(cfg *config.TrackerConfig) (*Tracker, error) {
|
func NewTracker(cfg *chihaya.TrackerConfig) (*Tracker, error) {
|
||||||
var achain announceChain
|
var achain announceChain
|
||||||
for _, mwName := range cfg.AnnounceMiddleware {
|
for _, mwName := range cfg.AnnounceMiddleware {
|
||||||
mw, ok := announceMiddleware[mwName]
|
mw, ok := announceMiddleware[mwName]
|
||||||
|
@ -51,7 +55,7 @@ func NewTracker(cfg *config.TrackerConfig) (*Tracker, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleAnnounce runs an AnnounceRequest through a Tracker's middleware and
|
// HandleAnnounce runs an AnnounceRequest through the Tracker's middleware and
|
||||||
// returns the result.
|
// returns the result.
|
||||||
func (t *Tracker) HandleAnnounce(req *chihaya.AnnounceRequest) (*chihaya.AnnounceResponse, error) {
|
func (t *Tracker) HandleAnnounce(req *chihaya.AnnounceRequest) (*chihaya.AnnounceResponse, error) {
|
||||||
resp := &chihaya.AnnounceResponse{}
|
resp := &chihaya.AnnounceResponse{}
|
||||||
|
@ -59,8 +63,8 @@ func (t *Tracker) HandleAnnounce(req *chihaya.AnnounceRequest) (*chihaya.Announc
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleScrape runs a ScrapeRequest through a Tracker's middleware and returns
|
// HandleScrape runs a ScrapeRequest through the Tracker's middleware and
|
||||||
// the result.
|
// returns the result.
|
||||||
func (t *Tracker) HandleScrape(req *chihaya.ScrapeRequest) (*chihaya.ScrapeResponse, error) {
|
func (t *Tracker) HandleScrape(req *chihaya.ScrapeRequest) (*chihaya.ScrapeResponse, error) {
|
||||||
resp := &chihaya.ScrapeResponse{}
|
resp := &chihaya.ScrapeResponse{}
|
||||||
err := t.handleScrape(t.cfg, req, resp)
|
err := t.handleScrape(t.cfg, req, resp)
|
||||||
|
|
Loading…
Reference in a new issue