From f757862ebe8824d3e50e31f093778a85885990e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Mon, 30 Oct 2023 03:25:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=8C=E6=AD=A5=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/http/controllers/info_controller.go | 16 ++++++------ .../postgresql15/postgresql15_controller.go | 15 ++++++++--- .../postgresql16/postgresql16_controller.go | 15 ++++++++--- .../controllers/plugins/toolbox/toolbox.go | 25 ++++++++++++++----- 4 files changed, 50 insertions(+), 21 deletions(-) diff --git a/app/http/controllers/info_controller.go b/app/http/controllers/info_controller.go index 6885dbc4ab..5a06c80fdc 100644 --- a/app/http/controllers/info_controller.go +++ b/app/http/controllers/info_controller.go @@ -156,14 +156,16 @@ func (c *InfoController) CountInfo(ctx http.Context) http.Response { if status == "active" { raw := tools.Exec(`echo "\l" | su - postgres -c "psql"`) databases := strings.Split(raw, "\n") - databases = databases[3 : len(databases)-1] - for _, db := range databases { - parts := strings.Split(db, "|") - if len(parts) != 9 || len(strings.TrimSpace(parts[0])) == 0 || strings.TrimSpace(parts[0]) == "template0" || strings.TrimSpace(parts[0]) == "template1" || strings.TrimSpace(parts[0]) == "postgres" { - continue - } + if len(databases) >= 4 { + databases = databases[3 : len(databases)-1] + for _, db := range databases { + parts := strings.Split(db, "|") + if len(parts) != 9 || len(strings.TrimSpace(parts[0])) == 0 || strings.TrimSpace(parts[0]) == "template0" || strings.TrimSpace(parts[0]) == "template1" || strings.TrimSpace(parts[0]) == "postgres" { + continue + } - databaseCount++ + databaseCount++ + } } } } diff --git a/app/http/controllers/plugins/postgresql15/postgresql15_controller.go b/app/http/controllers/plugins/postgresql15/postgresql15_controller.go index 5b2fc1874d..1510ffbe6e 100644 --- a/app/http/controllers/plugins/postgresql15/postgresql15_controller.go +++ b/app/http/controllers/plugins/postgresql15/postgresql15_controller.go @@ -256,16 +256,23 @@ func (c *Postgresql15Controller) DatabaseList(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, "PostgreSQL 已停止运行") } - raw := tools.Exec(`echo "\l" | su - postgres -c "psql"`) - databases := strings.Split(raw, "\n") - databases = databases[3 : len(databases)-1] - type database struct { Name string `json:"name"` Owner string `json:"owner"` Encoding string `json:"encoding"` } + raw := tools.Exec(`echo "\l" | su - postgres -c "psql"`) + databases := strings.Split(raw, "\n") + if len(databases) >= 4 { + databases = databases[3 : len(databases)-1] + } else { + return controllers.Success(ctx, http.Json{ + "total": 0, + "items": []database{}, + }) + } + var databaseList []database for _, db := range databases { parts := strings.Split(db, "|") diff --git a/app/http/controllers/plugins/postgresql16/postgresql16_controller.go b/app/http/controllers/plugins/postgresql16/postgresql16_controller.go index 57fd8e5f49..46f72386a3 100644 --- a/app/http/controllers/plugins/postgresql16/postgresql16_controller.go +++ b/app/http/controllers/plugins/postgresql16/postgresql16_controller.go @@ -256,16 +256,23 @@ func (c *Postgresql16Controller) DatabaseList(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusInternalServerError, "PostgreSQL 已停止运行") } - raw := tools.Exec(`echo "\l" | su - postgres -c "psql"`) - databases := strings.Split(raw, "\n") - databases = databases[3 : len(databases)-1] - type database struct { Name string `json:"name"` Owner string `json:"owner"` Encoding string `json:"encoding"` } + raw := tools.Exec(`echo "\l" | su - postgres -c "psql"`) + databases := strings.Split(raw, "\n") + if len(databases) >= 4 { + databases = databases[3 : len(databases)-1] + } else { + return controllers.Success(ctx, http.Json{ + "total": 0, + "items": []database{}, + }) + } + var databaseList []database for _, db := range databases { parts := strings.Split(db, "|") diff --git a/app/http/controllers/plugins/toolbox/toolbox.go b/app/http/controllers/plugins/toolbox/toolbox.go index 7d35a974e7..1562ee613c 100644 --- a/app/http/controllers/plugins/toolbox/toolbox.go +++ b/app/http/controllers/plugins/toolbox/toolbox.go @@ -2,7 +2,6 @@ package toolbox import ( "regexp" - "strconv" "strings" "github.com/goravel/framework/contracts/http" @@ -69,13 +68,14 @@ func (c *ToolBoxController) GetSWAP(ctx http.Context) http.Response { return check } - var total, size, used, free string + var total, used, free string + var size int64 if tools.Exists("/www/swap") { s, _ := tools.FileSize("/www/swap") - size = strconv.Itoa(int(s / 1024 / 1024)) + size = s / 1024 / 1024 total = tools.FormatBytes(float64(s)) } else { - size = "0.00 B" + size = 0 total = "0.00 B" } @@ -142,12 +142,25 @@ func (c *ToolBoxController) GetTimezone(ctx http.Context) http.Response { return controllers.Error(ctx, http.StatusUnprocessableEntity, "找不到时区信息") } + type zone struct { + Label string `json:"label"` + Value string `json:"value"` + } + zonesRaw := tools.Exec("LC_ALL=C timedatectl list-timezones") zones := strings.Split(zonesRaw, "\n") + var zonesList []zone + for _, z := range zones { + zonesList = append(zonesList, zone{ + Label: z, + Value: z, + }) + } + return controllers.Success(ctx, http.Json{ - "zone": match[1], - "zones": zones, + "timezone": match[1], + "timezones": zonesList, }) }