57 lines
1,012 B
Go
57 lines
1,012 B
Go
// dbs
|
|
package main
|
|
|
|
import (
|
|
"github.com/fzzy/radix/redis"
|
|
"gopkg.in/mgo.v2"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
dbs = Dbs{
|
|
MongoUri: "mongodb://127.0.0.1:27018",
|
|
RedisUri: "127.0.0.1:6379",
|
|
}
|
|
)
|
|
|
|
type Dbs struct {
|
|
MongoUri string
|
|
RedisUri string
|
|
rdb *redis.Client
|
|
mdb *mgo.Session
|
|
ll *mgo.Collection
|
|
}
|
|
|
|
type MongoLogin struct {
|
|
User string `json:"user"`
|
|
Protocol string `json:"protocol"`
|
|
Ip string `json:"ip"`
|
|
Date time.Time `json:"date"`
|
|
Read bool `json:"read"`
|
|
}
|
|
|
|
type Index struct {
|
|
User string `json:"user"`
|
|
Date time.Time `json:"date"`
|
|
}
|
|
|
|
func (db *Dbs) connectRedis() {
|
|
var err error
|
|
db.rdb, err = redis.Dial("tcp", db.RedisUri)
|
|
if err != nil {
|
|
log.Println("Redis connect Error: ", err.Error())
|
|
os.Exit(-1)
|
|
}
|
|
}
|
|
|
|
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("dovecot").C("lastlogin")
|
|
}
|