2013-06-22 03:43:11 +02:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
2013-06-24 04:34:13 +02:00
|
|
|
// Package config implements the configuration for a BitTorrent tracker
|
2013-06-22 01:31:32 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2013-07-05 12:50:52 +02:00
|
|
|
"io"
|
2013-06-22 01:31:32 +02:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
type Duration struct {
|
|
|
|
time.Duration
|
|
|
|
}
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
func (d *Duration) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(d.String())
|
|
|
|
}
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
|
|
|
var str string
|
|
|
|
err := json.Unmarshal(b, &str)
|
|
|
|
d.Duration, err = time.ParseDuration(str)
|
|
|
|
return err
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 20:38:45 +02:00
|
|
|
// DataStore represents the configuration used to connect to a data store.
|
|
|
|
type DataStore struct {
|
2013-06-22 01:31:32 +02:00
|
|
|
Driver string `json:"driver"`
|
2013-06-24 20:18:32 +02:00
|
|
|
Network string `json:"network`
|
2013-08-29 05:44:45 +02:00
|
|
|
Host string `json:"host"`
|
|
|
|
Port string `json:"port"`
|
2013-06-22 01:31:32 +02:00
|
|
|
Username string `json:"user"`
|
|
|
|
Password string `json:"pass"`
|
2013-06-24 20:18:32 +02:00
|
|
|
Schema string `json:"schema,omitempty"`
|
|
|
|
Encoding string `json:"encoding,omitempty"`
|
2013-06-26 03:58:06 +02:00
|
|
|
Prefix string `json:"prefix,omitempty"`
|
2013-06-24 20:18:32 +02:00
|
|
|
|
2013-09-07 00:39:14 +02:00
|
|
|
MaxIdleConns int `json:"max_idle_conns,omitempty"`
|
|
|
|
IdleTimeout *Duration `json:"idle_timeout,omitempty"`
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-07-05 12:50:52 +02:00
|
|
|
// Config represents a configuration for a server.Server.
|
2013-06-23 12:21:59 +02:00
|
|
|
type Config struct {
|
2013-08-26 20:38:45 +02:00
|
|
|
Addr string `json:"addr"`
|
|
|
|
PubAddr string `json:"pub_addr"`
|
|
|
|
Cache DataStore `json:"cache"`
|
|
|
|
Storage DataStore `json:"storage"`
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-06-23 12:21:59 +02:00
|
|
|
Private bool `json:"private"`
|
|
|
|
Freeleech bool `json:"freeleech"`
|
2013-07-12 06:36:24 +02:00
|
|
|
Slots bool `json:"slots"`
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-07-04 00:24:03 +02:00
|
|
|
Announce Duration `json:"announce"`
|
|
|
|
MinAnnounce Duration `json:"min_announce"`
|
|
|
|
ReadTimeout Duration `json:"read_timeout"`
|
|
|
|
DefaultNumWant int `json:"default_num_want"`
|
2013-08-26 20:38:45 +02:00
|
|
|
|
|
|
|
TxRetries int `json:"tx_retries"`
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-07-05 12:50:52 +02:00
|
|
|
// Open is a shortcut to open a file, read it, and generate a Config.
|
|
|
|
// It supports relative and absolute paths.
|
|
|
|
func Open(path string) (*Config, error) {
|
2013-06-22 01:31:32 +02:00
|
|
|
expandedPath := os.ExpandEnv(path)
|
|
|
|
f, err := os.Open(expandedPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2013-07-17 05:37:03 +02:00
|
|
|
conf, err := newConfig(f)
|
2013-06-22 01:31:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conf, nil
|
|
|
|
}
|
2013-06-23 12:21:59 +02:00
|
|
|
|
2013-07-05 12:50:52 +02:00
|
|
|
// New decodes JSON from a Reader into a Config.
|
2013-07-17 05:37:03 +02:00
|
|
|
func newConfig(raw io.Reader) (*Config, error) {
|
2013-07-05 12:50:52 +02:00
|
|
|
conf := &Config{}
|
|
|
|
err := json.NewDecoder(raw).Decode(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-06-23 12:21:59 +02:00
|
|
|
}
|
2013-07-05 12:50:52 +02:00
|
|
|
return conf, nil
|
2013-06-23 12:21:59 +02:00
|
|
|
}
|