package cfg import ( "fmt" "github.com/veypi/utils/cmd" "gorm.io/driver/mysql" "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/schema" ) var Path = cmd.GetCfgPath("oa", "settings") var CFG = &struct { AdminUser string Host string LoggerPath string LoggerLevel string APPUUID string APPKey string TimeFormat string Debug bool MediaDir string DB struct { Type string Addr string User string Pass string DB string } }{ APPUUID: "jU5Jo5hM", APPKey: "cB43wF94MLTksyBK", AdminUser: "admin", Host: "0.0.0.0:4001", LoggerPath: "", LoggerLevel: "debug", TimeFormat: "2006/01/02 15:04:05", Debug: true, MediaDir: "/Users/light/test/media/", DB: struct { Type string Addr string User string Pass string DB string }{ //Type: "sqlite", Addr: "127.0.0.1:3306", //Addr: "oa.db", User: "root", Pass: "123456", DB: "one_auth", }, } var ( db *gorm.DB ) func DB() *gorm.DB { if db == nil { ConnectDB() } return db } var gormCfg = &gorm.Config{ NamingStrategy: schema.NamingStrategy{ SingularTable: false, // 使用单数表名,启用该选项后,`User` 表将是`user` NoLowerCase: true, }, } func ConnectDB() *gorm.DB { var err error conn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local", CFG.DB.User, CFG.DB.Pass, CFG.DB.Addr, CFG.DB.DB) if CFG.DB.Type == "sqlite" { conn = CFG.DB.Addr db, err = gorm.Open(sqlite.Open(conn), gormCfg) } else { db, err = gorm.Open(mysql.Open(conn), gormCfg) } if err != nil { panic(err) } return db }