Begin refactor to better encapsulate configuration
This also updates the example_config.json which did not previously work. Config.Config is now composed of embedded structs of other configs that can later be used to clearly have a separation of concerns.
This commit is contained in:
parent
d754b5b376
commit
ddd10d9732
5 changed files with 69 additions and 84 deletions
101
config/config.go
101
config/config.go
|
@ -40,6 +40,8 @@ type DriverConfig struct {
|
|||
Params map[string]string `json:"params,omitempty"`
|
||||
}
|
||||
|
||||
// SubnetConfig is the configuration used to specify if local peers should be
|
||||
// given a preference when responding to an announce.
|
||||
type SubnetConfig struct {
|
||||
PreferredSubnet bool `json:"preferred_subnet,omitempty"`
|
||||
PreferredIPv4Subnet int `json:"preferred_ipv4_subnet,omitempty"`
|
||||
|
@ -55,6 +57,7 @@ type NetConfig struct {
|
|||
SubnetConfig
|
||||
}
|
||||
|
||||
// StatsConfig is the configuration used to record runtime statistics.
|
||||
type StatsConfig struct {
|
||||
BufferSize int `json:"stats_buffer_size"`
|
||||
IncludeMem bool `json:"include_mem_stats"`
|
||||
|
@ -63,59 +66,77 @@ type StatsConfig struct {
|
|||
MemUpdateInterval Duration `json:"mem_stats_interval"`
|
||||
}
|
||||
|
||||
type ShardConfig struct {
|
||||
TorrentMapShards int `json:"torrent_map_shards"`
|
||||
// WhitelistConfig is the configuration used enable and store a whitelist of
|
||||
// acceptable torrent client peer ID prefixes.
|
||||
type WhitelistConfig struct {
|
||||
ClientWhitelistEnabled bool `json:"client_whitelist_enabled"`
|
||||
ClientWhitelist []string `json:"client_whitelist,omitempty"`
|
||||
}
|
||||
|
||||
// Config is a configuration for a Server.
|
||||
type Config struct {
|
||||
Addr string `json:"addr"`
|
||||
Tracker DriverConfig `json:"tracker"`
|
||||
Backend DriverConfig `json:"backend"`
|
||||
// TrackerConfig is the configuration for tracker functionality.
|
||||
type TrackerConfig struct {
|
||||
PrivateEnabled bool `json:"private_enabled"`
|
||||
FreeleechEnabled bool `json:"freeleech_enabled"`
|
||||
PurgeInactiveTorrents bool `json:"purge_inactive_torrents"`
|
||||
Announce Duration `json:"announce"`
|
||||
MinAnnounce Duration `json:"min_announce"`
|
||||
NumWantFallback int `json:"default_num_want"`
|
||||
TorrentMapShards int `json:"torrent_map_shards"`
|
||||
|
||||
PrivateEnabled bool `json:"private_enabled"`
|
||||
FreeleechEnabled bool `json:"freeleech_enabled"`
|
||||
PurgeInactiveTorrents bool `json:"purge_inactive_torrents"`
|
||||
NetConfig
|
||||
WhitelistConfig
|
||||
}
|
||||
|
||||
Announce Duration `json:"announce"`
|
||||
MinAnnounce Duration `json:"min_announce"`
|
||||
RequestTimeout Duration `json:"request_timeout"`
|
||||
// HTTPConfig is the configuration for HTTP functionality.
|
||||
type HTTPConfig struct {
|
||||
ListenAddr string `json:"http_listen_addr"`
|
||||
RequestTimeout Duration `json:"http_request_timeout"`
|
||||
HttpReadTimeout Duration `json:"http_read_timeout"`
|
||||
HttpWriteTimeout Duration `json:"http_write_timeout"`
|
||||
HttpListenLimit int `json:"http_listen_limit"`
|
||||
NumWantFallback int `json:"default_num_want"`
|
||||
|
||||
ClientWhitelistEnabled bool `json:"client_whitelist_enabled"`
|
||||
ClientWhitelist []string `json:"client_whitelist,omitempty"`
|
||||
}
|
||||
|
||||
// Config is the global configuration for an instance of Chihaya.
|
||||
type Config struct {
|
||||
TrackerConfig
|
||||
HTTPConfig
|
||||
DriverConfig
|
||||
StatsConfig
|
||||
NetConfig
|
||||
ShardConfig
|
||||
}
|
||||
|
||||
// DefaultConfig is a configuration that can be used as a fallback value.
|
||||
var DefaultConfig = Config{
|
||||
Addr: ":6881",
|
||||
TrackerConfig: TrackerConfig{
|
||||
PrivateEnabled: false,
|
||||
FreeleechEnabled: false,
|
||||
PurgeInactiveTorrents: true,
|
||||
Announce: Duration{30 * time.Minute},
|
||||
MinAnnounce: Duration{15 * time.Minute},
|
||||
NumWantFallback: 50,
|
||||
TorrentMapShards: 1,
|
||||
|
||||
Tracker: DriverConfig{
|
||||
Name: "memory",
|
||||
NetConfig: NetConfig{
|
||||
AllowIPSpoofing: true,
|
||||
DualStackedPeers: true,
|
||||
RespectAF: false,
|
||||
},
|
||||
|
||||
WhitelistConfig: WhitelistConfig{
|
||||
ClientWhitelistEnabled: false,
|
||||
},
|
||||
},
|
||||
|
||||
Backend: DriverConfig{
|
||||
HTTPConfig: HTTPConfig{
|
||||
ListenAddr: ":6881",
|
||||
RequestTimeout: Duration{10 * time.Second},
|
||||
HttpReadTimeout: Duration{10 * time.Second},
|
||||
HttpWriteTimeout: Duration{10 * time.Second},
|
||||
},
|
||||
|
||||
DriverConfig: DriverConfig{
|
||||
Name: "noop",
|
||||
},
|
||||
|
||||
PrivateEnabled: false,
|
||||
FreeleechEnabled: false,
|
||||
PurgeInactiveTorrents: true,
|
||||
|
||||
Announce: Duration{30 * time.Minute},
|
||||
MinAnnounce: Duration{15 * time.Minute},
|
||||
RequestTimeout: Duration{10 * time.Second},
|
||||
HttpReadTimeout: Duration{10 * time.Second},
|
||||
HttpWriteTimeout: Duration{10 * time.Second},
|
||||
NumWantFallback: 50,
|
||||
|
||||
StatsConfig: StatsConfig{
|
||||
BufferSize: 0,
|
||||
IncludeMem: true,
|
||||
|
@ -123,18 +144,6 @@ var DefaultConfig = Config{
|
|||
|
||||
MemUpdateInterval: Duration{5 * time.Second},
|
||||
},
|
||||
|
||||
NetConfig: NetConfig{
|
||||
AllowIPSpoofing: true,
|
||||
DualStackedPeers: true,
|
||||
RespectAF: false,
|
||||
},
|
||||
|
||||
ShardConfig: ShardConfig{
|
||||
TorrentMapShards: 1,
|
||||
},
|
||||
|
||||
ClientWhitelistEnabled: false,
|
||||
}
|
||||
|
||||
// Open is a shortcut to open a file, read it, and generate a Config.
|
||||
|
|
|
@ -1,48 +1,24 @@
|
|||
{
|
||||
"addr": ":6881",
|
||||
|
||||
"tracker": {
|
||||
"driver": "memory"
|
||||
},
|
||||
|
||||
"backend": {
|
||||
"driver": "noop"
|
||||
},
|
||||
|
||||
"private_enabled": false,
|
||||
"freeleech_enabled": false,
|
||||
"purge_inactive_torrents": true,
|
||||
|
||||
"announce": "30m",
|
||||
"min_announce": "15m",
|
||||
"request_timeout": "10s",
|
||||
"default_num_want": 50,
|
||||
|
||||
"torrent_map_shards": 1,
|
||||
"allow_ip_spoofing": true,
|
||||
"dual_stacked_peers": true,
|
||||
"real_ip_header": "",
|
||||
"preferred_subnet": false,
|
||||
"preferred_ipv4_subnet": 0,
|
||||
"preferred_ipv6_subnet": 0,
|
||||
|
||||
"respect_af": false,
|
||||
"client_whitelist_enabled": false,
|
||||
"http_listen_addr": ":6881",
|
||||
"http_request_timeout": "10s",
|
||||
"http_read_timeout": "10s",
|
||||
"http_write_timeout": "10s",
|
||||
"http_listen_limit": 0,
|
||||
"driver": "noop",
|
||||
"stats_buffer_size": 0,
|
||||
"include_mem_stats": true,
|
||||
"verbose_mem_stats": false,
|
||||
"mem_stats_interval": "5s",
|
||||
|
||||
"client_whitelist_enabled": false,
|
||||
"client_whitelist": [
|
||||
"UT340-",
|
||||
"AZ2500",
|
||||
"exbc0J",
|
||||
"FUTB0L",
|
||||
"XBT054",
|
||||
"OP1011",
|
||||
"ML2.7.",
|
||||
"BOWA0C",
|
||||
"Q1-0-0",
|
||||
"Q1-10-",
|
||||
"346---",
|
||||
"QVOD00"
|
||||
]
|
||||
"mem_stats_interval": "5s"
|
||||
}
|
||||
|
|
|
@ -116,7 +116,7 @@ func Serve(cfg *config.Config, tkr *tracker.Tracker) {
|
|||
tracker: tkr,
|
||||
}
|
||||
|
||||
glog.V(0).Info("Starting on ", cfg.Addr)
|
||||
glog.V(0).Info("Starting on ", cfg.ListenAddr)
|
||||
if cfg.HttpListenLimit != 0 {
|
||||
glog.V(0).Info("Limiting connections to ", cfg.HttpListenLimit)
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ func Serve(cfg *config.Config, tkr *tracker.Tracker) {
|
|||
ConnState: srv.connState,
|
||||
ListenLimit: cfg.HttpListenLimit,
|
||||
Server: &http.Server{
|
||||
Addr: cfg.Addr,
|
||||
Addr: cfg.ListenAddr,
|
||||
Handler: newRouter(srv),
|
||||
ReadTimeout: cfg.HttpReadTimeout.Duration,
|
||||
WriteTimeout: cfg.HttpWriteTimeout.Duration,
|
||||
|
|
|
@ -35,7 +35,7 @@ type Storage struct {
|
|||
func NewStorage(cfg *config.Config) *Storage {
|
||||
s := &Storage{
|
||||
users: make(map[string]*models.User),
|
||||
shards: make([]Torrents, cfg.ShardConfig.TorrentMapShards),
|
||||
shards: make([]Torrents, cfg.TorrentMapShards),
|
||||
clients: make(map[string]bool),
|
||||
}
|
||||
for i := range s.shards {
|
||||
|
|
|
@ -27,7 +27,7 @@ type Tracker struct {
|
|||
// New creates a new Tracker, and opens any necessary connections.
|
||||
// Maintenance routines are automatically spawned in the background.
|
||||
func New(cfg *config.Config) (*Tracker, error) {
|
||||
bc, err := backend.Open(&cfg.Backend)
|
||||
bc, err := backend.Open(&cfg.DriverConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue