94 lines
1.7 KiB
Go
94 lines
1.7 KiB
Go
// dbs
|
|
package main
|
|
|
|
import (
|
|
"github.com/garyburd/redigo/redis"
|
|
// "github.com/fzzy/radix/redis"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"gopkg.in/mgo.v2"
|
|
)
|
|
|
|
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.Pool //*redis.Client
|
|
mdb *mgo.Session
|
|
ll *mgo.Collection
|
|
// us *mgo.Collection
|
|
}
|
|
|
|
type MongoLogin struct {
|
|
User string `json:"user"`
|
|
Protocol string `json:"protocol"`
|
|
Ip string `json:"ip"`
|
|
Date time.Time `json:"date"`
|
|
}
|
|
|
|
type Ips struct {
|
|
Ip string `json:"ip"`
|
|
}
|
|
|
|
type UserLogin struct {
|
|
User string `json:"user"`
|
|
Date time.Time `json:"date"`
|
|
Lock bool `json:"lock"`
|
|
}
|
|
|
|
type Index struct {
|
|
User string `json:"user"`
|
|
Date time.Time `json:"date"`
|
|
}
|
|
|
|
func (db *Dbs) poolRedis() {
|
|
|
|
dbs.rdb = &redis.Pool{
|
|
MaxIdle: 128,
|
|
MaxActive: 1000,
|
|
Wait: true,
|
|
IdleTimeout: 1 * time.Second,
|
|
Dial: func() (redis.Conn, error) {
|
|
c, err := redis.Dial("tcp", db.RedisUri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c, err
|
|
},
|
|
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
|
_, err := c.Do("PING")
|
|
return err
|
|
},
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
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("lastlogin").C("lastlogin" + time.Now().Format("0601"))
|
|
// db.us = db.mdb.DB("dovecot").C("userlogin")
|
|
}
|