diff --git a/cache/redis/redis.go b/cache/redis/redis.go index d87f451..84b006d 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -41,7 +41,7 @@ func (d *driver) New(conf *config.DataStore) cache.Pool { func makeDialFunc(conf *config.DataStore) func() (redis.Conn, error) { return func() (conn redis.Conn, err error) { - conn, err = redis.Dial(conf.Network, conf.Addr) + conn, err = redis.Dial(conf.Network, conf.Host+":"+conf.Port) if err != nil { return nil, err } diff --git a/config/config.go b/config/config.go index 24b06ea..c751909 100644 --- a/config/config.go +++ b/config/config.go @@ -31,7 +31,8 @@ func (d *Duration) UnmarshalJSON(b []byte) error { type DataStore struct { Driver string `json:"driver"` Network string `json:"network` - Addr string `json:"addr"` + Host string `json:"host"` + Port string `json:"port"` Username string `json:"user"` Password string `json:"pass"` Schema string `json:"schema,omitempty"` diff --git a/config/config_test.go b/config/config_test.go index e5bb22c..c63f7f0 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -5,95 +5,25 @@ package config import ( - "bufio" + "bytes" + "io/ioutil" "os" - "path/filepath" - "strings" "testing" ) -var exampleJson = `{ - - "network": "tcp", - "addr": ":34000", - "pub_addr": "tcp://*:34001", - - "cache": { - "driver": "redis", - "addr": "127.0.0.1:6379", - "user": "root", - "pass": "", - "prefix": "test:", - - "max_idle_conn": 3, - "idle_timeout": "240s" - }, - - "storage": { - "driver": "batter", - "addr": "127.0.0.1:5432", - "user": "postgres", - "pass": "" - }, - - "private": true, - "freeleech": false, - - "announce": "30m", - "min_announce": "15m", - "read_timeout": "20s", - "default_num_want": 50, - - "tx_retries": 3 -}` - -func TestNewConfig(t *testing.T) { - if _, err := newConfig(strings.NewReader(exampleJson)); err != nil { +func TestOpenConfig(t *testing.T) { + if _, err := Open(os.ExpandEnv("$GOPATH/src/github.com/pushrax/chihaya/config/example.json")); err != nil { t.Error(err) } } -func writeAndOpenJsonTest(t *testing.T, fn string) { - expandFn := os.ExpandEnv(fn) - // Write JSON to relative path, clean up - tfile, ferr := os.Create(expandFn) - // Remove failure not counted as error - defer os.Remove(expandFn) - if ferr != nil { - t.Fatal("Failed to create %s. Error: %v", expandFn, ferr) - } - - tWriter := bufio.NewWriter(tfile) - cw, err := tWriter.WriteString(exampleJson) +func TestNewConfig(t *testing.T) { + contents, err := ioutil.ReadFile(os.ExpandEnv("$GOPATH/src/github.com/pushrax/chihaya/config/example.json")) if err != nil { - t.Fatal("Failed to write json to config file. %v", err) + t.Error(err) } - if cw < len(exampleJson) { - t.Error("Incorrect length of config file written %v vs. %v", cw, len(exampleJson)) - } - fErr := tWriter.Flush() - if fErr != nil { - t.Error("Flush error: %v", fErr) - } - _, oErr := Open(fn) - if oErr != nil { - t.Error("Open error: %v", oErr) - } -} - -// These implcitly require the test program have -// read/write/delete file system permissions -func TestOpenCurDir(t *testing.T) { - if !testing.Short() { - writeAndOpenJsonTest(t, "testConfig.json") - } else { - t.Log("Write/Read file test skipped") - } -} -func TestOpenAbsEnvPath(t *testing.T) { - if !testing.Short() { - writeAndOpenJsonTest(t, filepath.Join(os.TempDir(), "testConfig.json")) - } else { - t.Log("Write/Read file test skipped") + buff := bytes.NewBuffer(contents) + if _, err := newConfig(buff); err != nil { + t.Error(err) } } diff --git a/config/example.json b/config/example.json new file mode 100644 index 0000000..cd1ae21 --- /dev/null +++ b/config/example.json @@ -0,0 +1,36 @@ +{ + + "network": "tcp", + "addr": ":80", + + "cache": { + "driver": "redis", + "host": "127.0.0.1", + "port": "6379", + "user": "root", + "pass": "", + "prefix": "test:", + + "max_idle_conn": 3, + "idle_timeout": "240s" + }, + + "storage": { + "driver": "batter", + "host": "127.0.0.1", + "port": "5432", + "user": "postgres", + "pass": "" + }, + + "private": true, + "freeleech": false, + + "announce": "30m", + "min_announce": "15m", + "read_timeout": "20s", + "default_num_want": 50, + + "tx_retries": 3 + +} diff --git a/server/addremove.go b/server/addremove.go new file mode 100644 index 0000000..560d485 --- /dev/null +++ b/server/addremove.go @@ -0,0 +1,17 @@ +// 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. + +package server + +import ( + "net/http" +) + +func (s Server) serveAdd(w http.ResponseWriter, r *http.Request) { + return +} + +func (s Server) serveRemove(w http.ResponseWriter, r *http.Request) { + return +} diff --git a/server/server.go b/server/server.go index 52f541f..d23cb7d 100644 --- a/server/server.go +++ b/server/server.go @@ -96,9 +96,16 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer s.waitgroup.Done() defer atomic.AddInt64(&s.deltaRequests, 1) - if r.URL.Path == "/stats" { + switch r.URL.Path { + case "/stats": s.serveStats(w, r) return + case "/add": + s.serveAdd(w, r) + return + case "/remove": + s.serveRemove(w, r) + return } _, action := path.Split(r.URL.Path) diff --git a/server/stats_test.go b/server/stats_test.go new file mode 100644 index 0000000..bd0f31b --- /dev/null +++ b/server/stats_test.go @@ -0,0 +1,40 @@ +// 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. + +package server + +import ( + "errors" + "fmt" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/pushrax/chihaya/config" + + _ "github.com/pushrax/chihaya/cache/redis" + _ "github.com/pushrax/chihaya/storage/batter" +) + +func TestStats(t *testing.T) { + testConfig, err := config.Open(os.ExpandEnv("$GOPATH/src/github.com/pushrax/chihaya/config/example.json")) + if err != nil { + t.Error(err) + } + fmt.Println(testConfig) + s, err := New(testConfig) + if err != nil { + t.Error(err) + } + r, err := http.NewRequest("GET", "127.0.0.1:80/stats", nil) + if err != nil { + t.Error(err) + } + w := httptest.NewRecorder() + s.serveStats(w, r) + if w.Code != 200 { + t.Error(errors.New("/stats did not return HTTP 200")) + } +} diff --git a/storage/batter/batter.go b/storage/batter/batter.go new file mode 100644 index 0000000..4badba2 --- /dev/null +++ b/storage/batter/batter.go @@ -0,0 +1,52 @@ +// 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. + +// Package batter provides a driver for a BitTorrent tracker to interface +// with the postgres database used by batter (github.com/wafflesfm/batter). +package batter + +import ( + "database/sql" + "fmt" + + "github.com/pushrax/chihaya/config" + "github.com/pushrax/chihaya/models" + "github.com/pushrax/chihaya/storage" + + _ "github.com/bmizerany/pq" +) + +type driver struct{} + +func (d *driver) New(conf *config.DataStore) storage.Conn { + dsn := fmt.Sprintf( + "host=%s user=%s password=%s dbname=%s", + conf.Host, + conf.Port, + conf.Username, + conf.Password, + conf.Schema, + ) + db, err := sql.Open("postgres", dsn) + if err != nil { + panic("batter: failed to open connection to postgres") + } + return &Conn{db} +} + +type Conn struct { + *sql.DB +} + +func (c *Conn) UpdateTorrents(t []models.Torrent) error { + return nil +} + +func (c *Conn) UpdateUsers(u []models.User) error { + return nil +} + +func init() { + storage.Register("batter", &driver{}) +}