scala-news-reader
rss/atom news reader in scala
git clone https://9o.is/git/scala-news-reader.git
MongoConfig.scala
(1450B)
1 package com.joereader
2 package config
3
4 import net.liftweb._
5 import common._
6 import mongodb._
7 import util.Props
8
9 import com.mongodb.{DBAddress, Mongo}
10
11 object MongoConfig extends Loggable {
12
13 def init() {
14 /**
15 * First checks for existence of mongo.default.url. If not found, then
16 * checks for mongo.default.host, port, and name. Uses defaults if those
17 * are not found.
18 */
19 val defaultDbAddress = Props.get("mongo.default.url")
20 .map(url => new DBAddress(url))
21 .openOr(new DBAddress(
22 Props.get("mongo.default.host", "127.0.0.1"),
23 Props.getInt("mongo.default.port", 27017),
24 Props.get("mongo.default.name", "joe-reader")
25 ))
26
27 /*
28 * If mongo.default.user, and pwd are defined,
29 * configure Mongo using authentication.
30 */
31 (Props.get("mongo.default.user"), Props.get("mongo.default.pwd")) match {
32 case (Full(user), Full(pwd)) =>
33 MongoDB.defineDbAuth(
34 DefaultMongoIdentifier,
35 new Mongo(defaultDbAddress),
36 defaultDbAddress.getDBName,
37 user,
38 pwd
39 )
40
41 logger.info("MongoDB inited using authentication: %s".
42 format(defaultDbAddress.toString))
43
44 case _ =>
45 MongoDB.defineDb(
46 DefaultMongoIdentifier,
47 new Mongo(defaultDbAddress),
48 defaultDbAddress.getDBName
49 )
50
51 logger.info("MongoDB inited: %s".format(defaultDbAddress.toString))
52 }
53 }
54 }
55