Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
goozp committed Sep 12, 2020
1 parent 8837b18 commit c02aa76
Show file tree
Hide file tree
Showing 104 changed files with 3,206 additions and 2,603 deletions.
5 changes: 3 additions & 2 deletions configs/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ safety:

# logger
log:
logger_file: logs/server.log
logger_file_info: logs/info.log
logger_file_error: logs/error.log
logger_max_size: 1024 # megabytes
logger_max_backups: 7
logger_max_age: 31 # days
Expand All @@ -25,7 +26,7 @@ log:
db:
db_type: mysql
name: db_puti # database name
addr: localhost:3306 # host:port
addr: 127.0.0.1:3306 # host:port
username: putiroot
password: puti123456
max_open_conns: 150
Expand Down
5 changes: 3 additions & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Changelog

## v0.3.0
## v0.3.0 Optimization [2020-09-12]

### 功能和优化
- 优化 Lin 主题 Latex 公式样式在移动端的展示
- 移除 vendor 目录和依赖,使用 goproxy
- 支持 Github Actions
- 大量基础组件优化和重构
- 后台 api 结构重构
- 升级 GROM 到 v2

### Bug 修复

- 修复标签修改出错的问题

---

Expand Down
15 changes: 15 additions & 0 deletions docs/CHANGELOG_EN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## v0.3.0 Optimization [2020-09-12]

### Feature and Enhancement
- Optimize the display of the Lin theme Latex formula style on the mobile terminal
- Remove vendor directory and dependencies, use goproxy
- Support Github Actions
- A large number of basic component optimization and reconstruction
- Back-end api structure reconstruction
- Upgrade GROM to v2

### Bug Fixed
- Fix the problem of incorrect label modification

---

## v0.2.2 Article Cover Picture [2019-08-14]

