diff --git a/QMPlusServer/config/config.go b/QMPlusServer/config/config.go index 56686330..b8e1573b 100644 --- a/QMPlusServer/config/config.go +++ b/QMPlusServer/config/config.go @@ -7,9 +7,15 @@ import ( ) type Config struct { - Admin Admin + MysqlAdmin MysqlAdmin + Qiniu Qiniu + CasbinConfig CasbinConfig } -type Admin struct { +type CasbinConfig struct { + ModelPath string // casbin model地址配置 +} + +type MysqlAdmin struct { // mysql admin 数据库配置 Username string Password string Path string @@ -17,12 +23,17 @@ type Admin struct { Config string } -var Dbconfig Config +type Qiniu struct { // 七牛 密钥配置 + AccessKey string + SecretKey string +} + +var GinVueAdminconfig Config func init() { v := viper.New() - v.SetConfigName("config") // 设置配置文件名 (不带后缀) - v.AddConfigPath("./static/dbconfig/") // 第一个搜索路径 + v.SetConfigName("config") // 设置配置文件名 (不带后缀) + v.AddConfigPath("./static/config/") // 第一个搜索路径 v.SetConfigType("json") err := v.ReadInConfig() // 搜索路径,并读取配置数据 if err != nil { @@ -32,7 +43,7 @@ func init() { v.OnConfigChange(func(e fsnotify.Event) { fmt.Println("Config file changed:", e.Name) }) - if err := v.Unmarshal(&Dbconfig); err != nil { + if err := v.Unmarshal(&GinVueAdminconfig); err != nil { fmt.Println(err) } } diff --git a/QMPlusServer/controller/servers/upload.go b/QMPlusServer/controller/servers/upload.go index 202bd12d..6348acc0 100644 --- a/QMPlusServer/controller/servers/upload.go +++ b/QMPlusServer/controller/servers/upload.go @@ -3,14 +3,15 @@ package servers import ( "context" "fmt" + "gin-vue-admin/config" "github.com/qiniu/api.v7/auth/qbox" "github.com/qiniu/api.v7/storage" "mime/multipart" "time" ) -var accessKey string = "25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ" // 你在七牛云的accessKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西 -var secretKey string = "pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY" // 你在七牛云的secretKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西 +var accessKey string = config.GinVueAdminconfig.Qiniu.AccessKey // 你在七牛云的accessKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西 +var secretKey string = config.GinVueAdminconfig.Qiniu.SecretKey // 你在七牛云的secretKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西 // 接收两个参数 一个文件流 一个 bucket 你的七牛云标准空间的名字 func Upload(file *multipart.FileHeader, bucket string, urlPath string) (err error, path string, key string) { diff --git a/QMPlusServer/init/qmsql/initMysql.go b/QMPlusServer/init/qmsql/initMysql.go index 06b83712..7a2344fb 100644 --- a/QMPlusServer/init/qmsql/initMysql.go +++ b/QMPlusServer/init/qmsql/initMysql.go @@ -10,7 +10,7 @@ import ( var DEFAULTDB *gorm.DB //初始化数据库并产生数据库全局变量 -func InitMysql(admin config.Admin) *gorm.DB { +func InitMysql(admin config.MysqlAdmin) *gorm.DB { if db, err := gorm.Open("mysql", admin.Username+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname+"?"+admin.Config); err != nil { log.Printf("DEFAULTDB数据库启动异常%S", err) } else { diff --git a/QMPlusServer/main.go b/QMPlusServer/main.go index cb7e7ed1..4e294a5e 100644 --- a/QMPlusServer/main.go +++ b/QMPlusServer/main.go @@ -20,12 +20,12 @@ import ( // @BasePath / func main() { - qmlog.InitLog() // 初始化日志 - db := qmsql.InitMysql(config.Dbconfig.Admin) // 链接初始化数据库 - registTable.RegistTable(db) //注册数据库表 - defer qmsql.DEFAULTDB.Close() // 程序结束前关闭数据库链接 - Router := initRouter.InitRouter() //注册路由 - qmlog.QMLog.Info("服务器开启") // 日志测试代码 + qmlog.InitLog() // 初始化日志 + db := qmsql.InitMysql(config.GinVueAdminconfig.MysqlAdmin) // 链接初始化数据库 + registTable.RegistTable(db) //注册数据库表 + defer qmsql.DEFAULTDB.Close() // 程序结束前关闭数据库链接 + Router := initRouter.InitRouter() //注册路由 + qmlog.QMLog.Info("服务器开启") // 日志测试代码 //Router.RunTLS(":443","ssl.pem", "ssl.key") // https支持 需要添加中间件 s := &http.Server{ Addr: ":8888", diff --git a/QMPlusServer/model/sysModel/sys_casbin.go b/QMPlusServer/model/sysModel/sys_casbin.go index a9895ec2..83460acf 100644 --- a/QMPlusServer/model/sysModel/sys_casbin.go +++ b/QMPlusServer/model/sysModel/sys_casbin.go @@ -2,6 +2,7 @@ package sysModel import ( "errors" + "gin-vue-admin/config" "gin-vue-admin/init/qmsql" "github.com/casbin/casbin" gormadapter "github.com/casbin/gorm-adapter" @@ -83,7 +84,7 @@ func ParamsMatchFunc(args ...interface{}) (interface{}, error) { //持久化到数据库 引入自定义规则 func Casbin() *casbin.Enforcer { a := gormadapter.NewAdapterByDB(qmsql.DEFAULTDB) - e := casbin.NewEnforcer("./static/rbacmodel/rbac_model.conf", a) + e := casbin.NewEnforcer(config.GinVueAdminconfig.CasbinConfig.ModelPath, a) e.AddFunction("ParamsMatch", ParamsMatchFunc) e.LoadPolicy() return e diff --git a/QMPlusServer/static/config/config.json b/QMPlusServer/static/config/config.json new file mode 100644 index 00000000..26043214 --- /dev/null +++ b/QMPlusServer/static/config/config.json @@ -0,0 +1,16 @@ +{ + "mysqlAdmin": { + "username": "root", + "password": "Aa@6447985", + "path": "127.0.0.1:3306", + "dbname": "qmplus", + "config": "charset=utf8&parseTime=True&loc=Local" + }, + "qiniu": { + "accessKey":"25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ", + "secretKey": "pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY" + }, + "casbinConfig":{ + "modelPath":"./static/rbacmodel/rbac_model.conf" + } +} \ No newline at end of file diff --git a/QMPlusServer/static/dbconfig/config.json b/QMPlusServer/static/dbconfig/config.json deleted file mode 100644 index b1ae2d09..00000000 --- a/QMPlusServer/static/dbconfig/config.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "admin": { - "username": "root", - "password": "Aa@6447985", - "path": "127.0.0.1:3306", - "dbname": "qmplus", - "config": "charset=utf8&parseTime=True&loc=Local" - } -} \ No newline at end of file