Skip to content
This repository has been archived by the owner on Mar 10, 2021. It is now read-only.

Commit

Permalink
Refactor to remove circle dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko committed Oct 7, 2018
1 parent 4c582db commit 9884a7d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 7 additions & 8 deletions mongo/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"time"

"github.com/oleg-balunenko/logs-converter/config"
log "github.com/sirupsen/logrus"

mgo "gopkg.in/mgo.v2"
Expand All @@ -18,23 +17,23 @@ 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)
if errDial != nil {
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,
Expand Down

0 comments on commit 9884a7d

Please sign in to comment.