Skip to content

Commit

Permalink
feat: 网站备份管理与上传备份500修复
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 20, 2023
1 parent a86badc commit 361199d
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 21 deletions.
32 changes: 29 additions & 3 deletions app/http/controllers/website_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,16 @@ func (r *WebsiteController) UpdateRemark(ctx http.Context) http.Response {
// @Tags 网站管理
// @Produce json
// @Security BearerToken
// @Success 200 {object} SuccessResponse{data=[]services.BackupFile}
// @Param data body commonrequests.Paginate true "request"
// @Success 200 {object} SuccessResponse{data=[]services.BackupFile}
// @Router /panel/website/backupList [get]
func (r *WebsiteController) BackupList(ctx http.Context) http.Response {
var paginateRequest commonrequests.Paginate
sanitize := Sanitize(ctx, &paginateRequest)
if sanitize != nil {
return sanitize
}

backupList, err := r.backup.WebsiteList()
if err != nil {
facades.Log().Request(ctx.Request()).Tags("面板", "网站管理").With(map[string]any{
Expand All @@ -338,7 +345,26 @@ func (r *WebsiteController) BackupList(ctx http.Context) http.Response {
return ErrorSystem(ctx)
}

return Success(ctx, backupList)
startIndex := (paginateRequest.Page - 1) * paginateRequest.Limit
endIndex := paginateRequest.Page * paginateRequest.Limit
if startIndex > len(backupList) {
return Success(ctx, http.Json{
"total": 0,
"items": []services.BackupFile{},
})
}
if endIndex > len(backupList) {
endIndex = len(backupList)
}
pagedBackupList := backupList[startIndex:endIndex]
if pagedBackupList == nil {
pagedBackupList = []services.BackupFile{}
}

return Success(ctx, http.Json{
"total": len(backupList),
"items": pagedBackupList,
})
}

// CreateBackup
Expand Down Expand Up @@ -389,7 +415,7 @@ func (r *WebsiteController) CreateBackup(ctx http.Context) http.Response {
// @Security BearerToken
// @Param file formData file true "备份文件"
// @Success 200 {object} SuccessResponse
// @Router /panel/website/uploadBackup [post]
// @Router /panel/website/uploadBackup [put]
func (r *WebsiteController) UploadBackup(ctx http.Context) http.Response {
file, err := ctx.Request().File("file")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion app/services/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (s *BackupImpl) WebsiteRestore(website models.Website, backupFile string) e
return errors.New("备份文件不存在")
}

if _, err := tools.Exec(`rm -rf '` + website.Path + `/*'`); err != nil {
if err := tools.Remove(website.Path); err != nil {
return err
}
if err := tools.UnArchive(backupFile, website.Path); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func init() {
"fiber": map[string]any{
// prefork mode, see https://docs.gofiber.io/api/fiber/#config
"prefork": false,
// Optional, default is 4MB
"body_limit": 4000,
"route": func() (route.Route, error) {
return fiberfacades.Route("fiber"), nil
},
Expand Down
19 changes: 15 additions & 4 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,17 @@ const docTemplate = `{
"网站管理"
],
"summary": "获取备份列表",
"parameters": [
{
"description": "request",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/commonrequests.Paginate"
}
}
],
"responses": {
"200": {
"description": "OK",
Expand Down Expand Up @@ -1177,7 +1188,7 @@ const docTemplate = `{
}
},
"/panel/website/uploadBackup": {
"post": {
"put": {
"security": [
{
"BearerToken": []
Expand Down Expand Up @@ -1930,7 +1941,7 @@ const docTemplate = `{
"ports": {
"type": "array",
"items": {
"type": "string"
"type": "integer"
}
}
}
Expand Down Expand Up @@ -2089,7 +2100,7 @@ const docTemplate = `{
"ports": {
"type": "array",
"items": {
"type": "string"
"type": "integer"
}
},
"raw": {
Expand Down Expand Up @@ -2334,7 +2345,7 @@ const docTemplate = `{
"ports": {
"type": "array",
"items": {
"type": "string"
"type": "integer"
}
},
"remark": {
Expand Down
19 changes: 15 additions & 4 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,17 @@
"网站管理"
],
"summary": "获取备份列表",
"parameters": [
{
"description": "request",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/commonrequests.Paginate"
}
}
],
"responses": {
"200": {
"description": "OK",
Expand Down Expand Up @@ -1170,7 +1181,7 @@
}
},
"/panel/website/uploadBackup": {
"post": {
"put": {
"security": [
{
"BearerToken": []
Expand Down Expand Up @@ -1923,7 +1934,7 @@
"ports": {
"type": "array",
"items": {
"type": "string"
"type": "integer"
}
}
}
Expand Down Expand Up @@ -2082,7 +2093,7 @@
"ports": {
"type": "array",
"items": {
"type": "string"
"type": "integer"
}
},
"raw": {
Expand Down Expand Up @@ -2327,7 +2338,7 @@
"ports": {
"type": "array",
"items": {
"type": "string"
"type": "integer"
}
},
"remark": {
Expand Down
15 changes: 11 additions & 4 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ definitions:
type: integer
ports:
items:
type: string
type: integer
type: array
type: object
requests.CertStore:
Expand Down Expand Up @@ -278,7 +278,7 @@ definitions:
type: integer
ports:
items:
type: string
type: integer
type: array
raw:
type: string
Expand Down Expand Up @@ -438,7 +438,7 @@ definitions:
type: integer
ports:
items:
type: string
type: integer
type: array
remark:
type: string
Expand Down Expand Up @@ -1056,6 +1056,13 @@ paths:
/panel/website/backupList:
get:
description: 获取网站的备份列表
parameters:
- description: request
in: body
name: data
required: true
schema:
$ref: '#/definitions/commonrequests.Paginate'
produces:
- application/json
responses:
Expand Down Expand Up @@ -1147,7 +1154,7 @@ paths:
tags:
- 网站管理
/panel/website/uploadBackup:
post:
put:
consumes:
- application/json
description: 上传网站的备份
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-acme/lego/v4 v4.14.2
github.com/gookit/color v1.5.4
github.com/gookit/validate v1.5.1
github.com/goravel/fiber v1.1.11-0.20231117182543-0e63ed108e45
github.com/goravel/fiber v1.1.11-0.20231120162926-818d5d2c1b6d
github.com/goravel/framework v1.13.1-0.20231117140817-50ecec9871ec
github.com/iancoleman/strcase v0.3.0
github.com/imroc/req/v3 v3.42.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ github.com/gookit/goutil v0.6.14 h1:96elyOG4BvVoDaiT7vx1vHPrVyEtFfYlPPBODR0/FGQ=
github.com/gookit/goutil v0.6.14/go.mod h1:YyDBddefmjS+mU2PDPgCcjVzTDM5WgExiDv5ZA/b8I8=
github.com/gookit/validate v1.5.1 h1:rPp64QZQJM+fysGFAhKpvekQAav4Ok6sjfTs9ZtxcpA=
github.com/gookit/validate v1.5.1/go.mod h1:SskOHUQokzMNt6T3r7N+N/4me/6fxDx+tmoXf/3ZQog=
github.com/goravel/fiber v1.1.11-0.20231117182543-0e63ed108e45 h1:BLIO7rbTvd1pOw1YfaCoyoL9YmBURK98DnDEOIjosRg=
github.com/goravel/fiber v1.1.11-0.20231117182543-0e63ed108e45/go.mod h1:XPw0Fc2s0VEKbPYp+ka8UIrPdbkV9iNQ4eYov8YcuC8=
github.com/goravel/fiber v1.1.11-0.20231120162926-818d5d2c1b6d h1:qBZUa4X629DgFQmwQkSO4MrmwXi0/A394xSVI2+rW14=
github.com/goravel/fiber v1.1.11-0.20231120162926-818d5d2c1b6d/go.mod h1:sgL5mMyIm9GSDRuepAAf22wte+dVo7F1Y8fWsWj14rs=
github.com/goravel/file-rotatelogs v0.0.0-20211215053220-2ab31dd9575c h1:obhFK91JAhcf7s6h5sggZishm1VyGW/gBCreo+7/SwQ=
github.com/goravel/file-rotatelogs v0.0.0-20211215053220-2ab31dd9575c/go.mod h1:YSWsLXlG16u5CWFaXNZHhEQD10+NwF3xfgDV816OwLE=
github.com/goravel/file-rotatelogs/v2 v2.4.1 h1:ogkeIFcTHSBRUBpZYiyJbpul8hkVXxHPuDbOaP78O1M=
Expand Down
4 changes: 2 additions & 2 deletions routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func Api() {
r.Get("defaultConfig", websiteController.GetDefaultConfig)
r.Post("defaultConfig", websiteController.SaveDefaultConfig)
r.Get("backupList", websiteController.BackupList)
r.Post("uploadBackup", websiteController.UploadBackup)
r.Post("deleteBackup", websiteController.DeleteBackup)
r.Put("uploadBackup", websiteController.UploadBackup)
r.Delete("deleteBackup", websiteController.DeleteBackup)
})
r.Prefix("websites").Middleware(middleware.Jwt(), middleware.MustInstall()).Group(func(r route.Router) {
websiteController := controllers.NewWebsiteController()
Expand Down

0 comments on commit 361199d

Please sign in to comment.