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

  1. package mongodb
  2. import (
  3. "fmt"
  4. "time"
  5. "git.aiterp.net/rpdata/api/internal/config"
  6. "git.aiterp.net/rpdata/api/repositories"
  7. "github.com/globalsign/mgo"
  8. )
  9. // Init initializes the mongodb database
  10. func Init(cfg config.Database) (bundle *repositories.Bundle, closeFn func() error, err error) {
  11. port := cfg.Port
  12. if port <= 0 {
  13. port = 27017
  14. }
  15. session, err := mgo.DialWithInfo(&mgo.DialInfo{
  16. Addrs: []string{fmt.Sprintf("%s:%d", cfg.Host, port)},
  17. Timeout: 30 * time.Second,
  18. Database: cfg.Db,
  19. Username: cfg.Username,
  20. Password: cfg.Password,
  21. Mechanism: cfg.Mechanism,
  22. Source: cfg.Db,
  23. })
  24. if err != nil {
  25. return
  26. }
  27. db := session.DB(cfg.Db)
  28. bundle = &repositories.Bundle{
  29. Tags: newTagRepository(db),
  30. }
  31. closeFn = func() error {
  32. session.Close()
  33. return nil
  34. }
  35. return
  36. }