diff --git a/apis/tools/dbtables.go b/apis/tools/dbtables.go index 0b6f258..9491afc 100644 --- a/apis/tools/dbtables.go +++ b/apis/tools/dbtables.go @@ -5,7 +5,6 @@ import ( "go-admin/models/tools" tools2 "go-admin/tools" "go-admin/tools/app" - config2 "go-admin/tools/config" "net/http" ) @@ -23,11 +22,11 @@ func GetDBTableList(c *gin.Context) { var err error var pageSize = 10 var pageIndex = 1 - if config2.DatabaseConfig.Dbtype=="sqlite3"{ - res.Msg="对不起,sqlite3 暂不支持代码生成!" - c.JSON(http.StatusOK, res.ReturnError(500)) - return - } + //if config2.DatabaseConfig.DbType=="sqlite3"{ + // res.Msg="对不起,sqlite3 暂不支持代码生成!" + // c.JSON(http.StatusOK, res.ReturnError(500)) + // return + //} if size := c.Request.FormValue("pageSize"); size != "" { pageSize = tools2.StrToInt(err, size) diff --git a/cmd/migrate/server.go b/cmd/migrate/server.go index 8cae163..4d012eb 100644 --- a/cmd/migrate/server.go +++ b/cmd/migrate/server.go @@ -52,7 +52,7 @@ func run() { } func migrateModel() error { - if config2.DatabaseConfig.Dbtype == "mysql" { + if config2.DatabaseConfig.DbType == "mysql" { orm.Eloquent = orm.Eloquent.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4") } return gorm.AutoMigrate(orm.Eloquent) diff --git a/config/db.sql b/config/db.sql index b96d52b..d13c0a7 100644 --- a/config/db.sql +++ b/config/db.sql @@ -1,6 +1,4 @@ -- 开始初始化数据 ; -SET NAMES utf8mb4; -SET FOREIGN_KEY_CHECKS = 0; BEGIN; INSERT INTO `casbin_rule` VALUES ('p', 'Tester', '/api/v1/calendar', 'GET', NULL, NULL, NULL); @@ -661,5 +659,4 @@ INSERT INTO `sys_user` VALUES (3, '李四', '13838385438', 2, '', '', '0', 'qq@q INSERT INTO `sys_user` VALUES (4, '王五', '13535353535', 3, '', '', '2', 'qq@qq.com', 8, 2, '1', '1', '', 0, '2020-04-12 14:06:49', '2020-04-12 14:07:09', NULL, 'wangwu', '$2a$10$3.RT6rpXANXvvlibX6PzU.FGA2CvfDxd1UmJ2H5zTzF4sYocbvsTO'); COMMIT; -SET FOREIGN_KEY_CHECKS = 1; -- 数据完成 ; \ No newline at end of file diff --git a/config/rbac_model.conf b/config/rbac_model.conf deleted file mode 100644 index 32781ee..0000000 --- a/config/rbac_model.conf +++ /dev/null @@ -1,11 +0,0 @@ -[request_definition] -r = sub, obj, act - -[policy_definition] -p = sub, obj, act - -[policy_effect] -e = some(where (p.eft == allow)) - -[matchers] -m = r.sub == p.sub && (keyMatch2(r.obj, p.obj) || keyMatch(r.obj, p.obj)) && (r.act == p.act || p.act == "*") \ No newline at end of file diff --git a/config/settings.yml b/config/settings.yml index dc6ade5..1bb78a0 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -18,9 +18,10 @@ settings: secret: go-admin timeout: 3600 database: - name: goadmindb dbtype: mysql - host: 127.0.0.1 - password: 123456 - port: 3306 - username: root + sqlite: + masterconn: ./sqlite3.db + mysql: + masterconn: user:password@/tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local + pgsql: + masterconn: host=myhost port=myport user=gorm dbname=gorm password=mypassword diff --git a/database/initialize.go b/database/initialize.go index 443b521..9d5290f 100644 --- a/database/initialize.go +++ b/database/initialize.go @@ -4,14 +4,19 @@ import "go-admin/tools/config" func Setup() { - dbType := config.DatabaseConfig.Dbtype + dbType := config.DatabaseConfig.DbType if dbType == "mysql" { var db = new(Mysql) db.Setup() } - if dbType == "sqlite" { + if dbType == "sqlite3" { var db = new(SqLite) db.Setup() } + + if dbType == "pgsql" { + var db = new(PgSql) + db.Setup() + } } diff --git a/database/mysql.go b/database/mysql.go index 28856c8..832ec90 100644 --- a/database/mysql.go +++ b/database/mysql.go @@ -1,22 +1,11 @@ package database import ( - "bytes" _ "github.com/go-sql-driver/mysql" //加载mysql "github.com/jinzhu/gorm" log "github.com/sirupsen/logrus" "go-admin/global/orm" "go-admin/tools/config" - "strconv" -) - -var ( - DbType string - Host string - Port int - Name string - Username string - Password string ) func (e *Mysql) Setup() { @@ -27,12 +16,12 @@ func (e *Mysql) Setup() { db = new(Mysql) orm.MysqlConn = db.GetConnect() log.Info(orm.MysqlConn) - orm.Eloquent, err = db.Open(DbType, orm.MysqlConn) + orm.Eloquent, err = db.Open(config.DatabaseConfig.DbType, orm.MysqlConn) if err != nil { - log.Fatalf("%s connect error %v", DbType, err) + log.Fatalf("%s connect error %v", config.DatabaseConfig.DbType, err) } else { - log.Printf("%s connect success!", DbType) + log.Printf("%s connect success!", config.DatabaseConfig.DbType) } if orm.Eloquent.Error != nil { @@ -50,25 +39,5 @@ func (e *Mysql) Open(dbType string, conn string) (db *gorm.DB, err error) { } func (e *Mysql) GetConnect() string { - - DbType = config.DatabaseConfig.Dbtype - Host = config.DatabaseConfig.Host - Port = config.DatabaseConfig.Port - Name = config.DatabaseConfig.Name - Username = config.DatabaseConfig.Username - Password = config.DatabaseConfig.Password - - var conn bytes.Buffer - conn.WriteString(Username) - conn.WriteString(":") - conn.WriteString(Password) - conn.WriteString("@tcp(") - conn.WriteString(Host) - conn.WriteString(":") - conn.WriteString(strconv.Itoa(Port)) - conn.WriteString(")") - conn.WriteString("/") - conn.WriteString(Name) - conn.WriteString("?charset=utf8&parseTime=True&loc=Local&timeout=1000ms") - return conn.String() + return config.DatabaseConfig.Mysql.MasterConn } diff --git a/database/pgsql.go b/database/pgsql.go index 3c6d4e1..896dd3f 100644 --- a/database/pgsql.go +++ b/database/pgsql.go @@ -5,6 +5,7 @@ import ( _ "github.com/jinzhu/gorm/dialects/sqlite" log "github.com/sirupsen/logrus" "go-admin/global/orm" + "go-admin/tools/config" ) type PgSql struct { @@ -17,7 +18,7 @@ func (*PgSql) Open(dbType string, conn string) (db *gorm.DB, err error) { func (e *PgSql) GetConnect() string { - return "" + return config.DatabaseConfig.PgSql.MasterConn } func (e *PgSql) Setup() { @@ -28,12 +29,12 @@ func (e *PgSql) Setup() { db = new(PgSql) orm.PgSqlConn = db.GetConnect() log.Info(orm.PgSqlConn) - orm.Eloquent, err = db.Open(DbType, orm.PgSqlConn) + orm.Eloquent, err = db.Open(config.DatabaseConfig.DbType, orm.PgSqlConn) if err != nil { - log.Fatalf("%s connect error %v", DbType, err) + log.Fatalf("%s connect error %v", config.DatabaseConfig.DbType, err) } else { - log.Printf("%s connect success!", DbType) + log.Printf("%s connect success!", config.DatabaseConfig.DbType) } if orm.Eloquent.Error != nil { @@ -41,4 +42,4 @@ func (e *PgSql) Setup() { } orm.Eloquent.LogMode(true) -} \ No newline at end of file +} diff --git a/database/sqlite3.go b/database/sqlite3.go index bc3781e..afe99f7 100644 --- a/database/sqlite3.go +++ b/database/sqlite3.go @@ -5,6 +5,7 @@ import ( _ "github.com/jinzhu/gorm/dialects/sqlite" log "github.com/sirupsen/logrus" "go-admin/global/orm" + "go-admin/tools/config" ) type SqLite struct { @@ -16,8 +17,7 @@ func (*SqLite) Open(dbType string, conn string) (db *gorm.DB, err error) { } func (e *SqLite) GetConnect() string { - - return "" + return config.DatabaseConfig.SqLite.MasterConn } func (e *SqLite) Setup() { @@ -28,12 +28,12 @@ func (e *SqLite) Setup() { db = new(SqLite) orm.SqLiteConn = db.GetConnect() log.Info(orm.SqLiteConn) - orm.Eloquent, err = db.Open(DbType, orm.SqLiteConn) + orm.Eloquent, err = db.Open(config.DatabaseConfig.DbType, orm.SqLiteConn) if err != nil { - log.Fatalf("%s connect error %v", DbType, err) + log.Fatalf("%s connect error %v", config.DatabaseConfig.DbType, err) } else { - log.Printf("%s connect success!", DbType) + log.Printf("%s connect success!", config.DatabaseConfig.DbType) } if orm.Eloquent.Error != nil { diff --git a/global/orm/gorm.go b/global/orm/gorm.go index 2428a65..c81db37 100644 --- a/global/orm/gorm.go +++ b/global/orm/gorm.go @@ -8,3 +8,4 @@ var Eloquent *gorm.DB var MysqlConn string var SqLiteConn string var PgSqlConn string +var DbType string diff --git a/go.mod b/go.mod index 2dc7224..0772f62 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/casbin/gorm-adapter/v2 v2.0.3 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/gin-gonic/gin v1.4.0 - github.com/go-kit/kit v0.8.0 github.com/go-ole/go-ole v1.2.4 // indirect github.com/go-openapi/spec v0.19.7 // indirect github.com/go-openapi/swag v0.19.8 // indirect diff --git a/models/initdb.go b/models/initdb.go index 88d403c..07ad02a 100644 --- a/models/initdb.go +++ b/models/initdb.go @@ -3,17 +3,16 @@ package models import ( "fmt" "go-admin/global/orm" - config2 "go-admin/tools/config" "io/ioutil" "strings" ) func InitDb() error { filePath := "config/db.sql" - if config2.DatabaseConfig.Dbtype == "sqlite3" { - fmt.Println("sqlite3数据库无需初始化!") - return nil - } + //if config2.DatabaseConfig.DbType == "sqlite" { + // fmt.Println("sqlite3数据库无需初始化!") + // return nil + //} sql, err := Ioutil(filePath) if err != nil { fmt.Println("数据库基础数据初始化脚本读取失败!原因:", err.Error()) diff --git a/models/tools/dbcolumns.go b/models/tools/dbcolumns.go index de95e46..a088267 100644 --- a/models/tools/dbcolumns.go +++ b/models/tools/dbcolumns.go @@ -2,6 +2,7 @@ package tools import ( "errors" + "github.com/jinzhu/gorm" "go-admin/global/orm" config2 "go-admin/tools/config" ) @@ -23,37 +24,43 @@ type DBColumns struct { func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int, error) { var doc []DBColumns + var count int + table := new(gorm.DB) - table := orm.Eloquent.Select("*").Table("information_schema.`COLUMNS`") - table = table.Where("table_schema= ? ", config2.DatabaseConfig.Name) + if config2.DatabaseConfig.DbType == "mysql" { + table = orm.Eloquent.Select("*").Table("information_schema.`COLUMNS`") + table = table.Where("table_schema= ? ", config2.DatabaseConfig.Mysql.DBName) - if e.TableName != "" { - return nil, 0, errors.New("table name cannot be empty!") + if e.TableName != "" { + return nil, 0, errors.New("table name cannot be empty!") + } + + table = table.Where("TABLE_NAME = ?", e.TableName) } - table = table.Where("TABLE_NAME = ?", e.TableName) - - var count int if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil { return nil, 0, err } table.Count(&count) return doc, count, nil + } func (e *DBColumns) GetList() ([]DBColumns, error) { var doc []DBColumns + table := new(gorm.DB) - table := orm.Eloquent.Select("*").Table("information_schema.columns") - table = table.Where("table_schema= ? ", config2.DatabaseConfig.Name) + if config2.DatabaseConfig.DbType == "mysql" { + table = orm.Eloquent.Select("*").Table("information_schema.columns") + table = table.Where("table_schema= ? ", config2.DatabaseConfig.Mysql.DBName) - if e.TableName == "" { - return nil, errors.New("table name cannot be empty!") + if e.TableName == "" { + return nil, errors.New("table name cannot be empty!") + } + + table = table.Where("TABLE_NAME = ?", e.TableName) } - - table = table.Where("TABLE_NAME = ?", e.TableName) - if err := table.Find(&doc).Error; err != nil { return doc, err } diff --git a/models/tools/dbtables.go b/models/tools/dbtables.go index aaf7cee..94bf4bc 100644 --- a/models/tools/dbtables.go +++ b/models/tools/dbtables.go @@ -2,6 +2,7 @@ package tools import ( "errors" + "github.com/jinzhu/gorm" "go-admin/global/orm" config2 "go-admin/tools/config" ) @@ -18,34 +19,54 @@ type DBTables struct { func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int, error) { var doc []DBTables - - table := orm.Eloquent.Select("*").Table("information_schema.tables") - table = table.Where("TABLE_NAME not in (select table_name from "+config2.DatabaseConfig.Name+".sys_tables) ") - table = table.Where("table_schema= ? ", config2.DatabaseConfig.Name) - - if e.TableName != "" { - table = table.Where("TABLE_NAME = ?", e.TableName) - } - + table := new(gorm.DB) var count int - if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil { - return nil, 0, err + if config2.DatabaseConfig.DbType == "mysql" { + table = orm.Eloquent.Select("*").Table("information_schema.tables") + table = table.Where("TABLE_NAME not in (select table_name from " + config2.DatabaseConfig.Mysql.DBName + ".sys_tables) ") + table = table.Where("table_schema= ? ", config2.DatabaseConfig.Mysql.DBName) + + if e.TableName != "" { + table = table.Where("TABLE_NAME = ?", e.TableName) + } + if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil { + return nil, 0, err + } + } else if config2.DatabaseConfig.DbType == "sqlite3" { + table = orm.Eloquent.Select("name as TABLE_NAME,name as ENGINE,name as TABLE_ROWS,name as TABLE_COLLATION,name as CREATE_TIME,name as UPDATE_TIME,name as TABLE_COMMENT ").Table("sqlite_master") + table = table.Where("type = 'table' ") + + if e.TableName != "" { + table = table.Where("name = ?", e.TableName) + } + if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil { + return nil, 0, err + } } + table.Count(&count) return doc, count, nil } func (e *DBTables) Get() (DBTables, error) { var doc DBTables + table := new(gorm.DB) + if config2.DatabaseConfig.DbType == "mysql" { + table = orm.Eloquent.Select("*").Table("information_schema.tables") + table = table.Where("table_schema= ? ", config2.DatabaseConfig.Mysql.DBName) + if e.TableName == "" { + return doc, errors.New("table name cannot be empty!") + } + table = table.Where("TABLE_NAME = ?", e.TableName) + } else if config2.DatabaseConfig.DbType == "sqlite3" { + table = orm.Eloquent.Select("name as TABLE_NAME,name as ENGINE,name as TABLE_ROWS,name as TABLE_COLLATION,name as CREATE_TIME,name as UPDATE_TIME,name as TABLE_COMMENT ").Table("sqlite_master") + table = table.Where("type = 'table' ") - table := orm.Eloquent.Select("*").Table("information_schema.tables") - table = table.Where("table_schema= ? ", config2.DatabaseConfig.Name) - if e.TableName == "" { - return doc, errors.New("table name cannot be empty!") + if e.TableName != "" { + table = table.Where("name = ?", e.TableName) + } } - table = table.Where("TABLE_NAME = ?", e.TableName) - if err := table.First(&doc).Error; err != nil { return doc, err } diff --git a/pkg/casbin/mycasbin.go b/pkg/casbin/mycasbin.go index 9db87b5..21e5070 100644 --- a/pkg/casbin/mycasbin.go +++ b/pkg/casbin/mycasbin.go @@ -2,23 +2,38 @@ package mycasbin import ( "github.com/casbin/casbin/v2" - gormadapter "github.com/casbin/gorm-adapter/v2" - "github.com/go-kit/kit/endpoint" + "github.com/casbin/casbin/v2/model" + gormAdapter "github.com/casbin/gorm-adapter/v2" _ "github.com/go-sql-driver/mysql" log "github.com/sirupsen/logrus" "go-admin/global/orm" - "go-admin/tools/config" ) -var Em endpoint.Middleware +// Initialize the model from a string. +var text = ` +[request_definition] +r = sub, obj, act + +[policy_definition] +p = sub, obj, act + +[policy_effect] +e = some(where (p.eft == allow)) + +[matchers] +m = r.sub == p.sub && (keyMatch2(r.obj, p.obj) || keyMatch(r.obj, p.obj)) && (r.act == p.act || p.act == "*") +` func Casbin() (*casbin.Enforcer, error) { - conn := orm.MysqlConn - Apter, err := gormadapter.NewAdapter(config.DatabaseConfig.Dbtype, conn, true) + Apter, err := gormAdapter.NewAdapterByDB(orm.Eloquent) if err != nil { return nil, err } - e, err := casbin.NewEnforcer("config/rbac_model.conf", Apter) + m, err := model.NewModelFromString(text) + if err != nil { + return nil, err + } + e, err := casbin.NewEnforcer(m, Apter) if err != nil { return nil, err } diff --git a/service/sysuser.go b/service/sysuser.go deleted file mode 100644 index 1740c93..0000000 --- a/service/sysuser.go +++ /dev/null @@ -1,170 +0,0 @@ -package service - -import ( - "errors" - log "github.com/sirupsen/logrus" - "go-admin/global/orm" - . "go-admin/models" - "go-admin/tools" - "golang.org/x/crypto/bcrypt" - "strings" -) - - -// 获取用户数据 -func (e *SysUser) Get() (SysUserView SysUserView, err error) { - - table := orm.Eloquent.Table(e.TableName()).Select([]string{"sys_user.*", "sys_role.role_name"}) - table = table.Joins("left join sys_role on sys_user.role_id=sys_role.role_id") - if e.UserId != 0 { - table = table.Where("user_id = ?", e.UserId) - } - - if e.Username != "" { - table = table.Where("username = ?", e.Username) - } - - if e.Password != "" { - table = table.Where("password = ?", e.Password) - } - - if e.RoleId != 0 { - table = table.Where("role_id = ?", e.RoleId) - } - - if e.DeptId != 0 { - table = table.Where("dept_id = ?", e.DeptId) - } - - if e.PostId != 0 { - table = table.Where("post_id = ?", e.PostId) - } - - if err = table.First(&SysUserView).Error; err != nil { - return - } - SysUserView.Password = "" - return -} - -func (e *SysUser) GetPage(pageSize int, pageIndex int) ([]SysUserPage, int, error) { - var doc []SysUserPage - table := orm.Eloquent.Select("sys_user.*,sys_dept.dept_name").Table(e.TableName()) - table = table.Joins("left join sys_dept on sys_dept.dept_id = sys_user.dept_id") - - if e.Username != "" { - table = table.Where("username = ?", e.Username) - } - if e.Status != "" { - table = table.Where("sys_user.status = ?", e.Status) - } - - if e.Phone != "" { - table = table.Where("sys_user.phone = ?", e.Phone) - } - - if e.DeptId != 0 { - table = table.Where("sys_user.dept_id in (select dept_id from sys_dept where dept_path like ? )", "%"+tools.IntToString(e.DeptId)+"%") - } - - // 数据权限控制 - dataPermission := new(DataPermission) - dataPermission.UserId, _ = tools.StringToInt(e.DataScope) - table, err := dataPermission.GetDataScope("sys_user", table) - if err != nil { - return nil, 0, err - } - var count int - - if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil { - return nil, 0, err - } - table.Where("sys_user.deleted_at IS NULL").Count(&count) - return doc, count, nil -} - -//加密 -func (e *SysUser) Encrypt() (err error) { - if e.Password == "" { - return - } - - var hash []byte - if hash, err = bcrypt.GenerateFromPassword([]byte(e.Password), bcrypt.DefaultCost); err != nil { - return - } else { - e.Password = string(hash) - return - } -} - -//添加 -func (e SysUser) Insert() (id int, err error) { - if err = e.Encrypt(); err != nil { - return - } - - // check 用户名 - var count int - orm.Eloquent.Table(e.TableName()).Where("username = ?", e.Username).Count(&count) - if count > 0 { - err = errors.New("账户已存在!") - return - } - - //添加数据 - if err = orm.Eloquent.Table(e.TableName()).Create(&e).Error; err != nil { - return - } - id = e.UserId - return -} - -//修改 -func (e *SysUser) Update(id int) (update SysUser, err error) { - if e.Password!="" { - if err = e.Encrypt(); err != nil { - return - } - } - if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil { - return - } - if e.RoleId == 0 { - e.RoleId = update.RoleId - } - - //参数1:是要修改的数据 - //参数2:是修改的数据 - if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil { - return - } - return -} - -func (e *SysUser) BatchDelete(id []int) (Result bool, err error) { - if err = orm.Eloquent.Table(e.TableName()).Where("user_id in (?)", id).Delete(&SysUser{}).Error; err != nil { - return - } - Result = true - return -} - -func (e *SysUser) SetPwd(pwd SysUserPwd) (Result bool, err error) { - user, err := e.Get() - if err != nil { - tools.HasError(err, "获取用户数据失败(代码202)", 500) - } - _, err = tools.CompareHashAndPassword(user.Password, pwd.OldPassword) - if err != nil { - if strings.Contains(err.Error(), "hashedPassword is not the hash of the given password") { - tools.HasError(err, "密码错误(代码202)", 500) - } - log.Print(err) - return - } - e.Password = pwd.NewPassword - _, err = e.Update(e.UserId) - tools.HasError(err, "更新密码失败(代码202)", 500) - return -} diff --git a/sqlite.db b/sqlite.db index 52d3a43..994fe62 100644 Binary files a/sqlite.db and b/sqlite.db differ diff --git a/sqlite3.db b/sqlite3.db index e94506a..029ba3b 100644 Binary files a/sqlite3.db and b/sqlite3.db differ diff --git a/tools/config/database.go b/tools/config/database.go index 87f341d..491dfe9 100644 --- a/tools/config/database.go +++ b/tools/config/database.go @@ -3,23 +3,79 @@ package config import "github.com/spf13/viper" type Database struct { - Dbtype string - Host string - Port int - Name string - Username string - Password string + DbType string + SqLite *SqLite + Mysql *Mysql + PgSql *PgSql +} + +type SqLite struct { + MasterConn string + DBName string +} + +type Mysql struct { + MasterConn string + DBName string +} + +type PgSql struct { + MasterConn string + DBName string } func InitDatabase(cfg *viper.Viper) *Database { - return &Database{ - Port: cfg.GetInt("port"), - Dbtype: cfg.GetString("dbType"), - Host: cfg.GetString("host"), - Name: cfg.GetString("name"), - Username: cfg.GetString("username"), - Password: cfg.GetString("password"), + + dbType := cfg.GetString("dbType") + + db := &Database{ + DbType: cfg.GetString("dbType"), } + + if dbType == "sqlite3" { + sqlit := cfg.Sub("sqlite") + if sqlit == nil { + panic("config not found settings.database.sqlite") + } + db.SqLite = InitSqlite(sqlit) + } else if dbType == "mysql" { + mysql := cfg.Sub("mysql") + if mysql == nil { + panic("config not found settings.database.mysql") + } + db.Mysql = InitMysql(mysql) + } else if dbType == "pgsql" { + pgsql := cfg.Sub("pgsql") + if pgsql == nil { + panic("config not found settings.database.pgsql") + } + db.PgSql = InitPgsql(pgsql) + } else { + panic("unknown dbtype") + } + + return db } var DatabaseConfig = new(Database) + +func InitSqlite(cfg *viper.Viper) *SqLite { + return &SqLite{ + MasterConn: cfg.GetString("masterconn"), + DBName: cfg.GetString("dbname"), + } +} + +func InitMysql(cfg *viper.Viper) *Mysql { + return &Mysql{ + MasterConn: cfg.GetString("masterconn"), + DBName: cfg.GetString("dbname"), + } +} + +func InitPgsql(cfg *viper.Viper) *PgSql { + return &PgSql{ + MasterConn: cfg.GetString("masterconn"), + DBName: cfg.GetString("dbname"), + } +} diff --git a/tools/env.go b/tools/env.go index 9cb337b..def4f64 100644 --- a/tools/env.go +++ b/tools/env.go @@ -9,5 +9,5 @@ const ( ModeTest Mode = "test" //测试模式 ModeProd Mode = "prod" //生产模式 Mysql = "mysql" //mysql数据库标识 - Sqlite = "sqlite" //sqlite + Sqlite = "sqlite3" //sqlite )