Skip to content

Instantly share code, notes, and snippets.

@ctcarrier
Created April 27, 2014 15:17
Show Gist options
  • Save ctcarrier/11348157 to your computer and use it in GitHub Desktop.
Save ctcarrier/11348157 to your computer and use it in GitHub Desktop.
ReactiveMongoConnection
class ReactiveMongoConnection extends Module {
import ExecutionContext.Implicits.global
private val config = ConfigFactory.load()
implicit val context = inject[ActorSystem]
val driver = new MongoDriver
val pattern = "^mongodb:\\/\\/([\\w]*):([\\w]*)@([\\w\\.-]+):([\\d]+)\\/([\\w]+)".r
val envUri = Properties.envOrElse("MONGOLAB_URI", "").toString
val (connection, db) = if (!envUri.isEmpty){
val pattern(user, password, host, port, dbName) = envUri
val connection = driver.connection(List("%s:%s".format(host, port)))
val userName =Properties.envOrElse("MONGODB_USER", "FAIL")
val pass = Properties.envOrElse("MONGODB_PASS", "FAIL")
// Gets a reference to the database "plugin"
val db = connection(dbName)
val authResult = Await.result(db.authenticate(userName, pass)(120.seconds), 120.seconds)
(connection, db)
}
else {
val connection = driver.connection(List(config.getString("mongodb.url")))
// Gets a reference to the database "plugin"
val db = connection(config.getString("mongodb.database"))
(connection, db)
}
// Gets a reference to the collection "acoll"
// By default, you get a BSONCollection.
bind[DB] to db
bind[BSONCollection] as 'vaultCollection to db(config.getString("infescrow.vault.collection"))
bind[BSONCollection] as 'dataCollection to db(config.getString("infescrow.vaultdata.collection"))
bind[BSONCollection] as 'userCollection to db(config.getString("infescrow.user.collection"))
bind[BSONCollection] as 'inviteCollection to db(config.getString("infescrow.invite.collection"))
bind[BSONCollection] as 'stateCollection to db(config.getString("infescrow.userstate.collection"))
// userCollection.indexesManager.ensure(Index(
// key = Seq("email" -> IndexType.Ascending),
// unique = true,
// dropDups = true
// ))
}
@OlegIlyenko
Copy link

I would rewrite it this way, to make sure that inject is called lazily:

class ReactiveMongoConnection extends Module {
  binding to ConfigFactory.load()

  bind[DB] to {
    import ExecutionContext.Implicits.global

    implicit val context = inject [ActorSystem]

    val driver = new MongoDriver

    val pattern = "^mongodb:\\/\\/([\\w]*):([\\w]*)@([\\w\\.-]+):([\\d]+)\\/([\\w]+)".r

    val envUri = Properties.envOrElse("MONGOLAB_URI", "").toString

    if (!envUri.isEmpty){
      val pattern(user, password, host, port, dbName) = envUri

      val connection = driver.connection(List("%s:%s".format(host, port)))

      val userName =Properties.envOrElse("MONGODB_USER", "FAIL")
      val pass = Properties.envOrElse("MONGODB_PASS", "FAIL")

      // Gets a reference to the database "plugin"
      val db = connection(dbName)
      val authResult = Await.result(db.authenticate(userName, pass)(120.seconds), 120.seconds)

      db
    } else {
      val connection = driver.connection(List(inject[Config].getString("mongodb.url")))

      // Gets a reference to the database "plugin"
      val db = connection(inject[Config].getString("mongodb.database"))

      db
    }
  }

  bind[BSONCollection] as 'vaultCollection to db(inject[Config].getString("infescrow.vault.collection"))
  bind[BSONCollection] as 'dataCollection to db(inject[Config].getString("infescrow.vaultdata.collection"))
  bind[BSONCollection] as 'inviteCollection to db(inject[Config].getString("infescrow.invite.collection"))
  bind[BSONCollection] as 'stateCollection to db(inject[Config].getString("infescrow.userstate.collection"))
  bind[BSONCollection] as 'userCollection to db(inject[Config].getString("infescrow.user.collection")) initWith { collection =>
    collection.indexesManager.ensure(Index(
      key = Seq("email" -> IndexType.Ascending),
      unique = true,
      dropDups = true
    )) 
  }

  private def db = inject [DB]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment