From 9884a7d4d2c6d29007b9e18a2cf78e5af023d789 Mon Sep 17 00:00:00 2001 From: Oleg Balunenko Date: Sun, 7 Oct 2018 03:07:46 +0300 Subject: [PATCH] Refactor to remove circle dependency --- main.go | 2 +- mongo/db.go | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index e2502b3..e5c1cb8 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,7 @@ func main() { log.Fatalf("Failed to load config: %v \nExiting", errLoadCfg) } - db := mongo.NewConnection(cfg) + db := mongo.NewConnection(cfg.MongoURL, cfg.MongoDB, cfg.MongoCollection, cfg.MongoUsername, cfg.MongoPassword) if cfg.DropDB { db.DropDatabase() diff --git a/mongo/db.go b/mongo/db.go index 90582e3..72e2064 100644 --- a/mongo/db.go +++ b/mongo/db.go @@ -4,7 +4,6 @@ import ( "fmt" "time" - "github.com/oleg-balunenko/logs-converter/config" log "github.com/sirupsen/logrus" mgo "gopkg.in/mgo.v2" @@ -18,14 +17,14 @@ type DB struct { } // NewConnection establishes connection with mongoDB and return MongoDB object -func NewConnection(cfg *config.Config) *DB { +func NewConnection(url, dbName, collectionName, username, password string) *DB { mongoDBDialInfo := &mgo.DialInfo{ - Addrs: []string{cfg.MongoURL}, + Addrs: []string{url}, Timeout: 60 * time.Second, - Database: cfg.MongoDB, - Username: cfg.MongoUsername, - Password: cfg.MongoPassword, + Database: dbName, + Username: username, + Password: password, } session, errDial := mgo.DialWithInfo(mongoDBDialInfo) @@ -33,8 +32,8 @@ func NewConnection(cfg *config.Config) *DB { log.Fatalf("Could not leave without Mongo: %v\nExiting...", errDial) } - database := session.DB(cfg.MongoDB) - collection := database.C(cfg.MongoCollection) + database := session.DB(dbName) + collection := database.C(collectionName) return &DB{ session: session,