Skip to content

Commit

Permalink
feat: 优化分页函数
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jun 18, 2024
1 parent 49b72aa commit ae9e39d
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 411 deletions.
93 changes: 8 additions & 85 deletions app/http/controllers/container_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import (
"strings"

"github.com/TheTNB/panel/pkg/tools"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume"
"github.com/docker/go-connections/nat"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/support/carbon"

commonrequests "github.com/TheTNB/panel/app/http/requests/common"
requests "github.com/TheTNB/panel/app/http/requests/container"
"github.com/TheTNB/panel/internal/services"
)
Expand All @@ -41,28 +37,12 @@ func NewContainerController() *ContainerController {
// @Success 200 {object} SuccessResponse
// @Router /panel/container/list [get]
func (r *ContainerController) ContainerList(ctx http.Context) http.Response {
var request commonrequests.Paginate
if sanitize := SanitizeRequest(ctx, &request); sanitize != nil {
return sanitize
}

containers, err := r.container.ContainerListAll()
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}

startIndex := (request.Page - 1) * request.Limit
endIndex := request.Page * request.Limit
if startIndex > len(containers) {
return Success(ctx, http.Json{
"total": 0,
"items": []any{},
})
}
if endIndex > len(containers) {
endIndex = len(containers)
}
paged := containers[startIndex:endIndex]
paged, total := Paginate(ctx, containers)

items := make([]any, 0)
for _, item := range paged {
Expand All @@ -85,7 +65,7 @@ func (r *ContainerController) ContainerList(ctx http.Context) http.Response {
}

return Success(ctx, http.Json{
"total": len(containers),
"total": total,
"items": items,
})
}
Expand Down Expand Up @@ -516,31 +496,12 @@ func (r *ContainerController) ContainerPrune(ctx http.Context) http.Response {
// @Success 200 {object} SuccessResponse
// @Router /panel/container/network/list [get]
func (r *ContainerController) NetworkList(ctx http.Context) http.Response {
var request commonrequests.Paginate
if sanitize := SanitizeRequest(ctx, &request); sanitize != nil {
return sanitize
}

networks, err := r.container.NetworkList()
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}

startIndex := (request.Page - 1) * request.Limit
endIndex := request.Page * request.Limit
if startIndex > len(networks) {
return Success(ctx, http.Json{
"total": 0,
"items": []any{},
})
}
if endIndex > len(networks) {
endIndex = len(networks)
}
paged := networks[startIndex:endIndex]
if paged == nil {
paged = []types.NetworkResource{}
}
paged, total := Paginate(ctx, networks)

items := make([]any, 0)
for _, item := range paged {
Expand Down Expand Up @@ -574,7 +535,7 @@ func (r *ContainerController) NetworkList(ctx http.Context) http.Response {
}

return Success(ctx, http.Json{
"total": len(networks),
"total": total,
"items": items,
})
}
Expand Down Expand Up @@ -751,31 +712,12 @@ func (r *ContainerController) NetworkPrune(ctx http.Context) http.Response {
// @Success 200 {object} SuccessResponse
// @Router /panel/container/image/list [get]
func (r *ContainerController) ImageList(ctx http.Context) http.Response {
var request commonrequests.Paginate
if sanitize := SanitizeRequest(ctx, &request); sanitize != nil {
return sanitize
}

images, err := r.container.ImageList()
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}

startIndex := (request.Page - 1) * request.Limit
endIndex := request.Page * request.Limit
if startIndex > len(images) {
return Success(ctx, http.Json{
"total": 0,
"items": []any{},
})
}
if endIndex > len(images) {
endIndex = len(images)
}
paged := images[startIndex:endIndex]
if paged == nil {
paged = []image.Summary{}
}
paged, total := Paginate(ctx, images)

items := make([]any, 0)
for _, item := range paged {
Expand All @@ -791,7 +733,7 @@ func (r *ContainerController) ImageList(ctx http.Context) http.Response {
}

return Success(ctx, http.Json{
"total": len(images),
"total": total,
"items": items,
})
}
Expand Down Expand Up @@ -919,31 +861,12 @@ func (r *ContainerController) ImageInspect(ctx http.Context) http.Response {
// @Success 200 {object} SuccessResponse
// @Router /panel/container/volume/list [get]
func (r *ContainerController) VolumeList(ctx http.Context) http.Response {
var request commonrequests.Paginate
if sanitize := SanitizeRequest(ctx, &request); sanitize != nil {
return sanitize
}

volumes, err := r.container.VolumeList()
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}

startIndex := (request.Page - 1) * request.Limit
endIndex := request.Page * request.Limit
if startIndex > len(volumes) {
return Success(ctx, http.Json{
"total": 0,
"items": []any{},
})
}
if endIndex > len(volumes) {
endIndex = len(volumes)
}
paged := volumes[startIndex:endIndex]
if paged == nil {
paged = []*volume.Volume{}
}
paged, total := Paginate(ctx, volumes)

items := make([]any, 0)
for _, item := range paged {
Expand All @@ -968,7 +891,7 @@ func (r *ContainerController) VolumeList(ctx http.Context) http.Response {
}

return Success(ctx, http.Json{
"total": len(volumes),
"total": total,
"items": items,
})
}
Expand Down
27 changes: 27 additions & 0 deletions app/http/controllers/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
commonrequests "github.com/TheTNB/panel/app/http/requests/common"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"
)
Expand Down Expand Up @@ -38,6 +39,32 @@ func ErrorSystem(ctx http.Context) http.Response {
})
}

func Paginate[T any](ctx http.Context, allItems []T) (pagedItems []T, total int) {
var paginateRequest commonrequests.Paginate
sanitize := SanitizeRequest(ctx, &paginateRequest)
if sanitize != nil {
return []T{}, 0
}

page := ctx.Request().QueryInt("page", 1)
limit := ctx.Request().QueryInt("limit", 10)
total = len(allItems)
startIndex := (page - 1) * limit
endIndex := page * limit

if total == 0 {
return []T{}, 0
}
if startIndex > total {
return []T{}, total
}
if endIndex > total {
endIndex = total
}

return allItems[startIndex:endIndex], total
}

// SanitizeRequest 消毒请求参数
func SanitizeRequest(ctx http.Context, request http.FormRequest) http.Response {
errors, err := ctx.Request().ValidateRequest(request)
Expand Down
23 changes: 2 additions & 21 deletions app/http/controllers/file_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"syscall"

commonrequests "github.com/TheTNB/panel/app/http/requests/common"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/support/carbon"

Expand Down Expand Up @@ -466,12 +465,6 @@ func (r *FileController) List(ctx http.Context) http.Response {
return sanitize
}

var paginate commonrequests.Paginate
paginateSanitize := SanitizeRequest(ctx, &paginate)
if paginateSanitize != nil {
return paginateSanitize
}

fileInfoList, err := os.ReadDir(request.Path)
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
Expand Down Expand Up @@ -500,22 +493,10 @@ func (r *FileController) List(ctx http.Context) http.Response {
})
}

