Browse Source

升级beego V2

master
燕鹏 4 years ago
parent
commit
170d97145d
  1. 2
      controllers/BlogController.go
  2. 2
      controllers/CatController.go
  3. 2
      controllers/CommentController.go
  4. 27
      controllers/FileController.go
  5. 2
      controllers/IndexController.go
  6. 6
      controllers/LikeController.go
  7. 2
      controllers/MapController.go
  8. 2
      controllers/NoteController.go
  9. 5
      controllers/PageController.go
  10. 2
      controllers/UPageController.go
  11. 12
      controllers/UserController.go
  12. 4
      filter/Security.go
  13. 6
      go.mod
  14. 10
      main.go
  15. 5
      mcontrollers/MBlogController.go
  16. 29
      models/DataInit.go
  17. 2
      routers/BlogRouter.go
  18. 2
      routers/CatRouter.go
  19. 2
      routers/CommentRouter.go
  20. 2
      routers/LikeRouter.go
  21. 2
      routers/MBlogRouter.go
  22. 2
      routers/NoteRouter.go
  23. 2
      routers/UPageRouter.go
  24. 2
      routers/UserRouter.go
  25. 2
      routers/router.go
  26. 20
      service/BlogService.go
  27. 2
      service/CategoryService.go
  28. 2
      service/CommentService.go
  29. 4
      service/LikeService.go
  30. 2
      service/NoteService.go
  31. 3
      service/UserService.go
  32. 2
      utils/EsUtil.go

2
controllers/BlogController.go

@ -4,7 +4,7 @@ import (
"beeblog/models"
"beeblog/service"
"beeblog/utils"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
"strconv"
"time"
)

2
controllers/CatController.go

