databse.go 960 字节
Newer Older
E
eoLinker API Management 已提交
1 2 3 4
package console

import (
	"fmt"
黄孟柱 已提交
5 6
	"github.com/eolinker/goku-api-gateway/common/database"
	"github.com/eolinker/goku-api-gateway/server/entity"
E
eoLinker API Management 已提交
7 8
)

Y
Your Name 已提交
9
//ClusterDatabaseConfig 集群数据库对象
E
eoLinker API Management 已提交
10 11
type ClusterDatabaseConfig entity.ClusterDB

Y
Your Name 已提交
12
//GetDriver 获取驱动
E
eoLinker API Management 已提交
13 14 15 16
func (c *ClusterDatabaseConfig) GetDriver() string {
	return c.Driver
}

Y
Your Name 已提交
17
//GetSource 获取链接字符串
E
eoLinker API Management 已提交
18 19 20 21 22 23 24 25
func (c *ClusterDatabaseConfig) GetSource() string {

	//dsn = conf.Value("db_user") + ":" + conf.Value("db_password")
	//dsn = dsn + "@tcp(" + conf.Value("db_host") + ":" + conf.Value("db_port") + ")/" + conf.Value("db_name")
	//dsn = dsn + "?charset=utf8"
	return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", c.UserName, c.Password, c.Host, c.Port, c.Database)
}

Y
Your Name 已提交
26
//InitDatabase 初始化数据库
E
eoLinker API Management 已提交
27 28 29 30 31 32 33 34 35 36 37
func InitDatabase() {
	def, err := getDefaultDatabase()
	if err != nil {
		panic(err)
	}
	c := ClusterDatabaseConfig(*def)
	e := database.InitConnection(&c)
	if e != nil {
		panic(e)
	}
}