package parts
import (
"Qmoser/linstream_identity_servter/src/config"
"fmt"
"github.com/go-redis/redis"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
mgo "gopkg.in/mgo.v2"
)
type loading interface {
Loading_mongdb()
Close_mongdb()
Loading_mysql()
Close_mysql()
Loading_redis()
Close_redis()
}
type MMR struct {
Mongdb_conn *mgo.Session
Mysql_conn *xorm.Engine
Redis_conn *redis.Client
loading
}
func (self *MMR) Loading_mongdb() {
mongdb_conn_str := config.Global_Config.Get("mongdb_conn")
session, err := mgo.Dial(mongdb_conn_str)
if err != nil {
panic(err)
} else {
self.Mongdb_conn = session
}
// defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)
}
func (self *MMR) Close_mongdb() {
if self.Mongdb_conn != nil {
self.Mongdb_conn.Close()
}
}
func (self *MMR) Loading_mysql() {
var err error
mysql_conn := config.Global_Config.Get("mysql_conn")
self.Mysql_conn, err = xorm.NewEngine("mysql", mysql_conn)
if err != nil {
panic("mysql " + err.Error())
}
fmt.Println("***************************************")
fmt.Println(self.Mysql_conn)
fmt.Println("***************************************")
}
func (self *MMR) Close_mysql() {
if self.Mysql_conn != nil {
self.Mysql_conn.Close()
}
}
func (self *MMR) Loading_redis() {
redis_ip, redis_port, redis_pwd, redis_db :=
config.Global_Config.Get("redis_ip"),
config.Global_Config.Get("redis_port"),
config.Global_Config.Get("redis_pwd"),
config.Global_Config.GetInt("redis_pwd")
// use default DB
redis_ip_port := redis_ip + `:` + redis_port
self.Redis_conn = redis.NewClient(&redis.Options{Addr: redis_ip_port, Password: redis_pwd, DB: redis_db})
if _, err := self.Redis_conn.Ping().Result(); err != nil {
panic("Redis " + err.Error())
}
}
func (self *MMR) Close_redis() {
if self.Redis_conn != nil {
self.Redis_conn.Close()
}
}
var GParts *MMR
func init() {
GParts = new(MMR)
// GParts.Loading_mongdb()
GParts.Loading_mysql()
fmt.Println("***************************************")
fmt.Println(GParts.Mysql_conn)
fmt.Println("***************************************")
// GParts.Loading_redis()
}
external code calls GParts directly