78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
|
// dbs
|
||
|
package m2r
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
// rt "gopkg.in/dancannon/gorethink.v2"
|
||
|
"gopkg.in/mgo.v2"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
dbs = Dbs{
|
||
|
MongoDB: "lastlogin",
|
||
|
RethinkDB: "lastlogin",
|
||
|
}
|
||
|
)
|
||
|
|
||
|
// Dbs structure
|
||
|
type Dbs struct {
|
||
|
MongoURI string
|
||
|
MongoDB string
|
||
|
RethinkURI string
|
||
|
RethinkDB string
|
||
|
rtdb *Rethink
|
||
|
mdb *mgo.Session
|
||
|
ll *mgo.Collection
|
||
|
}
|
||
|
|
||
|
// MongoLogin structure
|
||
|
type MongoLogin struct {
|
||
|
ID string `json:"_id" bson:"_id" gorethink:"id"`
|
||
|
User string `json:"user" bson:"user" gorethink:"user"`
|
||
|
Protocol string `json:"protocol" bson:"protocol" gorethink:"protocol"`
|
||
|
IP string `json:"ip" bson:"ip" gorethink:"ip"`
|
||
|
Date time.Time `json:"date" bson:"date" gorethink:"date"`
|
||
|
Insert time.Time `json:"insert" bson:"insert" gorethink:"insert"`
|
||
|
}
|
||
|
|
||
|
func (db *Dbs) poolRethink() {
|
||
|
var err error
|
||
|
|
||
|
if opts.Debug {
|
||
|
fmt.Printf("DBS: %+v\n", db)
|
||
|
}
|
||
|
|
||
|
if db.RethinkURI != "" {
|
||
|
uri := strings.Split(dbs.RethinkURI, ",")
|
||
|
if opts.Debug {
|
||
|
fmt.Printf("RT_URI: %s\n", uri)
|
||
|
}
|
||
|
db.rtdb, err = NewRethinkDB(uri)
|
||
|
if err != nil {
|
||
|
fmt.Println("RethinkDB connect Error: ", err.Error())
|
||
|
os.Exit(-4)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if opts.Debug {
|
||
|
fmt.Printf("DBS: %+v\n", db)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func (db *Dbs) connectMongo() {
|
||
|
var err error
|
||
|
db.mdb, err = mgo.Dial(db.MongoURI)
|
||
|
if err != nil {
|
||
|
log.Println("Mongodb connect Error: ", err.Error())
|
||
|
os.Exit(-3)
|
||
|
}
|
||
|
|
||
|
db.ll = db.mdb.DB(db.MongoDB).C(fmt.Sprintf("lastlogin_%s", opts.Month))
|
||
|
}
|