123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package db
- import (
- "gorm.io/driver/mysql"
- "gorm.io/gorm"
- "gorm.io/gorm/logger"
- "gorm.io/gorm/schema"
- "time"
- log "gogs.baozhida.cn/zoie/OAuth-core/logger"
- "gogs.baozhida.cn/zoie/OAuth-core/pkg"
- mycasbin "gogs.baozhida.cn/zoie/OAuth-core/pkg/casbin"
- "gogs.baozhida.cn/zoie/OAuth-core/sdk"
- toolsConfig "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
- toolsDB "gogs.baozhida.cn/zoie/OAuth-core/tools/database"
- . "gogs.baozhida.cn/zoie/OAuth-core/tools/gorm/logger"
- )
- var DB *gorm.DB
- // Setup 配置数据库
- func Setup() {
- c := toolsConfig.DatabaseConfig
- registers := make([]toolsDB.ResolverConfigure, len(c.Registers))
- for i := range c.Registers {
- registers[i] = toolsDB.NewResolverConfigure(
- c.Registers[i].Sources,
- c.Registers[i].Replicas,
- c.Registers[i].Policy,
- c.Registers[i].Tables)
- }
- //resolverConfig := toolsDB.NewConfigure(c.Source, c.MaxIdleConns, c.MaxOpenConns, c.ConnMaxIdleTime, c.ConnMaxLifeTime, registers)
- //db, err := resolverConfig.Init(&gorm.Config{
- // NamingStrategy: schema.NamingStrategy{
- // SingularTable: true,
- // },
- // Logger: New(
- // logger.Config{
- // SlowThreshold: time.Second,
- // Colorful: true,
- // LogLevel: logger.LogLevel(
- // log.DefaultLogger.Options().Level.LevelForGorm()),
- // },
- // ),
- // DisableAutomaticPing: true,
- //}, mysql.Open)
- db, err := gorm.Open(mysql.Open(c.Source), &gorm.Config{
- NamingStrategy: schema.NamingStrategy{
- SingularTable: true,
- },
- Logger: New(
- logger.Config{
- SlowThreshold: time.Second,
- Colorful: true,
- LogLevel: logger.LogLevel(
- log.DefaultLogger.Options().Level.LevelForGorm()),
- },
- ),
- DisableAutomaticPing: true})
- if err != nil {
- log.Fatal(pkg.Red("mysql connect error :"), err)
- } else {
- log.Info(pkg.Green("mysql connect success !"))
- }
- DB = db
- e := mycasbin.Setup(db)
- sdk.Runtime.SetDb(toolsConfig.ApplicationConfig.Host, db)
- sdk.Runtime.SetCasbin(toolsConfig.ApplicationConfig.Host, e)
- }
|