@ -3,7 +3,7 @@ package controllers
import (
"beeblog/models"
"beeblog/service"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
)
type CatController struct {

2
controllers/CommentController.go

@ -1,9 +1,9 @@
package controllers
import (
"github.com/astaxie/beego"
"beeblog/models"
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
"strconv"
)

27
controllers/FileController.go

@ -1,15 +1,16 @@
package controllers
import (
"github.com/astaxie/beego"
"path"
"time"
"os"
"fmt"
"crypto/md5"
"math/rand"
"beeblog/models"
"beeblog/service"
"crypto/md5"
"fmt"
"github.com/beego/beego/v2/core/config"
beego "github.com/beego/beego/v2/server/web"
"math/rand"
"os"
"path"
"time"
)
type FileController struct {
@ -45,7 +46,8 @@ func (this *FileController) Upload() {
}
//创建目录
urlDir := time.Now().Format("2006/01/02/")
uploadDir := beego.AppConfig.String("uploadDir") + urlDir
uploadDir, _ := config.String("uploadDir")
uploadDir = uploadDir + urlDir
err := os.MkdirAll(uploadDir, 777)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
@ -67,7 +69,8 @@ func (this *FileController) Upload() {
this.ServeJSON()
return
}
urlDir = beego.AppConfig.String("imgUrlPrefix") + urlDir
imgUrlPrefix, _ := config.String("imgUrlPrefix")
urlDir = imgUrlPrefix + urlDir
this.Data["json"] = models.ReurnData("", urlDir)
this.ServeJSON()
return
@ -106,7 +109,8 @@ func (this *FileController) HeadImgUpload() {
}
//创建目录
urlDir := time.Now().Format("2006/01/02/")
uploadDir := beego.AppConfig.String("uploadDir") + urlDir
uploadDir, _ := config.String("uploadDir")
uploadDir = uploadDir + urlDir
err := os.MkdirAll(uploadDir, 777)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
@ -128,7 +132,8 @@ func (this *FileController) HeadImgUpload() {
this.ServeJSON()
return
}
urlDir = beego.AppConfig.String("imgUrlPrefix") + urlDir
imgUrlPrefix, _ := config.String("imgUrlPrefix")
urlDir = imgUrlPrefix + urlDir
user := &models.User{Id: uid.(int64), Headimg: urlDir}
userSerivce := service.UserService{}
if _, err = userSerivce.EditHeadImg(user); err != nil {

2
controllers/IndexController.go

@ -1,8 +1,8 @@
package controllers
import (
"github.com/astaxie/beego"
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
)
type IndexController struct {

6
controllers/LikeController.go

@ -1,10 +1,10 @@
package controllers
import (
"strconv"
"beeblog/service"
"github.com/astaxie/beego"
"beeblog/models"
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
"strconv"
)
type LikeController struct {

2
controllers/MapController.go

@ -1,6 +1,6 @@
package controllers
import "github.com/astaxie/beego"
import beego "github.com/beego/beego/v2/server/web"
type MapController struct {
beego.Controller

2
controllers/NoteController.go

@ -3,7 +3,7 @@ package controllers
import (
"beeblog/models"
"beeblog/service"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
"strconv"
)

5
controllers/PageController.go

@ -1,16 +1,15 @@
package controllers
import (
"github.com/astaxie/beego"
"beeblog/service"
"beeblog/models"
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
)
type PageController struct {
beego.Controller
}
func (this *PageController) IframeUser() {
userService := service.UserService{}
uid := this.GetSession("userid")

2
controllers/UPageController.go

@ -1,6 +1,6 @@
package controllers
import "github.com/astaxie/beego"
import beego "github.com/beego/beego/v2/server/web"
type UPageController struct {
beego.Controller

12
controllers/UserController.go

@ -1,14 +1,14 @@
package controllers
import (
"github.com/astaxie/beego"
"beeblog/service"
"beeblog/models"
"strings"
"beeblog/service"
"crypto/md5"
"time"
"strconv"
"encoding/hex"
beego "github.com/beego/beego/v2/server/web"
"strconv"
"strings"
"time"
)
type UserController struct {
@ -295,7 +295,7 @@ func (this *UserController) Regist() {
return
}
h := md5.New()
h.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10) + beego.AppConfig.String("host")))
h.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10) + "aiprose:nelson"))
salt := hex.EncodeToString(h.Sum(nil))
h = md5.New()
h.Write([]byte(userpwd + salt))

4
filter/Security.go

@ -1,10 +1,10 @@
package filter
import (
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/logs"
"beeblog/models"
"encoding/json"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web/context"
)
var LogFilter = func(ctx *context.Context) {

6
go.mod

@ -1,10 +1,10 @@
module beeblog
require (
github.com/astaxie/beego v1.12.0
github.com/beego/bee/v2 v2.0.2 // indirect
github.com/beego/beego/v2 v2.0.1
github.com/fortytw2/leaktest v1.3.0 // indirect
github.com/go-sql-driver/mysql v1.4.1
github.com/google/go-cmp v0.3.1 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e // indirect
github.com/olivere/elastic v6.2.22+incompatible
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect

10
main.go

@ -4,10 +4,10 @@ import (
"beeblog/filter"
"beeblog/models"
_ "beeblog/routers"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/plugins/cors"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/logs"
beego "github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/filter/cors"
)
func init() {
@ -17,7 +17,7 @@ func init() {
}
func main() {
orm.Debug = false
orm.RunSyncdb("default", false, true)
beego.AddFuncMap("NAdd", NAdd)
logs.SetLogger(logs.AdapterFile, `{"filename":"/opt/logs/aiprose.log","level":1}`)

5
mcontrollers/MBlogController.go

@ -3,7 +3,7 @@ package mcontrollers
import (
"beeblog/models"
"beeblog/service"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
"strconv"
"time"
)
@ -12,7 +12,6 @@ type MBlogController struct {
beego.Controller
}
func (this *MBlogController) Get() {
blogService := service.BlogService{}
userService := service.UserService{}
@ -31,7 +30,6 @@ func (this *MBlogController) Get() {
return
}
func (this *MBlogController) BlogsPage() {
blogService := service.BlogService{}
num, _ := this.GetInt("num")
@ -58,7 +56,6 @@ func (this *MBlogController) BlogsPage() {
return
}
func (this *MBlogController) EditPage() {
blogService := service.BlogService{}
uid := this.GetSession("userid")

29
models/DataInit.go

@ -1,29 +1,18 @@
package models
import (
"github.com/astaxie/beego"
//"github.com/Unknwon/com"
//"os"
//"path"
"github.com/astaxie/beego/orm"
//_ "github.com/mattn/go-sqlite3"
"fmt"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/config"
_ "github.com/go-sql-driver/mysql"
)
const (
_DB_NAME = "data/beeblog.db"
_SQLITE3_DRIVER = "sqlite3"
)
func RegistDB() {
//if !com.IsExist(_DB_NAME){
// os.MkdirAll(path.Dir(_DB_NAME),os.ModePerm)
// os.Create(_DB_NAME)
//}
//orm.RegisterModel(new(Attachment),new(Topic))
//orm.RegisterDriver(_SQLITE3_DRIVER,orm.DRSqlite)
//orm.RegisterDataBase("default",_SQLITE3_DRIVER,_DB_NAME,10)
dbUrl, _ := config.String("dburl")
fmt.Println(dbUrl)
orm.RegisterDriver("mysql", orm.DRMySQL)
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.RegisterDataBase("default", "mysql", "root:"+beego.AppConfig.String("dburl")+"/beeblog?charset=utf8&loc=Local", 30)
//orm.RunSyncdb("default", false, true)
}

2
routers/BlogRouter.go

@ -2,7 +2,7 @@ package routers
import (
"beeblog/controllers"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
)
func init() {

2
routers/CatRouter.go

@ -2,7 +2,7 @@ package routers
import (
"beeblog/controllers"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
)
func init() {

2
routers/CommentRouter.go

@ -1,8 +1,8 @@
package routers
import (
"github.com/astaxie/beego"
"beeblog/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init() {

2
routers/LikeRouter.go

@ -2,7 +2,7 @@ package routers
import (
"beeblog/controllers"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
)
func init() {

2
routers/MBlogRouter.go

@ -2,7 +2,7 @@ package routers
import (
"beeblog/mcontrollers"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
)
func init() {

2
routers/NoteRouter.go

@ -1,8 +1,8 @@
package routers
import (
"github.com/astaxie/beego"
"beeblog/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init() {

2
routers/UPageRouter.go

@ -2,7 +2,7 @@ package routers
import (
"beeblog/controllers"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
)
func init() {

2
routers/UserRouter.go

@ -2,7 +2,7 @@ package routers
import (
"beeblog/controllers"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
)
func init() {

2
routers/router.go

@ -2,7 +2,7 @@ package routers
import (
"beeblog/controllers"
"github.com/astaxie/beego"
beego "github.com/beego/beego/v2/server/web"
)
func init() {

20
service/BlogService.go

@ -1,9 +1,9 @@
package service
import (
"github.com/astaxie/beego/orm"
"beeblog/models"
"beeblog/utils"
"github.com/beego/beego/v2/client/orm"
)
type BlogService struct {
@ -133,7 +133,6 @@ func (this *BlogService) FindBlogs(num int, size int, cat int64, flag int) (*uti
return page, nil
}
func (this *BlogService) AllBlogs() ([]*models.Blog, error) {
var blogs []*models.Blog
@ -147,13 +146,12 @@ func (this *BlogService) AllBlogs() ([]*models.Blog, error) {
return blogs, nil
}
func (this *BlogService) SaveBlog(blog *models.Blog, strs []string) error {
o := orm.NewOrm()
o.Begin()
tx, _ := o.Begin()
id, eror := o.Insert(blog)
if eror != nil {
o.Rollback()
tx.Rollback()
return eror
} else {
blog.Id = id
@ -163,21 +161,21 @@ func (this *BlogService) SaveBlog(blog *models.Blog, strs []string) error {
nlabels[i] = &models.NLabel{Title: strs[i], BlogId: id, UserId: blog.UserId}
}
if _, err := o.InsertMulti(len(nlabels), nlabels); err != nil {
o.Rollback()
tx.Rollback()
return err
}
}
o.Commit()
tx.Commit()
}
return nil
}
func (this *BlogService) EditBlog(blog *models.Blog, strs []string) error {
o := orm.NewOrm()
o.Begin()
tx, _ := o.Begin()
_, eror := o.Update(blog)
if eror != nil {
o.Rollback()
tx.Rollback()
return eror
} else {
o.QueryTable(models.NLabel{}).Filter("BlogId", blog.Id).Delete()
@ -187,11 +185,11 @@ func (this *BlogService) EditBlog(blog *models.Blog, strs []string) error {
nlabels[i] = &models.NLabel{Title: strs[i], BlogId: blog.Id, UserId: blog.UserId}
}
if _, err := o.InsertMulti(len(nlabels), nlabels); err != nil {
o.Rollback()
tx.Rollback()
return err
}
}
o.Commit()
tx.Commit()
}
return nil
}

2
service/CategoryService.go

@ -1,8 +1,8 @@
package service
import (
"github.com/astaxie/beego/orm"
"beeblog/models"
"github.com/beego/beego/v2/client/orm"
)
type CategoryService struct {

2
service/CommentService.go

@ -2,8 +2,8 @@ package service
import (
"beeblog/models"
"github.com/astaxie/beego/orm"
"fmt"
"github.com/beego/beego/v2/client/orm"
)
type CommentService struct {

4
service/LikeService.go

@ -2,9 +2,9 @@ package service
import (
"beeblog/models"
"github.com/astaxie/beego/orm"
"fmt"
"beeblog/utils"
"fmt"
"github.com/beego/beego/v2/client/orm"
)
type LikeService struct {

2
service/NoteService.go

@ -2,8 +2,8 @@ package service
import (
"beeblog/models"
"github.com/astaxie/beego/orm"
"errors"
"github.com/beego/beego/v2/client/orm"
)
type NoteService struct {

3
service/UserService.go

@ -1,8 +1,8 @@
package service
import (
"github.com/astaxie/beego/orm"
"beeblog/models"
"github.com/beego/beego/v2/client/orm"
)
type UserService struct {
@ -36,7 +36,6 @@ func (this *UserService) SaveUser(user *models.User) error {
return eror
} else {
user.Id = id
o.Commit()
}
return nil
}

2
utils/EsUtil.go

@ -13,7 +13,7 @@ import (
)
var client *elastic.Client
var host = "http://47.98.109.5:8209/"
var host = "http://49.235.160.131:6002/"
const mapping = `
{

Loading…
Cancel
Save