### Feature and Enhancement
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ require (
golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gorm.io/driver/mysql v1.0.1
gorm.io/gorm v1.20.0
gorm.io/gorm v1.20.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ gorm.io/driver/mysql v1.0.1/go.mod h1:KtqSthtg55lFp3S5kUXqlGaelnWpKitn4k1xZTnoiP
gorm.io/gorm v1.9.19/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.0 h1:qfIlyaZvrF7kMWY3jBdEBXkXJ2M5MFYMTppjILxS3fQ=
gorm.io/gorm v1.20.0/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.1 h1:+hOwlHDqvqmBIMflemMVPLJH7tZYK4RxFDBHEfJTup0=
gorm.io/gorm v1.20.1/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
57 changes: 57 additions & 0 deletions internal/backend/api/article/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package article

import (
"github.com/puti-projects/puti/internal/backend/api"
"github.com/puti-projects/puti/internal/backend/service"
"github.com/puti-projects/puti/internal/pkg/errno"
"github.com/puti-projects/puti/internal/pkg/token"

"github.com/gin-gonic/gin"
)

// Create create article (published or draft) handler
func Create(c *gin.Context) {
// get token and parse
t := c.Query("token")
userContext, err := token.ParseToken(t)

var r service.ArticleCreateRequest
if err := c.Bind(&r); err != nil {
api.SendResponse(c, errno.ErrBind, nil)
return
}

// check params
if err := checkCreateParam(&r); err != nil {
api.SendResponse(c, err, nil)
return
}

rsp, err := service.CreateArticle(&r, userContext.ID)
if err != nil {
api.SendResponse(c, errno.ErrArticleCreateFailed, nil)
return
}

api.SendResponse(c, nil, rsp)
}

func checkCreateParam(r *service.ArticleCreateRequest) error {
if r.Title == "" {
return errno.New(errno.ErrValidation, nil).Add("Title can not be empty.")
}

if r.Content == "" {
return errno.New(errno.ErrValidation, nil).Add("Content can not be empty.")
}

if r.Status == "" {
return errno.New(errno.ErrValidation, nil).Add("Status can not be empty.")
}

if r.Status != "publish" && r.Status != "draft" {
return errno.New(errno.ErrValidation, nil).Add("Status is incorrect.")
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package article
import (
"strconv"

Response "github.com/puti-projects/puti/internal/backend/handler"
"github.com/puti-projects/puti/internal/backend/api"
"github.com/puti-projects/puti/internal/backend/service"
"github.com/puti-projects/puti/internal/pkg/errno"

"github.com/gin-gonic/gin"
)

// Delete delete article and it's relationship
// Delete delete article handler
func Delete(c *gin.Context) {
articleID, _ := strconv.Atoi(c.Param("id"))

if err := service.DeletePost("article", uint64(articleID)); err != nil {
Response.SendResponse(c, errno.ErrDatabase, nil)
api.SendResponse(c, errno.ErrDatabase, nil)
return
}

Response.SendResponse(c, nil, nil)
api.SendResponse(c, nil, nil)
}
24 changes: 24 additions & 0 deletions internal/backend/api/article/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package article

import (
"strconv"

"github.com/puti-projects/puti/internal/backend/api"
"github.com/puti-projects/puti/internal/backend/service"
"github.com/puti-projects/puti/internal/pkg/errno"

"github.com/gin-gonic/gin"
)

// Get get article handler (article info in detail)
func Get(c *gin.Context) {
articleID, _ := strconv.Atoi(c.Param("id"))

article, err := service.GetArticleDetail(uint64(articleID))
if err != nil {
api.SendResponse(c, errno.ErrArticleNotFount, nil)
return
}

api.SendResponse(c, nil, article)
}
40 changes: 40 additions & 0 deletions internal/backend/api/article/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package article

import (
"math"

"github.com/puti-projects/puti/internal/backend/api"
"github.com/puti-projects/puti/internal/backend/service"
"github.com/puti-projects/puti/internal/pkg/constvar"
"github.com/puti-projects/puti/internal/pkg/errno"

"github.com/gin-gonic/gin"
)

// List return the article list in page
func List(c *gin.Context) {
var r service.ArticleListRequest
if err := c.Bind(&r); err != nil {
api.SendResponse(c, errno.ErrBind, nil)
return
}

if r.Number == 0 {
r.Number = constvar.DefaultLimit
}

infos, count, err := service.ListArticle("article", &r)
if err != nil {
api.SendResponse(c, err, nil)
return
}

number := int64(r.Number)
totalPage := math.Ceil(float64(count / number))

api.SendResponse(c, nil, service.ArticleListResponse{
TotalCount: count,
TotalPage: uint64(totalPage),
ArticleList: infos,
})
}
72 changes: 72 additions & 0 deletions internal/backend/api/article/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package article

import (
"strconv"

"github.com/puti-projects/puti/internal/backend/api"
"github.com/puti-projects/puti/internal/backend/service"
"github.com/puti-projects/puti/internal/pkg/errno"

"github.com/gin-gonic/gin"
)

// Update update article
// Delete and restore article are also in this function and it depends on the 'status'
func Update(c *gin.Context) {
// Get article id
ID, _ := strconv.Atoi(c.Param("id"))

var r service.ArticleUpdateRequest
if err := c.ShouldBind(&r); err != nil {
api.SendResponse(c, errno.ErrBind, nil)
return
}

articleID := uint64(ID)

// check params
if err := checkUpdateParam(&r, articleID); err != nil {
api.SendResponse(c, err, nil)
return
}

if r.Status == "deleted" {
if err := service.TrashPost(articleID); err != nil {
api.SendResponse(c, err, nil)
return
}
} else if r.Status == "restore" {
if err := service.RestorePost(articleID); err != nil {
api.SendResponse(c, err, nil)
return
}
} else {
if err := service.UpdateArticle(&r); err != nil {
api.SendResponse(c, err, nil)
return
}
}

api.SendResponse(c, nil, nil)
return
}

func checkUpdateParam(r *service.ArticleUpdateRequest, articleID uint64) error {
if r.ID == 0 {
return errno.New(errno.ErrValidation, nil).Add("need id.")
}

if r.ID != articleID {
return errno.New(errno.ErrValidation, nil).Add("error id.")
}

if r.Status == "" {
return errno.New(errno.ErrValidation, nil).Add("need status.")
}

if r.Status != "publish" && r.Status != "draft" && r.Status != "deleted" && r.Status != "restore" {
return errno.New(errno.ErrValidation, nil).Add("error status.")
}

return nil
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package auth

import (
Response "github.com/puti-projects/puti/internal/backend/handler"
"github.com/puti-projects/puti/internal/backend/api"
"github.com/puti-projects/puti/internal/backend/service"
"github.com/puti-projects/puti/internal/pkg/errno"

Expand All @@ -14,9 +14,9 @@ func Info(c *gin.Context) {

user, err := service.GetUserByToken(t)
if err != nil {
Response.SendResponse(c, errno.ErrUserNotFound, nil)
api.SendResponse(c, errno.ErrUserNotFound, nil)
return
}

Response.SendResponse(c, nil, user)
api.SendResponse(c, nil, user)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package auth

import (
Response "github.com/puti-projects/puti/internal/backend/handler"
"github.com/puti-projects/puti/internal/backend/api"
"github.com/puti-projects/puti/internal/backend/service"
"github.com/puti-projects/puti/internal/pkg/errno"

Expand All @@ -12,15 +12,15 @@ import (
func Login(c *gin.Context) {
var u service.LoginRequest
if err := c.Bind(&u); err != nil {
Response.SendResponse(c, errno.ErrBind, nil)
api.SendResponse(c, errno.ErrBind, nil)
return
}

token, err := service.LoginAuth(c, u.Username, u.Password)
if err != nil {
Response.SendResponse(c, err, nil)
api.SendResponse(c, err, nil)
return
}

Response.SendResponse(c, nil, token)
api.SendResponse(c, nil, token)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package api

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package media
import (
"strconv"

Response "github.com/puti-projects/puti/internal/backend/handler"
"github.com/puti-projects/puti/internal/backend/api"
"github.com/puti-projects/puti/internal/backend/service"

"github.com/gin-gonic/gin"
Expand All @@ -14,9 +14,9 @@ func Delete(c *gin.Context) {
mediaID, _ := strconv.Atoi(c.Param("id"))

if err := service.DeleteMedia(uint64(mediaID)); err != nil {
Response.SendResponse(c, err, nil)
api.SendResponse(c, err, nil)
return
}

Response.SendResponse(c, nil, nil)
api.SendResponse(c, nil, nil)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package media
import (
"strconv"

Response "github.com/puti-projects/puti/internal/backend/handler"
"github.com/puti-projects/puti/internal/backend/api"
"github.com/puti-projects/puti/internal/backend/service"
"github.com/puti-projects/puti/internal/pkg/errno"

Expand All @@ -18,9 +18,9 @@ func Detail(c *gin.Context) {
mediaID, _ := strconv.Atoi(ID)
media, err := service.GetMediaDetail(uint64(mediaID))
if err != nil {
Response.SendResponse(c, errno.ErrMediaNotFound, nil)
api.SendResponse(c, errno.ErrMediaNotFound, nil)
return
}

Response.SendResponse(c, nil, media)
api.SendResponse(c, nil, media)
}
Loading

0 comments on commit c02aa76

Please sign in to comment.