GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

44 lines
814 B

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
}