llmongodb/dbs.go

138 lines
2.5 KiB
Go
Raw Normal View History

2015-07-30 15:55:32 +02:00
// dbs
package main
import (
"github.com/garyburd/redigo/redis"
// "github.com/fzzy/radix/redis"
"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-11-10 09:01:10 +01:00
// rt "gopkg.in/dancannon/gorethink.v2"
"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 {
2016-11-10 09:01:10 +01:00
MongoURI string
Database string
RedisURI string
RethinkURI string
rdb *redis.Pool //*redis.Client
rtdb *Rethink
mdb *mgo.Session
ll *mgo.Collection
2015-10-16 11:43:18 +02:00
// us *mgo.Collection
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) isRethink() bool {
if db.RethinkURI != "" {
return true
}
return false
}
func (db *Dbs) isMongodb() bool {
if db.MongoURI != "" {
return true
}
return false
}
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) 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() {
var err error
2016-10-05 11:55:50 +02:00
db.mdb, err = mgo.Dial(db.MongoURI)
2015-07-30 15:55:32 +02:00
if err != nil {
log.Println("Mongodb connect Error: ", err.Error())
os.Exit(-3)
}
db.ll = db.mdb.DB(db.Database).C(fmt.Sprintf("lastlogin_%s", opts.Month))
2015-07-30 15:55:32 +02:00
}