llmongodb/dbs.go

94 lines
1.7 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"
"time"
"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
MongoURI: "mongodb://127.0.0.1:27018",
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-10-05 11:55:50 +02:00
MongoURI string
Database string
2016-10-05 11:55:50 +02:00
RedisURI string
rdb *redis.Pool //*redis.Client
2015-07-30 15:55:32 +02:00
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 {
ID string `json:"_id" bson:"_id"`
2015-07-30 15:55:32 +02:00
User string `json:"user"`
Protocol string `json:"protocol"`
2016-10-05 11:55:50 +02:00
IP string `json:"ip"`
2015-07-30 15:55:32 +02:00
Date time.Time `json:"date"`
2016-05-13 13:28:04 +02:00
Insert time.Time `json:"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"`
}
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
}