diff --git a/app/http/controllers/container_controller.go b/app/http/controllers/container_controller.go index a244ed1e..f4c03f8d 100644 --- a/app/http/controllers/container_controller.go +++ b/app/http/controllers/container_controller.go @@ -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" ) @@ -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 { @@ -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, }) } @@ -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 { @@ -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, }) } @@ -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 { @@ -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, }) } @@ -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 { @@ -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, }) } diff --git a/app/http/controllers/controller.go b/app/http/controllers/controller.go index 3019aa67..04bcb4bd 100644 --- a/app/http/controllers/controller.go +++ b/app/http/controllers/controller.go @@ -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" ) @@ -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) diff --git a/app/http/controllers/file_controller.go b/app/http/controllers/file_controller.go index 269be5fc..f5f90847 100644 --- a/app/http/controllers/file_controller.go +++ b/app/http/controllers/file_controller.go @@ -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" @@ -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()) @@ -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, }) } diff --git a/app/http/controllers/plugin_controller.go b/app/http/controllers/plugin_controller.go index 8724fd33..7dd05629 100644 --- a/app/http/controllers/plugin_controller.go +++ b/app/http/controllers/plugin_controller.go @@ -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, }) } diff --git a/app/http/controllers/plugins/fail2ban_controller.go b/app/http/controllers/plugins/fail2ban_controller.go index c14712d6..e15496f8 100644 --- a/app/http/controllers/plugins/fail2ban_controller.go +++ b/app/http/controllers/plugins/fail2ban_controller.go @@ -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()) @@ -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, }) } diff --git a/app/http/controllers/plugins/mysql_controller.go b/app/http/controllers/plugins/mysql_controller.go index 51af0eb2..f2e0211f 100644 --- a/app/http/controllers/plugins/mysql_controller.go +++ b/app/http/controllers/plugins/mysql_controller.go @@ -187,21 +187,16 @@ func (r *MySQLController) SetRootPassword(ctx http.Context) http.Response { oldRootPassword := r.setting.Get(models.SettingKeyMysqlRootPassword) if oldRootPassword != rootPassword { - if _, err := tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + oldRootPassword + " -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '" + rootPassword + "';\""); err != nil { + if _, err = tools.Exec(fmt.Sprintf(`/www/server/mysql/bin/mysql -uroot -p%s -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '%s';"`, oldRootPassword, rootPassword)); err != nil { + return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("设置root密码失败: %v", err)) + } + if _, err = tools.Exec(fmt.Sprintf(`/www/server/mysql/bin/mysql -uroot -p%s -e "FLUSH PRIVILEGES;"`, rootPassword)); err != nil { return controllers.Error(ctx, http.StatusInternalServerError, "设置root密码失败") } - if _, err := tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + oldRootPassword + " -e \"FLUSH PRIVILEGES;\""); err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "设置root密码失败") - } - err := r.setting.Set(models.SettingKeyMysqlRootPassword, rootPassword) - if err != nil { - if _, err := tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '" + oldRootPassword + "';\""); err != nil { - return nil - } - if _, err := tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\""); err != nil { - return nil - } - return controllers.Error(ctx, http.StatusInternalServerError, "设置root密码失败") + if err = r.setting.Set(models.SettingKeyMysqlRootPassword, rootPassword); err != nil { + _, _ = tools.Exec(fmt.Sprintf(`/www/server/mysql/bin/mysql -uroot -p%s -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '%s';"`, rootPassword, oldRootPassword)) + _, _ = tools.Exec(fmt.Sprintf(`/www/server/mysql/bin/mysql -uroot -p%s -e "FLUSH PRIVILEGES;"`, oldRootPassword)) + return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("设置保存失败: %v", err)) } } @@ -240,40 +235,22 @@ func (r *MySQLController) DatabaseList(ctx http.Context) http.Response { var databases []database for rows.Next() { var d database - err := rows.Scan(&d.Name) - if err != nil { + if err = rows.Scan(&d.Name); err != nil { continue } databases = append(databases, d) } - if err := rows.Err(); err != nil { + if err = rows.Err(); err != nil { return controllers.Error(ctx, http.StatusInternalServerError, "获取数据库列表失败") } - page := ctx.Request().QueryInt("page", 1) - limit := ctx.Request().QueryInt("limit", 10) - startIndex := (page - 1) * limit - endIndex := page * limit - if startIndex > len(databases) { - return controllers.Success(ctx, http.Json{ - "total": 0, - "items": []database{}, - }) - } - if endIndex > len(databases) { - endIndex = len(databases) - } - pagedDatabases := databases[startIndex:endIndex] - - if pagedDatabases == nil { - pagedDatabases = []database{} - } + paged, total := controllers.Paginate(ctx, databases) return controllers.Success(ctx, http.Json{ - "total": len(databases), - "items": pagedDatabases, + "total": total, + "items": paged, }) } @@ -327,32 +304,16 @@ func (r *MySQLController) DeleteDatabase(ctx http.Context) http.Response { // BackupList 获取备份列表 func (r *MySQLController) BackupList(ctx http.Context) http.Response { - backupList, err := r.backup.MysqlList() + backups, err := r.backup.MysqlList() if err != nil { return controllers.Error(ctx, http.StatusInternalServerError, err.Error()) } - page := ctx.Request().QueryInt("page", 1) - limit := ctx.Request().QueryInt("limit", 10) - startIndex := (page - 1) * limit - endIndex := page * limit - if startIndex > len(backupList) { - return controllers.Success(ctx, http.Json{ - "total": 0, - "items": []types.BackupFile{}, - }) - } - if endIndex > len(backupList) { - endIndex = len(backupList) - } - pagedBackupList := backupList[startIndex:endIndex] - if pagedBackupList == nil { - pagedBackupList = []types.BackupFile{} - } + paged, total := controllers.Paginate(ctx, backups) return controllers.Success(ctx, http.Json{ - "total": len(backupList), - "items": pagedBackupList, + "total": total, + "items": paged, }) } @@ -463,8 +424,7 @@ func (r *MySQLController) UserList(ctx http.Context) http.Response { for rows.Next() { var u user - err := rows.Scan(&u.User, &u.Host) - if err != nil { + if err = rows.Scan(&u.User, &u.Host); err != nil { continue } @@ -477,47 +437,29 @@ func (r *MySQLController) UserList(ctx http.Context) http.Response { for grantsRows.Next() { var grant string - err := grantsRows.Scan(&grant) - if err != nil { + if err = grantsRows.Scan(&grant); err != nil { continue } u.Grants = append(u.Grants, grant) } - if err := grantsRows.Err(); err != nil { + if err = grantsRows.Err(); err != nil { continue } userGrants = append(userGrants, u) } - if err := rows.Err(); err != nil { + if err = rows.Err(); err != nil { return controllers.Error(ctx, http.StatusInternalServerError, "获取用户列表失败") } - page := ctx.Request().QueryInt("page", 1) - limit := ctx.Request().QueryInt("limit", 10) - startIndex := (page - 1) * limit - endIndex := page * limit - if startIndex > len(userGrants) { - return controllers.Success(ctx, http.Json{ - "total": 0, - "items": []user{}, - }) - } - if endIndex > len(userGrants) { - endIndex = len(userGrants) - } - pagedUserGrants := userGrants[startIndex:endIndex] - - if pagedUserGrants == nil { - pagedUserGrants = []user{} - } + paged, total := controllers.Paginate(ctx, userGrants) return controllers.Success(ctx, http.Json{ - "total": len(userGrants), - "items": pagedUserGrants, + "total": total, + "items": paged, }) } diff --git a/app/http/controllers/plugins/postgresql_controller.go b/app/http/controllers/plugins/postgresql_controller.go index cf85adb9..23740abe 100644 --- a/app/http/controllers/plugins/postgresql_controller.go +++ b/app/http/controllers/plugins/postgresql_controller.go @@ -189,28 +189,11 @@ func (r *PostgreSQLController) DatabaseList(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, err.Error()) } - page := ctx.Request().QueryInt("page", 1) - limit := ctx.Request().QueryInt("limit", 10) - startIndex := (page - 1) * limit - endIndex := page * limit - if startIndex > len(databases) { - return controllers.Success(ctx, http.Json{ - "total": 0, - "items": []database{}, - }) - } - if endIndex > len(databases) { - endIndex = len(databases) - } - pagedDatabases := databases[startIndex:endIndex] - - if pagedDatabases == nil { - pagedDatabases = []database{} - } + paged, total := controllers.Paginate(ctx, databases) return controllers.Success(ctx, http.Json{ - "total": len(databases), - "items": pagedDatabases, + "total": total, + "items": paged, }) } @@ -271,32 +254,16 @@ func (r *PostgreSQLController) DeleteDatabase(ctx http.Context) http.Response { // BackupList 获取备份列表 func (r *PostgreSQLController) BackupList(ctx http.Context) http.Response { - backupList, err := r.backup.PostgresqlList() + backups, err := r.backup.PostgresqlList() if err != nil { return controllers.Error(ctx, http.StatusInternalServerError, "获取备份列表失败") } - page := ctx.Request().QueryInt("page", 1) - limit := ctx.Request().QueryInt("limit", 10) - startIndex := (page - 1) * limit - endIndex := page * limit - if startIndex > len(backupList) { - return controllers.Success(ctx, http.Json{ - "total": 0, - "items": []types.BackupFile{}, - }) - } - if endIndex > len(backupList) { - endIndex = len(backupList) - } - pagedBackupList := backupList[startIndex:endIndex] - if pagedBackupList == nil { - pagedBackupList = []types.BackupFile{} - } + paged, total := controllers.Paginate(ctx, backups) return controllers.Success(ctx, http.Json{ - "total": len(backupList), - "items": pagedBackupList, + "total": total, + "items": paged, }) } @@ -440,28 +407,11 @@ func (r *PostgreSQLController) RoleList(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, err.Error()) } - page := ctx.Request().QueryInt("page", 1) - limit := ctx.Request().QueryInt("limit", 10) - startIndex := (page - 1) * limit - endIndex := page * limit - if startIndex > len(roles) { - return controllers.Success(ctx, http.Json{ - "total": 0, - "items": []role{}, - }) - } - if endIndex > len(roles) { - endIndex = len(roles) - } - pagedRoles := roles[startIndex:endIndex] - - if pagedRoles == nil { - pagedRoles = []role{} - } + paged, total := controllers.Paginate(ctx, roles) return controllers.Success(ctx, http.Json{ - "total": len(roles), - "items": pagedRoles, + "total": total, + "items": paged, }) } diff --git a/app/http/controllers/plugins/pureftpd_controller.go b/app/http/controllers/plugins/pureftpd_controller.go index 75f2a751..9148833c 100644 --- a/app/http/controllers/plugins/pureftpd_controller.go +++ b/app/http/controllers/plugins/pureftpd_controller.go @@ -43,28 +43,11 @@ func (r *PureFtpdController) 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(users) { - return controllers.Success(ctx, http.Json{ - "total": 0, - "items": []types.PureFtpdUser{}, - }) - } - if endIndex > len(users) { - endIndex = len(users) - } - pagedUsers := users[startIndex:endIndex] - - if pagedUsers == nil { - pagedUsers = []types.PureFtpdUser{} - } + paged, total := controllers.Paginate(ctx, users) return controllers.Success(ctx, http.Json{ - "total": len(users), - "items": pagedUsers, + "total": total, + "items": paged, }) } diff --git a/app/http/controllers/plugins/rsync_controller.go b/app/http/controllers/plugins/rsync_controller.go index 358664f0..24f843a2 100644 --- a/app/http/controllers/plugins/rsync_controller.go +++ b/app/http/controllers/plugins/rsync_controller.go @@ -7,7 +7,6 @@ import ( "github.com/goravel/framework/contracts/http" "github.com/TheTNB/panel/app/http/controllers" - commonrequests "github.com/TheTNB/panel/app/http/requests/common" requests "github.com/TheTNB/panel/app/http/requests/plugins/rsync" "github.com/TheTNB/panel/pkg/tools" "github.com/TheTNB/panel/types" @@ -31,12 +30,6 @@ func NewRsyncController() *RsyncController { // @Success 200 {object} controllers.SuccessResponse // @Router /plugins/rsync/modules [get] func (r *RsyncController) List(ctx http.Context) http.Response { - var paginateRequest commonrequests.Paginate - sanitize := controllers.SanitizeRequest(ctx, &paginateRequest) - if sanitize != nil { - return sanitize - } - config, err := tools.Read("/etc/rsyncd.conf") if err != nil { return controllers.Error(ctx, http.StatusInternalServerError, err.Error()) @@ -91,25 +84,11 @@ func (r *RsyncController) List(ctx http.Context) http.Response { modules = append(modules, *currentModule) } - startIndex := (paginateRequest.Page - 1) * paginateRequest.Limit - endIndex := paginateRequest.Page * paginateRequest.Limit - if startIndex > len(modules) { - return controllers.Success(ctx, http.Json{ - "total": 0, - "items": []types.RsyncModule{}, - }) - } - if endIndex > len(modules) { - endIndex = len(modules) - } - pagedModules := modules[startIndex:endIndex] - if pagedModules == nil { - pagedModules = []types.RsyncModule{} - } + paged, total := controllers.Paginate(ctx, modules) return controllers.Success(ctx, http.Json{ - "total": len(modules), - "items": pagedModules, + "total": total, + "items": paged, }) } diff --git a/app/http/controllers/plugins/s3fs_controller.go b/app/http/controllers/plugins/s3fs_controller.go index 5a91969f..daadcebd 100644 --- a/app/http/controllers/plugins/s3fs_controller.go +++ b/app/http/controllers/plugins/s3fs_controller.go @@ -27,34 +27,17 @@ func NewS3fsController() *S3fsController { // List 所有 S3fs 挂载 func (r *S3fsController) List(ctx http.Context) http.Response { - page := ctx.Request().QueryInt("page", 1) - limit := ctx.Request().QueryInt("limit", 10) - var s3fsList []types.S3fsMount err := json.UnmarshalString(r.setting.Get("s3fs", "[]"), &s3fsList) if err != nil { return controllers.Error(ctx, http.StatusInternalServerError, "获取 S3fs 挂载失败") } - startIndex := (page - 1) * limit - endIndex := page * limit - if startIndex > len(s3fsList) { - return controllers.Success(ctx, http.Json{ - "total": 0, - "items": []types.S3fsMount{}, - }) - } - if endIndex > len(s3fsList) { - endIndex = len(s3fsList) - } - pagedS3fsList := s3fsList[startIndex:endIndex] - if pagedS3fsList == nil { - pagedS3fsList = []types.S3fsMount{} - } + paged, total := controllers.Paginate(ctx, s3fsList) return controllers.Success(ctx, http.Json{ - "total": len(s3fsList), - "items": pagedS3fsList, + "total": total, + "items": paged, }) } diff --git a/app/http/controllers/plugins/supervisor_controller.go b/app/http/controllers/plugins/supervisor_controller.go index 04ce0d16..35747bcf 100644 --- a/app/http/controllers/plugins/supervisor_controller.go +++ b/app/http/controllers/plugins/supervisor_controller.go @@ -92,9 +92,6 @@ func (r *SupervisorController) SaveConfig(ctx http.Context) http.Response { // Processes 进程列表 func (r *SupervisorController) Processes(ctx http.Context) http.Response { - page := ctx.Request().QueryInt("page", 1) - limit := ctx.Request().QueryInt("limit", 10) - type process struct { Name string `json:"name"` Status string `json:"status"` @@ -107,7 +104,7 @@ func (r *SupervisorController) Processes(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, out) } - var processList []process + var processes []process for _, line := range strings.Split(out, "\n") { if len(line) == 0 { continue @@ -127,28 +124,14 @@ func (r *SupervisorController) Processes(ctx http.Context) http.Response { p.Pid = "-" p.Uptime = "-" } - processList = append(processList, p) + processes = append(processes, p) } - startIndex := (page - 1) * limit - endIndex := page * limit - if startIndex > len(processList) { - return controllers.Success(ctx, http.Json{ - "total": 0, - "items": []process{}, - }) - } - if endIndex > len(processList) { - endIndex = len(processList) - } - pagedProcessList := processList[startIndex:endIndex] - if pagedProcessList == nil { - pagedProcessList = []process{} - } + paged, total := controllers.Paginate(ctx, processes) return controllers.Success(ctx, http.Json{ - "total": len(processList), - "items": pagedProcessList, + "total": total, + "items": paged, }) } diff --git a/app/http/controllers/safe_controller.go b/app/http/controllers/safe_controller.go index 0ea88eb8..5a2e4b44 100644 --- a/app/http/controllers/safe_controller.go +++ b/app/http/controllers/safe_controller.go @@ -4,7 +4,6 @@ import ( "regexp" "strings" - commonrequests "github.com/TheTNB/panel/app/http/requests/common" "github.com/goravel/framework/contracts/http" "github.com/spf13/cast" @@ -77,12 +76,6 @@ func (r *SafeController) SetFirewallStatus(ctx http.Context) http.Response { // GetFirewallRules 获取防火墙规则 func (r *SafeController) GetFirewallRules(ctx http.Context) http.Response { - var paginateRequest commonrequests.Paginate - sanitize := SanitizeRequest(ctx, &paginateRequest) - if sanitize != nil { - return sanitize - } - if !r.firewallStatus() { return Success(ctx, nil) } @@ -130,26 +123,11 @@ func (r *SafeController) GetFirewallRules(ctx http.Context) http.Response { } } - startIndex := (paginateRequest.Page - 1) * paginateRequest.Limit - endIndex := paginateRequest.Page * paginateRequest.Limit - if startIndex > len(rules) { - return Success(ctx, http.Json{ - "total": 0, - "items": []map[string]string{}, - }) - } - if endIndex > len(rules) { - endIndex = len(rules) - } - pagedRules := rules[startIndex:endIndex] - - if pagedRules == nil { - pagedRules = []map[string]string{} - } + paged, total := Paginate(ctx, rules) return Success(ctx, http.Json{ - "total": len(rules), - "items": pagedRules, + "total": total, + "items": paged, }) } diff --git a/app/http/controllers/website_controller.go b/app/http/controllers/website_controller.go index 4a4a39b5..24e4cb36 100644 --- a/app/http/controllers/website_controller.go +++ b/app/http/controllers/website_controller.go @@ -328,7 +328,7 @@ func (r *WebsiteController) BackupList(ctx http.Context) http.Response { return sanitize } - backupList, err := r.backup.WebsiteList() + backups, err := r.backup.WebsiteList() if err != nil { facades.Log().Request(ctx.Request()).Tags("面板", "网站管理").With(map[string]any{ "error": err.Error(), @@ -336,25 +336,11 @@ func (r *WebsiteController) BackupList(ctx http.Context) http.Response { return ErrorSystem(ctx) } - startIndex := (paginateRequest.Page - 1) * paginateRequest.Limit - endIndex := paginateRequest.Page * paginateRequest.Limit - if startIndex > len(backupList) { - return Success(ctx, http.Json{ - "total": 0, - "items": []types.BackupFile{}, - }) - } - if endIndex > len(backupList) { - endIndex = len(backupList) - } - pagedBackupList := backupList[startIndex:endIndex] - if pagedBackupList == nil { - pagedBackupList = []types.BackupFile{} - } + paged, total := Paginate(ctx, backups) return Success(ctx, http.Json{ - "total": len(backupList), - "items": pagedBackupList, + "total": total, + "items": paged, }) }