forked from MG-RAST/Shock
/
db.go
44 lines (38 loc) · 827 Bytes
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package db
import (
"fmt"
"github.com/MG-RAST/Shock/shock-server/conf"
"labix.org/v2/mgo"
"os"
"time"
)
const (
DbTimeout = time.Duration(time.Second * 1)
)
var (
Connection connection
)
type connection struct {
dbname string
username string
password string
Session *mgo.Session
DB *mgo.Database
}
func Initialize() {
c := connection{}
s, err := mgo.DialWithTimeout(conf.Conf["mongodb-hosts"], DbTimeout)
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: no reachable mongodb servers")
os.Exit(1)
}
c.Session = s
c.DB = c.Session.DB(conf.Conf["mongodb-database"])
if conf.Conf["mongodb-user"] != "" && conf.Conf["mongodb-password"] != "" {
c.DB.Login(conf.Conf["mongodb-user"], conf.Conf["mongodb-password"])
}
Connection = c
}
func Drop() error {
return Connection.DB.DropDatabase()
}