tracker/config.go

99 lines
2.5 KiB
Go
Raw Normal View History

2016-01-25 06:41:39 +01:00
// 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.
2016-03-03 02:18:55 +01:00
package chihaya
2016-01-25 06:41:39 +01:00
import (
"io"
"io/ioutil"
"os"
"time"
"gopkg.in/yaml.v2"
)
// DefaultConfig is a sane configuration used as a fallback or for testing.
var DefaultConfig = Config{
Tracker: TrackerConfig{
AnnounceInterval: 30 * time.Minute,
MinAnnounceInterval: 20 * time.Minute,
AnnounceMiddleware: []MiddlewareConfig{},
ScrapeMiddleware: []MiddlewareConfig{},
2016-01-25 06:41:39 +01:00
},
Servers: []ServerConfig{},
}
// Config represents the global configuration of a chihaya binary.
type Config struct {
Tracker TrackerConfig `yaml:"tracker"`
Servers []ServerConfig `yaml:"servers"`
}
2016-03-03 02:18:55 +01:00
// TrackerConfig represents the configuration of protocol-agnostic BitTorrent
// Tracker used by Servers started by chihaya.
2016-01-25 06:41:39 +01:00
type TrackerConfig struct {
AnnounceInterval time.Duration `yaml:"announce"`
MinAnnounceInterval time.Duration `yaml:"min_announce"`
AnnounceMiddleware []MiddlewareConfig `yaml:"announce_middleware"`
ScrapeMiddleware []MiddlewareConfig `yaml:"scrape_middleware"`
}
// MiddlewareConfig represents the configuration of a middleware used by
// the tracker.
type MiddlewareConfig struct {
Name string `yaml:"name"`
Config interface{} `yaml:"config"`
2016-01-25 06:41:39 +01:00
}
2016-03-03 02:18:55 +01:00
// ServerConfig represents the configuration of the Servers started by chihaya.
2016-01-25 06:41:39 +01:00
type ServerConfig struct {
Name string `yaml:"name"`
Config interface{} `yaml:"config"`
}
2016-03-03 02:18:55 +01:00
// ConfigFile represents a YAML configuration file that namespaces all chihaya
// configuration under the "chihaya" namespace.
type ConfigFile struct {
Chihaya Config `yaml:"chihaya"`
}
2016-01-25 06:41:39 +01:00
2016-03-03 02:18:55 +01:00
// DecodeConfigFile unmarshals an io.Reader into a new Config.
func DecodeConfigFile(r io.Reader) (*Config, error) {
contents, err := ioutil.ReadAll(r)
2016-01-25 06:41:39 +01:00
if err != nil {
return nil, err
}
2016-03-03 02:18:55 +01:00
cfgFile := &ConfigFile{}
err = yaml.Unmarshal(contents, cfgFile)
2016-01-25 06:41:39 +01:00
if err != nil {
return nil, err
}
2016-03-03 02:18:55 +01:00
return &cfgFile.Chihaya, nil
2016-01-25 06:41:39 +01:00
}
2016-03-03 02:18:55 +01:00
// 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 == "" {
return &DefaultConfig, nil
}
f, err := os.Open(os.ExpandEnv(path))
2016-01-25 06:41:39 +01:00
if err != nil {
return nil, err
}
2016-03-03 02:18:55 +01:00
defer f.Close()
2016-01-25 06:41:39 +01:00
2016-03-03 02:18:55 +01:00
cfg, err := DecodeConfigFile(f)
2016-01-25 06:41:39 +01:00
if err != nil {
return nil, err
}
return cfg, nil
}