1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-10 09:52:51 +00:00

moved server to separate repo

This commit is contained in:
2016-08-28 19:21:54 -05:00
parent 1c224f95ab
commit 29448c29a8
13 changed files with 2 additions and 796 deletions

View File

@@ -1,66 +0,0 @@
package db
import (
"gopkg.in/mgo.v2"
"log"
"time"
)
var Mongo Driver
type Driver struct {
Session *mgo.Session
Info DatabaseInfo
}
type DatabaseInfo struct {
URL string `json:"url"`
Database string `json:"database"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
func Configure(d DatabaseInfo) {
Mongo.Info = d
}
func (d *Driver) Connect() {
if (d.Info.URL != ""){
// Connect to MongoDB
database_info := &mgo.DialInfo{
Addrs: []string{d.Info.URL},
Database: d.Info.Database,
Timeout: 5*time.Second,
Username: d.Info.Username,
Password: d.Info.Password,
}
s, err := mgo.DialWithInfo(database_info)
if err != nil {
log.Println("MongoDB Driver Error", err)
return
}
d.Session = s
// Prevents these errors: read tcp 127.0.0.1:27017: i/o timeout
d.Session.SetSocketTimeout(10 * time.Second)
// Check if is alive
if err = d.Session.Ping(); err != nil {
log.Println("Database Error", err)
}
log.Println("Connected to database")
} else {
log.Println("Database not configured")
}
}
func (d *Driver) Connected() bool {
if d.Session != nil {
return true
}
return false
}