From c082ceeefcdb9ae195897c5dabf8e12abd595f13 Mon Sep 17 00:00:00 2001 From: Ian Clark Date: Sun, 11 Dec 2016 21:29:17 -0600 Subject: [PATCH 1/3] Issue #179 Changed default implementations of `mysql` found in the `reaper/reaper.go` file. Now, depending on the config, we launch a new connection to whatever is the specified database. CHANGE: - config.yaml Now defaults to postgres (this is only important for my development environment. --- config.yaml | 6 +++--- database/mysql/mysql.go | 12 +++--------- reaper/reaper.go | 26 ++++++++++++++++++++++---- server/server.go | 16 ++++++++++++++-- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/config.yaml b/config.yaml index ebfee45..9760f2a 100644 --- a/config.yaml +++ b/config.yaml @@ -5,9 +5,9 @@ UsePostgres: true UseMySQL: false DBHost: localhost -DBPort: "3306" -DBUser: testuser -DBPass: testuser +DBPort: "5432" +DBUser: postgres +DBPass: postgres DBName: testdb # Redis Options RedisServer: localhost diff --git a/database/mysql/mysql.go b/database/mysql/mysql.go index 46d32cc..892edd6 100644 --- a/database/mysql/mysql.go +++ b/database/mysql/mysql.go @@ -14,13 +14,7 @@ import ( // MysqlHost listed in the config func OpenConnection() (db *gorm.DB, err error) { c := config.LoadConfig() - - db, err = gorm.Open("mysql", formatConnectString(c)) - if err != nil { - err = fmt.Errorf("Failed to open connection to MySQL: %v", err) - } - - return + return OpenConnectionWithConfig(&c) } // OpenConnectionWithConfig does as its name dictates and opens a connection to the @@ -28,10 +22,10 @@ func OpenConnection() (db *gorm.DB, err error) { func OpenConnectionWithConfig(cfg *config.ConfigStruct) (db *gorm.DB, err error) { db, err = gorm.Open("mysql", formatConnectString(*cfg)) if err != nil { - err = fmt.Errorf("Failed to open connection to MySQL: %v", err) + return nil, fmt.Errorf("Failed to open connection to MySQL: %v", err) } - return + return db, nil } // InitDB initializes database tables. diff --git a/reaper/reaper.go b/reaper/reaper.go index 976807a..6e94d81 100644 --- a/reaper/reaper.go +++ b/reaper/reaper.go @@ -2,7 +2,9 @@ package reaper import ( "fmt" + "github.com/GrappigPanda/notorious/config" "github.com/GrappigPanda/notorious/database/mysql" + "github.com/GrappigPanda/notorious/database/postgres" "github.com/GrappigPanda/notorious/peerStore/redis" "gopkg.in/redis.v3" "strconv" @@ -83,10 +85,26 @@ func StartReapingScheduler(waitTime time.Duration) { addedBy := new(string) dateAdded := new(int64) - x, err := mysql.GetWhitelistedTorrents(nil) - for x.Next() { - x.Scan(infoHash, name, addedBy, dateAdded) - redisPeerStore.CreateNewTorrentKey(nil, *infoHash) + cfg := config.LoadConfig() + + if cfg.DBChoice == "mysql" { + whitelistedTorrents, err := mysql.GetWhitelistedTorrents(nil) + if err == nil { + for whitelistedTorrents.Next() { + whitelistedTorrents.Scan(infoHash, name, addedBy, dateAdded) + redisPeerStore.CreateNewTorrentKey(nil, *infoHash) + } + } + } else if cfg.DBChoice == "postgres" { + whitelistedTorrents, err := postgres.GetWhitelistedTorrents(nil) + if err == nil { + for whitelistedTorrents.Next() { + whitelistedTorrents.Scan(infoHash, name, addedBy, dateAdded) + redisPeerStore.CreateNewTorrentKey(nil, *infoHash) + } + } + } else { + panic("Invalid DBChoice encountered when starting the peer reaper.") } // Start the actual peer reaper. diff --git a/server/server.go b/server/server.go index 12b26a4..8cb6160 100644 --- a/server/server.go +++ b/server/server.go @@ -156,11 +156,23 @@ func writeResponse(w http.ResponseWriter, values string) { // RunServer spins up the server and muxes the routes. func RunServer() { + // Load the config and initiate a `SQLStore` implementation. + var sqlObj db.SQLStore + cfg := config.LoadConfig() + + if cfg.DBChoice == "mysql" { + sqlObj = new(sqlStoreImpl.MySQLStore) + } else if cfg.DBChoice == "postgres" { + sqlObj = new(sqlStoreImpl.PostgresStore) + } else { + panic("Invalid config value for DBChoice. Must be either postgres or MySQL.") + } + app := applicationContext{ - config: config.LoadConfig(), + config: cfg, trackerLevel: a.RATIOLESS, peerStoreClient: new(redisPeerStoreImpl.RedisStore), - sqlObj: new(sqlStoreImpl.MySQLStore), + sqlObj: sqlObj, } mux := http.NewServeMux() From 5dc908feff8c019fbd4db02c5a8a22cab721b164 Mon Sep 17 00:00:00 2001 From: Ian Clark Date: Sun, 11 Dec 2016 21:56:52 -0600 Subject: [PATCH 2/3] Issue #179 Attempting to resolve build failures --- database/mysql/database_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/database/mysql/database_test.go b/database/mysql/database_test.go index 23bfa78..ec51366 100644 --- a/database/mysql/database_test.go +++ b/database/mysql/database_test.go @@ -18,14 +18,13 @@ var CONFIG = config.ConfigStruct{ false, } -var DBCONN, _ = OpenConnectionWithConfig(&CONFIG) +var DBCONN, ERR = OpenConnectionWithConfig(&CONFIG) -func TestOpenConn(t *testing.T) { - dbConn, err := OpenConnection() - if err != nil { - t.Fatalf("%v", err) +func TestOpenConnPostgres(t *testing.T) { + if ERR != nil { + t.Fatalf("Unable to connect %v", ERR) } - InitDB(dbConn) + InitDB(DBCONN) } func TestAddWhitelistedTorrent(t *testing.T) { From 444a7a41da2ffc0f36572d8179d926780c83e4c7 Mon Sep 17 00:00:00 2001 From: Ian Clark Date: Sun, 11 Dec 2016 22:00:17 -0600 Subject: [PATCH 3/3] Issue #179 Updatin config back to original version. --- config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config.yaml b/config.yaml index 9760f2a..9e0848d 100644 --- a/config.yaml +++ b/config.yaml @@ -5,9 +5,9 @@ UsePostgres: true UseMySQL: false DBHost: localhost -DBPort: "5432" -DBUser: postgres -DBPass: postgres +DBPort: "3306" +DBUser: testuser +DBPass: testuser DBName: testdb # Redis Options RedisServer: localhost