start := paginate.Limit * (paginate.Page - 1)
end := paginate.Limit * paginate.Page
if start > len(paths) {
start = len(paths)
}
if end > len(paths) {
end = len(paths)
}

paged := paths[start:end]
if paged == nil {
paged = []any{}
}
paged, total := Paginate(ctx, paths)

return Success(ctx, http.Json{
"total": len(paths),
"total": total,
"items": paged,
})
}
Expand Down
19 changes: 3 additions & 16 deletions app/http/controllers/plugin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,11 @@ func (r *PluginController) List(ctx http.Context) http.Response {
})
}

page := ctx.Request().QueryInt("page", 1)
limit := ctx.Request().QueryInt("limit", 10)
startIndex := (page - 1) * limit
endIndex := page * limit
if startIndex > len(pluginArr) {
return Success(ctx, http.Json{
"total": 0,
"items": []plugin{},
})
}
if endIndex > len(pluginArr) {
endIndex = len(pluginArr)
}
pagedPlugins := pluginArr[startIndex:endIndex]
paged, total := Paginate(ctx, pluginArr)

return Success(ctx, http.Json{
"total": len(pluginArr),
"items": pagedPlugins,
"total": total,
"items": paged,
})
}

Expand Down
22 changes: 3 additions & 19 deletions app/http/controllers/plugins/fail2ban_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ func NewFail2banController() *Fail2banController {

// List 所有 Fail2ban 规则
func (r *Fail2banController) List(ctx http.Context) http.Response {
page := ctx.Request().QueryInt("page", 1)
limit := ctx.Request().QueryInt("limit", 10)
raw, err := tools.Read("/etc/fail2ban/jail.local")
if err != nil {
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
Expand Down Expand Up @@ -67,25 +65,11 @@ func (r *Fail2banController) List(ctx http.Context) http.Response {
})
}

startIndex := (page - 1) * limit
endIndex := page * limit
if startIndex > len(jails) {
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []types.Fail2banJail{},
})
}
if endIndex > len(jails) {
endIndex = len(jails)
}
pagedJails := jails[startIndex:endIndex]
if pagedJails == nil {
pagedJails = []types.Fail2banJail{}
}
paged, total := controllers.Paginate(ctx, jails)

return controllers.Success(ctx, http.Json{
"total": len(jails),
"items": pagedJails,
"total": total,
"items": paged,
})
}

Expand Down
Loading

0 comments on commit ae9e39d

Please sign in to comment.