Created
September 4, 2017 10:28
-
-
Save vuongngo/2b263e4521570f0e1c4e20f6686e012d to your computer and use it in GitHub Desktop.
This file contains 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
package db | |
import ( | |
"errors" | |
"gopkg.in/mgo.v2" | |
) | |
/* | |
Mongo construction | |
*/ | |
//Mongo struct hold uri based on environment | |
type MONGO struct { | |
Uri string | |
Database string | |
Session *mgo.Session | |
} | |
//Establish connection to mongodb | |
func (mongo *MONGO) Dial() { | |
session, err := mgo.Dial(mongo.Uri) | |
if err != nil { | |
panic(err) | |
} | |
mongo.Session = session | |
} | |
// Get session | |
func (mongo *MONGO) GetSession() (*mgo.Database, *mgo.Session) { | |
if mongo.Session == nil { | |
panic(errors.New("Db session not exist")) | |
return nil, nil | |
} | |
newSession := mongo.Session.Copy() | |
db := newSession.DB(mongo.Database) | |
return db, newSession | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment