add API to Boot() and clean things up

This commit is contained in:
Jimmy Zelinskie 2015-10-11 02:27:58 -04:00
parent 9add47ed07
commit 5f413e6e2d

View file

@ -2,6 +2,9 @@
// 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 chihaya implements the ability to boot the Chihaya BitTorrent
// tracker with your own imports that can dynamically register additional
// functionality.
package chihaya package chihaya
import ( import (
@ -14,6 +17,7 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"github.com/chihaya/chihaya/api"
"github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/http" "github.com/chihaya/chihaya/http"
"github.com/chihaya/chihaya/stats" "github.com/chihaya/chihaya/stats"
@ -34,6 +38,11 @@ func init() {
flag.StringVar(&configPath, "config", "", "path to the configuration file") flag.StringVar(&configPath, "config", "", "path to the configuration file")
} }
type server interface {
Serve()
Stop()
}
// Boot starts Chihaya. By exporting this function, anyone can import their own // Boot starts Chihaya. By exporting this function, anyone can import their own
// custom drivers into their own package main and then call chihaya.Boot. // custom drivers into their own package main and then call chihaya.Boot.
func Boot() { func Boot() {
@ -65,28 +74,29 @@ func Boot() {
glog.Fatal("New: ", err) glog.Fatal("New: ", err)
} }
var wg sync.WaitGroup var servers []server
var servers []tracker.Server
if cfg.HTTPListenAddr != "" { if cfg.APIConfig.ListenAddr != "" {
wg.Add(1) srv := api.NewServer(cfg, tkr)
srv := http.NewServer(cfg, tkr)
servers = append(servers, srv) servers = append(servers, srv)
go func() {
defer wg.Done()
srv.Serve(cfg.HTTPListenAddr)
}()
} }
if cfg.UDPListenAddr != "" { if cfg.HTTPConfig.ListenAddr != "" {
wg.Add(1) srv := http.NewServer(cfg, tkr)
servers = append(servers, srv)
}
if cfg.UDPConfig.ListenAddr != "" {
srv := udp.NewServer(cfg, tkr) srv := udp.NewServer(cfg, tkr)
servers = append(servers, srv) servers = append(servers, srv)
}
var wg sync.WaitGroup
for _, srv := range servers {
wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
srv.Serve(cfg.UDPListenAddr) srv.Serve()
}() }()
} }