Browse Source

fix:升级beego V2读取配置文件core修改为web

master
燕鹏 4 years ago
parent
commit
30d90b6b1c
  1. 4
      conf/app.conf
  2. 9
      controllers/FileController.go
  3. 6
      models/DataInit.go

4
conf/app.conf

@ -1,6 +1,6 @@
appname = beeblog appname = beeblog
runmode = prod runmode = "dev"
sessionon = true sessionon = true
sessiongcmaxlifetime = 36000 sessiongcmaxlifetime = 36000
@ -13,7 +13,7 @@ host = aiprose.com
httpport = 8082 httpport = 8082
uploadDir = "static/upload/" uploadDir = "static/upload/"
imgUrlPrefix = "http://localhost:8082/static/upload/" imgUrlPrefix = "http://localhost:8082/static/upload/"
dburl = "trgis@tcp(47.98.109.5:3309)" dburl = "yasaka@tcp(localhost:3306)"
[prod] [prod]
httpport = 8082 httpport = 8082

9
controllers/FileController.go

@ -5,7 +5,6 @@ import (
"beeblog/service" "beeblog/service"
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"github.com/beego/beego/v2/core/config"
beego "github.com/beego/beego/v2/server/web" beego "github.com/beego/beego/v2/server/web"
"math/rand" "math/rand"
"os" "os"
@ -46,7 +45,7 @@ func (this *FileController) Upload() {
} }
//创建目录 //创建目录
urlDir := time.Now().Format("2006/01/02/") urlDir := time.Now().Format("2006/01/02/")
uploadDir, _ := config.String("uploadDir") uploadDir, _ := beego.AppConfig.String("uploadDir")
uploadDir = uploadDir + urlDir uploadDir = uploadDir + urlDir
err := os.MkdirAll(uploadDir, 777) err := os.MkdirAll(uploadDir, 777)
if err != nil { if err != nil {
@ -69,7 +68,7 @@ func (this *FileController) Upload() {
this.ServeJSON() this.ServeJSON()
return return
} }
imgUrlPrefix, _ := config.String("imgUrlPrefix") imgUrlPrefix, _ := beego.AppConfig.String("imgUrlPrefix")
urlDir = imgUrlPrefix + urlDir urlDir = imgUrlPrefix + urlDir
this.Data["json"] = models.ReurnData("", urlDir) this.Data["json"] = models.ReurnData("", urlDir)
this.ServeJSON() this.ServeJSON()
@ -109,7 +108,7 @@ func (this *FileController) HeadImgUpload() {
} }
//创建目录 //创建目录
urlDir := time.Now().Format("2006/01/02/") urlDir := time.Now().Format("2006/01/02/")
uploadDir, _ := config.String("uploadDir") uploadDir, _ := beego.AppConfig.String("uploadDir")
uploadDir = uploadDir + urlDir uploadDir = uploadDir + urlDir
err := os.MkdirAll(uploadDir, 777) err := os.MkdirAll(uploadDir, 777)
if err != nil { if err != nil {
@ -132,7 +131,7 @@ func (this *FileController) HeadImgUpload() {
this.ServeJSON() this.ServeJSON()
return return
} }
imgUrlPrefix, _ := config.String("imgUrlPrefix") imgUrlPrefix, _ := beego.AppConfig.String("imgUrlPrefix")
urlDir = imgUrlPrefix + urlDir urlDir = imgUrlPrefix + urlDir
user := &models.User{Id: uid.(int64), Headimg: urlDir} user := &models.User{Id: uid.(int64), Headimg: urlDir}
userSerivce := service.UserService{} userSerivce := service.UserService{}

6
models/DataInit.go

@ -3,16 +3,16 @@ package models
import ( import (
"fmt" "fmt"
"github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/config" "github.com/beego/beego/v2/server/web"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
func RegistDB() { func RegistDB() {
dbUrl, _ := config.String("dburl") dbUrl, _ := web.AppConfig.String("dburl")
fmt.Println(dbUrl) fmt.Println(dbUrl)
orm.RegisterDriver("mysql", orm.DRMySQL) orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "root:"+dbUrl+"/beeblog?charset=utf8&loc=Local") orm.RegisterDataBase("default", "mysql", "root:"+dbUrl+"/beeblog?charset=utf8&loc=Local")
orm.RegisterModel(new(User), new(Blog), new(NLabel), new(Note), new(NoteColl), new(Category), new(Like), new(Comment)) orm.RegisterModel(new(User), new(Blog), new(NLabel), new(Note), new(NoteColl), new(Category), new(Like), new(Comment))
//orm.RunSyncdb("default", false, true) orm.RunSyncdb("default", false, true)
} }

Loading…
Cancel
Save