tracker/cmd/trakr/main.go

174 lines
4.2 KiB
Go
Raw Normal View History

2016-08-05 07:47:04 +02:00
package main
import (
"errors"
2016-08-05 09:35:17 +02:00
"io/ioutil"
2016-08-05 07:47:04 +02:00
"log"
2016-08-05 09:35:17 +02:00
"net/http"
2016-08-05 07:47:04 +02:00
"os"
"os/signal"
"runtime/pprof"
"syscall"
2016-08-05 09:35:17 +02:00
"github.com/prometheus/client_golang/prometheus"
2016-08-05 07:47:04 +02:00
"github.com/spf13/cobra"
2016-08-05 09:35:17 +02:00
"gopkg.in/yaml.v2"
2016-08-05 07:47:04 +02:00
2016-08-07 04:41:33 +02:00
"github.com/jzelinskie/trakr/backend"
2016-08-09 21:01:36 +02:00
httpfrontend "github.com/jzelinskie/trakr/frontend/http"
udpfrontend "github.com/jzelinskie/trakr/frontend/udp"
2016-08-05 07:47:04 +02:00
)
2016-08-05 09:35:17 +02:00
type ConfigFile struct {
Config struct {
PrometheusAddr string `yaml:"prometheus_addr"`
backend.BackendConfig
2016-08-07 04:41:33 +02:00
HTTPConfig httpfrontend.Config `yaml:"http"`
UDPConfig udpfrontend.Config `yaml:"udp"`
2016-08-05 09:35:17 +02:00
} `yaml:"trakr"`
}
// ParseConfigFile returns a new ConfigFile given the path to a YAML
// configuration file.
//
// It supports relative and absolute paths and environment variables.
func ParseConfigFile(path string) (*ConfigFile, error) {
if path == "" {
return nil, errors.New("no config path specified")
}
f, err := os.Open(os.ExpandEnv(path))
if err != nil {
return nil, err
}
defer f.Close()
contents, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
var cfgFile ConfigFile
err = yaml.Unmarshal(contents, &cfgFile)
if err != nil {
return nil, err
}
return &cfgFile, nil
}
2016-08-05 07:47:04 +02:00
func main() {
var configFilePath string
var cpuProfilePath string
var rootCmd = &cobra.Command{
Use: "trakr",
Short: "BitTorrent Tracker",
Long: "A customizible, multi-protocol BitTorrent Tracker",
Run: func(cmd *cobra.Command, args []string) {
if err := func() error {
if cpuProfilePath != "" {
log.Println("enabled CPU profiling to " + cpuProfilePath)
f, err := os.Create(cpuProfilePath)
if err != nil {
return err
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
2016-08-05 09:35:17 +02:00
configFile, err := ParseConfigFile(configFilePath)
2016-08-05 07:47:04 +02:00
if err != nil {
return errors.New("failed to read config: " + err.Error())
}
2016-08-05 09:35:17 +02:00
go func() {
promServer := http.Server{
Addr: configFile.Config.PrometheusAddr,
Handler: prometheus.Handler(),
}
log.Println("started serving prometheus stats on", configFile.Config.PrometheusAddr)
if err := promServer.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
// TODO create PeerStore
2016-08-09 22:01:14 +02:00
// TODO create Hooks
trackerBackend, err := backend.New(configFile.Config.BackendConfig, nil, nil, nil, nil, nil)
if err != nil {
return err
}
2016-08-07 04:41:33 +02:00
errChan := make(chan error)
closedChan := make(chan struct{})
var hFrontend *httpfrontend.Frontend
var uFrontend *udpfrontend.Frontend
if configFile.Config.HTTPConfig.Addr != "" {
// TODO get the real TrackerFuncs
2016-08-09 22:01:14 +02:00
hFrontend = httpfrontend.NewFrontend(trackerBackend, configFile.Config.HTTPConfig)
2016-08-07 04:41:33 +02:00
go func() {
log.Println("started serving HTTP on", configFile.Config.HTTPConfig.Addr)
if err := hFrontend.ListenAndServe(); err != nil {
errChan <- errors.New("failed to cleanly shutdown HTTP frontend: " + err.Error())
}
}()
}
if configFile.Config.UDPConfig.Addr != "" {
// TODO get the real TrackerFuncs
2016-08-09 22:01:14 +02:00
uFrontend = udpfrontend.NewFrontend(trackerBackend, configFile.Config.UDPConfig)
2016-08-07 04:41:33 +02:00
go func() {
log.Println("started serving UDP on", configFile.Config.UDPConfig.Addr)
if err := uFrontend.ListenAndServe(); err != nil {
errChan <- errors.New("failed to cleanly shutdown UDP frontend: " + err.Error())
}
}()
}
shutdown := make(chan os.Signal)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
2016-08-05 07:47:04 +02:00
go func() {
<-shutdown
2016-08-07 04:41:33 +02:00
if uFrontend != nil {
uFrontend.Stop()
}
if hFrontend != nil {
hFrontend.Stop()
}
2016-08-07 23:20:31 +02:00
// TODO: stop PeerStore
2016-08-07 04:41:33 +02:00
close(errChan)
close(closedChan)
2016-08-05 07:47:04 +02:00
}()
2016-08-07 23:20:31 +02:00
for err := range errChan {
if err != nil {
close(shutdown)
<-closedChan
return err
}
2016-08-05 07:47:04 +02:00
}
return nil
}(); err != nil {
log.Fatal(err)
}
},
}
rootCmd.Flags().StringVar(&configFilePath, "config", "/etc/trakr.yaml", "location of configuration file (defaults to /etc/trakr.yaml)")
rootCmd.Flags().StringVarP(&cpuProfilePath, "cpuprofile", "", "", "location to save a CPU profile")
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}