package main

import (
	"flag"
	"sparrow/pkg/mysql"

	"github.com/jinzhu/gorm"
)

const (
	flagRabbitHost    = "rabbithost"
	defaultRabbitHost = "amqp://knowocloud:123456@192.168.175.60:5672/"

	flagDBHost = "dbhost"
	flagDBPort = "dbport"
	flagDBName = "dbname"
	flagDBUser = "dbuser"
	flagDBPass = "dbpass"

	flagAESKey = "aeskey"

	defaultDBHost = "192.168.175.60"
	defaultDBPort = "3306"
	defaultDBName = "SparrowCloud"
	defaultDBUser = "SparrowCloud"
)

var (
	confDBHost     = flag.String(flagDBHost, defaultDBHost, "database host address.")
	confDBPort     = flag.String(flagDBPort, defaultDBPort, "database host port.")
	confDBName     = flag.String(flagDBName, defaultDBName, "database name.")
	confDBUser     = flag.String(flagDBUser, defaultDBUser, "database user.")
	confDBPass     = flag.String(flagDBPass, "", "databse password.")
	confRabbitHost = flag.String(flagRabbitHost, defaultRabbitHost, "rabbitmq host address, amqp://user:password@ip:port/")
	confAESKey     = flag.String(flagAESKey, "", "use your own aes encryting key.")
)

func getDB() (*gorm.DB, error) {
	db, err := mysql.GetClient(*confDBHost, *confDBPort, *confDBName, *confDBUser, *confDBPass)
	if err != nil {
		return nil, err
	}
	gormdb, err := gorm.Open("mysql", db)
	if err != nil {
		return nil, err
	}
	gormdb.SingularTable(true)
	gormdb.LogMode(true)
	return gormdb, nil
}
