123456789101112131415161718192021222324252627282930313233343536373839 |
- package parser
- import (
- "errors"
- "fmt"
- "gopkg.in/mgo.v2"
- "property-garden/config"
- )
- var Session *mgo.Session
- func MgoHandler(conf *config.Configure) {
- err := MgoInit(conf.Mgo.Addr, conf.Mgo.User, conf.Mgo.Password)
- if err != nil {
- panic("mongodb error:"+err.Error())
- }
- }
- func MgoInit(addr string, username string, password string) error {
- var err error
- var session *mgo.Session
- if addr == "" {
- return errors.New("mongo init need addr")
- }
- if username == "" && password == "" {
- //fmt.Printf("1:%v,%v,%v\n", addr, username, password)
- session, err = mgo.Dial(addr)
- } else {
- //fmt.Printf("2:%v,%v,%v\n", addr, username, password)
- url := fmt.Sprintf("mongodb://%s:%s@%s", username, password, addr)
- session, err = mgo.Dial(url)
- }
- if err != nil {
- return err
- }
- Session = session
- return nil
- }
|