llmongodb/dbs.go

105 lines
1.9 KiB
Go
Raw Normal View History

2015-07-30 15:55:32 +02:00
// dbs
package main
import (
// "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"
"github.com/garyburd/redigo/redis"
"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",
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 {
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
type Ips struct {
2016-10-05 11:55:50 +02:00
IP string `json:"ip"`
}
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
}
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,
Dial: func() (redis.Conn, error) {
2016-10-05 11:55:50 +02:00
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
},
}
}
2015-07-30 15:55:32 +02:00
func (db *Dbs) connectMongo() {
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
}