From 4f2d7b8cd0e3d6609b3cbf4803ccfee6beac1528 Mon Sep 17 00:00:00 2001 From: lealife Date: Wed, 9 Sep 2015 10:10:24 +0800 Subject: [PATCH] get mongodb configuration from env --- app/db/Mgo.go | 9 +++++++++ conf/app.conf | 1 + 2 files changed, 10 insertions(+) diff --git a/app/db/Mgo.go b/app/db/Mgo.go index c062860..9b79dd2 100644 --- a/app/db/Mgo.go +++ b/app/db/Mgo.go @@ -6,6 +6,7 @@ import ( . "github.com/leanote/leanote/app/lea" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" + "strings" ) // Init mgo and the common DAO @@ -62,6 +63,14 @@ func Init(url, dbname string) { config := revel.Config if url == "" { url, ok = config.String("db.url") + if !ok { + url, ok = config.String("db.urlEnv") + } + if ok { + // get dbname from url + urls := strings.Split(url, "/") + dbname = urls[len(urls)-1] + } } if dbname == "" { dbname, _ = config.String("db.dbname") diff --git a/conf/app.conf b/conf/app.conf index d7ebd1b..67d309a 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -15,6 +15,7 @@ db.password= # if not exists, please leave it blank # or you can set the mongdb url for more complex needs the format is: # mongodb://myuser:mypass@localhost:40001,otherhost:40001/mydb # db.url=mongodb://root:root123@localhost:27017/leanote +db.urlEnv=${MONGODB_URL} # set url from env # You Must Change It !! About Security!! app.secret=V85ZzBeTnzpsHyjQX4zukbQ8qqtju9y2aDM55VWxAH9Qop19poekx3xkcDVvrD0y