|
|
@ -5,16 +5,30 @@
|
|
|
|
package conf
|
|
|
|
package conf
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"database/sql"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/alimy/cfg"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
|
|
|
|
_sqldb *sql.DB
|
|
|
|
_redisClient *redis.Client
|
|
|
|
_redisClient *redis.Client
|
|
|
|
_onceRedis sync.Once
|
|
|
|
_onceSql, _onceRedis sync.Once
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func MustSqlDB() *sql.DB {
|
|
|
|
|
|
|
|
_onceSql.Do(func() {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if _, _sqldb, err = newSqlDB(); err != nil {
|
|
|
|
|
|
|
|
logrus.Fatalf("new sql db failed: %s", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return _sqldb
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func MustRedis() *redis.Client {
|
|
|
|
func MustRedis() *redis.Client {
|
|
|
|
_onceRedis.Do(func() {
|
|
|
|
_onceRedis.Do(func() {
|
|
|
|
_redisClient = redis.NewClient(&redis.Options{
|
|
|
|
_redisClient = redis.NewClient(&redis.Options{
|
|
|
@ -25,3 +39,19 @@ func MustRedis() *redis.Client {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return _redisClient
|
|
|
|
return _redisClient
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func newSqlDB() (driver string, db *sql.DB, err error) {
|
|
|
|
|
|
|
|
if cfg.If("MySQL") {
|
|
|
|
|
|
|
|
driver = "mysql"
|
|
|
|
|
|
|
|
db, err = sql.Open(driver, MysqlSetting.Dsn())
|
|
|
|
|
|
|
|
} else if cfg.If("PostgreSQL") || cfg.If("Postgres") {
|
|
|
|
|
|
|
|
driver = "pgx"
|
|
|
|
|
|
|
|
db, err = sql.Open(driver, PostgresSetting.Dsn())
|
|
|
|
|
|
|
|
} else if cfg.If("Sqlite3") {
|
|
|
|
|
|
|
|
driver, db, err = OpenSqlite3()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
driver = "mysql"
|
|
|
|
|
|
|
|
db, err = sql.Open(driver, MysqlSetting.Dsn())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|