12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- 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{
- DisableForeignKeyConstraintWhenMigrating: true,
- 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)
- }
|