2015-07-30 15:55:32 +02:00
|
|
|
// dbs
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-10 16:56:09 +01:00
|
|
|
// "fmt"
|
2015-07-30 15:55:32 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
2016-11-10 09:01:10 +01:00
|
|
|
"strings"
|
2015-07-30 15:55:32 +02:00
|
|
|
"time"
|
2016-05-09 10:55:42 +02:00
|
|
|
|
2017-02-10 16:56:09 +01:00
|
|
|
"github.com/garyburd/redigo/redis"
|
|
|
|
|
2016-05-09 10:55:42 +02:00
|
|
|
"gopkg.in/mgo.v2"
|
2015-07-30 15:55:32 +02:00
|
|
|
)
|
|
|
|
|
2015-07-30 16:53:24 +02:00
|
|
|
var (
|
|
|
|
dbs = Dbs{
|
2016-10-05 11:55:50 +02:00
|
|
|
RedisURI: "127.0.0.1:6379",
|
2016-05-16 16:03:23 +02:00
|
|
|
Database: "lastlogin",
|
2015-07-30 16:53:24 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-10-05 11:55:50 +02:00
|
|
|
// Dbs structure
|
2015-07-30 15:55:32 +02:00
|
|
|
type Dbs struct {
|
2017-02-10 16:56:09 +01:00
|
|
|
MongoURI string
|
|
|
|
Database string
|
|
|
|
RedisURI string
|
|
|
|
rdb *redis.Pool //*redis.Client
|
|
|
|
mdb []*mgo.Session
|
2015-07-30 15:55:32 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 11:55:50 +02:00
|
|
|
// MongoLogin structure
|
2015-07-30 15:55:32 +02:00
|
|
|
type MongoLogin struct {
|
2016-11-10 09:01:10 +01:00
|
|
|
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"`
|
2015-10-16 11:43:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 11:55:50 +02:00
|
|
|
// Ips structure
|
2015-11-24 17:56:34 +01:00
|
|
|
type Ips struct {
|
2016-10-05 11:55:50 +02:00
|
|
|
IP string `json:"ip"`
|
2015-11-24 17:56:34 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 11:55:50 +02:00
|
|
|
// UserLogin structure
|
2015-10-16 11:43:18 +02:00
|
|
|
type UserLogin struct {
|
|
|
|
User string `json:"user"`
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
Lock bool `json:"lock"`
|
2015-07-30 15:55:32 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 11:55:50 +02:00
|
|
|
// Index structure
|
2015-07-30 15:55:32 +02:00
|
|
|
type Index struct {
|
|
|
|
User string `json:"user"`
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:01:10 +01:00
|
|
|
func (db *Dbs) isMongodb() bool {
|
|
|
|
if db.MongoURI != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-19 10:06:50 +01:00
|
|
|
func (db *Dbs) poolRedis() {
|
|
|
|
|
|
|
|
dbs.rdb = &redis.Pool{
|
2015-11-19 17:12:53 +01:00
|
|
|
MaxIdle: 128,
|
|
|
|
MaxActive: 1000,
|
|
|
|
Wait: true,
|
|
|
|
IdleTimeout: 1 * time.Second,
|
2015-11-19 10:06:50 +01:00
|
|
|
Dial: func() (redis.Conn, error) {
|
2016-10-05 11:55:50 +02:00
|
|
|
c, err := redis.Dial("tcp", db.RedisURI)
|
2015-11-19 10:06:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, err
|
|
|
|
},
|
|
|
|
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
|
|
|
_, err := c.Do("PING")
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:55:32 +02:00
|
|
|
func (db *Dbs) connectMongo() {
|
2016-05-16 16:03:23 +02:00
|
|
|
|
2017-02-10 16:56:09 +01:00
|
|
|
mongoList := strings.Split(db.MongoURI, ",")
|
|
|
|
|
|
|
|
for m := range mongoList {
|
|
|
|
nm, err := mgo.Dial(mongoList[m])
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Mongodb connect Error: ", err.Error())
|
|
|
|
os.Exit(-3)
|
|
|
|
}
|
|
|
|
nm.SetSocketTimeout(5 * time.Second)
|
|
|
|
nm.SetSyncTimeout(5 * time.Second)
|
|
|
|
db.mdb = append(db.mdb, nm)
|
|
|
|
}
|
2015-07-30 15:55:32 +02:00
|
|
|
}
|