Storage API matured, docs, tests & more

This commit is contained in:
Jimmy Zelinskie
2013-07-05 06:50:52 -04:00
parent 279c78192f
commit 5cb4e2fabb
11 changed files with 199 additions and 157 deletions

View File

@@ -14,11 +14,8 @@ import (
)
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
conn := s.connPool.Get()
defer conn.Close()
passkey, _ := path.Split(r.URL.Path)
_, err := validatePasskey(passkey, conn)
_, err := s.validatePasskey(passkey)
if err != nil {
fail(err, w, r)
return
@@ -42,12 +39,16 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
return
}
if !s.conf.ClientWhitelisted(pq.params["peer_id"]) {
ok, err := s.dataStore.ClientWhitelisted(pq.params["peer_id"])
if err != nil {
log.Panicf("server: %s", err)
}
if !ok {
fail(errors.New("Your client is not approved"), w, r)
return
}
torrent, exists, err := conn.FindTorrent(pq.params["infohash"])
torrent, exists, err := s.dataStore.FindTorrent(pq.params["infohash"])
if err != nil {
log.Panicf("server: %s", err)
}
@@ -56,7 +57,7 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
return
}
tx, err := conn.NewTx()
tx, err := s.dataStore.Begin()
if err != nil {
log.Panicf("server: %s", err)
}

View File

@@ -6,7 +6,6 @@ package server
import (
"errors"
"github.com/pushrax/chihaya/config"
"net/http"
"net/url"
"strconv"

View File

@@ -19,7 +19,7 @@ import (
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
passkey, _ := path.Split(r.URL.Path)
_, err := validatePasskey(passkey, s.storage)
_, err := s.validatePasskey(passkey)
if err != nil {
fail(err, w, r)
return
@@ -35,7 +35,7 @@ func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
bencode(w, "files")
if pq.infohashes != nil {
for _, infohash := range pq.infohashes {
torrent, exists, err := s.storage.FindTorrent(infohash)
torrent, exists, err := s.dataStore.FindTorrent(infohash)
if err != nil {
log.Panicf("server: %s", err)
}
@@ -45,7 +45,7 @@ func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
}
}
} else if infohash, exists := pq.params["info_hash"]; exists {
torrent, exists, err := s.storage.FindTorrent(infohash)
torrent, exists, err := s.dataStore.FindTorrent(infohash)
if err != nil {
log.Panicf("server: %s", err)
}

View File

@@ -23,9 +23,9 @@ import (
)
type Server struct {
conf *config.Config
listener net.Listener
connPool storage.Pool
conf *config.Config
listener net.Listener
dataStore storage.DS
serving bool
startTime time.Time
@@ -39,14 +39,14 @@ type Server struct {
}
func New(conf *config.Config) (*Server, error) {
pool, err := storage.Open(&conf.Storage)
ds, err := storage.Open(&conf.Storage)
if err != nil {
return nil, err
}
s := &Server{
conf: conf,
storage: pool,
conf: conf,
dataStore: ds,
Server: http.Server{
Addr: conf.Addr,
ReadTimeout: conf.ReadTimeout.Duration,
@@ -76,7 +76,7 @@ func (s *Server) ListenAndServe() error {
func (s *Server) Stop() error {
s.serving = false
s.waitgroup.Wait()
err := s.storage.Close()
err := s.dataStore.Close()
if err != nil {
return err
}
@@ -129,13 +129,13 @@ func fail(err error, w http.ResponseWriter, r *http.Request) {
w.(http.Flusher).Flush()
}
func validatePasskey(dir string, s storage.Conn) (*storage.User, error) {
func (s *Server) validatePasskey(dir string) (*storage.User, error) {
if len(dir) != 34 {
return nil, errors.New("Your passkey is invalid")
return nil, errors.New("Passkey is invalid")
}
passkey := dir[1:33]
user, exists, err := s.FindUser(passkey)
user, exists, err := s.dataStore.FindUser(passkey)
if err != nil {
log.Panicf("server: %s", err)
}