Created
April 27, 2014 15:17
-
-
Save ctcarrier/11348157 to your computer and use it in GitHub Desktop.
ReactiveMongoConnection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
// )) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would rewrite it this way, to make sure that
inject
is called lazily: