mirror of
https://github.com/gowvp/gb28181.git
synced 2025-12-24 12:12:31 +08:00
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package data
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"github.com/google/wire"
|
|
"github.com/gowvp/gb28181/internal/conf"
|
|
"github.com/ixugo/goddd/pkg/orm"
|
|
"github.com/ixugo/goddd/pkg/system"
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// ProviderSet is data providers.
|
|
var ProviderSet = wire.NewSet(SetupDB)
|
|
|
|
// SetupDB 初始化数据存储
|
|
func SetupDB(c *conf.Bootstrap) (*gorm.DB, error) {
|
|
cfg := c.Data.Database
|
|
dial, isSQLite := getDialector(cfg.Dsn)
|
|
if isSQLite {
|
|
cfg.MaxIdleConns = 1
|
|
cfg.MaxOpenConns = 1
|
|
}
|
|
db, err := orm.New(dial, orm.Config{
|
|
MaxIdleConns: int(cfg.MaxIdleConns),
|
|
MaxOpenConns: int(cfg.MaxOpenConns),
|
|
ConnMaxLifetime: cfg.ConnMaxLifetime.Duration(),
|
|
SlowThreshold: cfg.SlowThreshold.Duration(),
|
|
})
|
|
return db, err
|
|
}
|
|
|
|
// getDialector 返回 dial 和 是否 sqlite
|
|
func getDialector(dsn string) (gorm.Dialector, bool) {
|
|
switch true {
|
|
case strings.HasPrefix(dsn, "postgres"):
|
|
return postgres.New(postgres.Config{
|
|
DriverName: "pgx",
|
|
DSN: dsn,
|
|
}), false
|
|
case strings.HasPrefix(dsn, "mysql"):
|
|
return mysql.Open(dsn), false
|
|
default:
|
|
return sqlite.Open(filepath.Join(system.Getwd(), dsn)), true
|
|
}
|
|
}
|