package mongodb import ( "fmt" "time" "git.aiterp.net/rpdata/api/internal/config" "git.aiterp.net/rpdata/api/repositories" "github.com/globalsign/mgo" ) // Init initializes the mongodb database func Init(cfg config.Database) (bundle *repositories.Bundle, closeFn func() error, err error) { port := cfg.Port if port <= 0 { port = 27017 } session, err := mgo.DialWithInfo(&mgo.DialInfo{ Addrs: []string{fmt.Sprintf("%s:%d", cfg.Host, port)}, Timeout: 30 * time.Second, Database: cfg.Db, Username: cfg.Username, Password: cfg.Password, Mechanism: cfg.Mechanism, Source: cfg.Db, }) if err != nil { return } db := session.DB(cfg.Db) bundle = &repositories.Bundle{ Tags: newTagRepository(db), } closeFn = func() error { session.Close() return nil } return }