Skip to content

Commit

Permalink
feat: 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Feb 9, 2025
1 parent 89be5e0 commit 6cff72d
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 51 deletions.
2 changes: 1 addition & 1 deletion internal/apps/nginx/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *App) SaveConfig(w http.ResponseWriter, r *http.Request) {

if err = systemctl.Reload("nginx"); err != nil {
_, err = shell.Execf("nginx -t")
service.Error(w, http.StatusInternalServerError, "重载服务失败%v", err)
service.Error(w, http.StatusInternalServerError, "重载服务失败: %v", err)
return
}

Expand Down
12 changes: 6 additions & 6 deletions internal/apps/postgresql/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func (s *App) UpdateConfig(w http.ResponseWriter, r *http.Request) {
return
}

if err := io.Write(fmt.Sprintf("%s/server/postgresql/data/postgresql.conf", app.Root), req.Config, 0644); err != nil {
if err = io.Write(fmt.Sprintf("%s/server/postgresql/data/postgresql.conf", app.Root), req.Config, 0644); err != nil {
service.Error(w, http.StatusInternalServerError, "写入PostgreSQL配置失败")
return
}

if err := systemctl.Reload("postgresql"); err != nil {
service.Error(w, http.StatusInternalServerError, "重载服务失败")
if err = systemctl.Reload("postgresql"); err != nil {
service.Error(w, http.StatusInternalServerError, "重载服务失败: %v", err)
return
}

Expand All @@ -84,13 +84,13 @@ func (s *App) UpdateUserConfig(w http.ResponseWriter, r *http.Request) {
return
}

if err := io.Write(fmt.Sprintf("%s/server/postgresql/data/pg_hba.conf", app.Root), req.Config, 0644); err != nil {
if err = io.Write(fmt.Sprintf("%s/server/postgresql/data/pg_hba.conf", app.Root), req.Config, 0644); err != nil {
service.Error(w, http.StatusInternalServerError, "写入PostgreSQL配置失败")
return
}

if err := systemctl.Reload("postgresql"); err != nil {
service.Error(w, http.StatusInternalServerError, "重载服务失败")
if err = systemctl.Reload("postgresql"); err != nil {
service.Error(w, http.StatusInternalServerError, "重载服务失败: %v", err)
return
}

Expand Down
2 changes: 1 addition & 1 deletion internal/apps/supervisor/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *App) UpdateConfig(w http.ResponseWriter, r *http.Request) {
}

if err = systemctl.Restart(s.name); err != nil {
service.Error(w, http.StatusInternalServerError, "重启 %s 服务失败", s.name)
service.Error(w, http.StatusInternalServerError, "重启 %s 服务失败: %v", s.name, err)
return
}

Expand Down
20 changes: 10 additions & 10 deletions internal/data/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,59 +152,59 @@ func (r *containerRepo) Create(req *request.ContainerCreate) (string, error) {

// Remove 移除容器
func (r *containerRepo) Remove(id string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker rm -f %s", id)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker rm -f %s", id)
return err
}

// Start 启动容器
func (r *containerRepo) Start(id string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker start %s", id)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker start %s", id)
return err
}

// Stop 停止容器
func (r *containerRepo) Stop(id string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker stop %s", id)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker stop %s", id)
return err
}

// Restart 重启容器
func (r *containerRepo) Restart(id string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker restart %s", id)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker restart %s", id)
return err
}

// Pause 暂停容器
func (r *containerRepo) Pause(id string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker pause %s", id)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker pause %s", id)
return err
}

// Unpause 恢复容器
func (r *containerRepo) Unpause(id string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker unpause %s", id)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker unpause %s", id)
return err
}

// Kill 杀死容器
func (r *containerRepo) Kill(id string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker kill %s", id)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker kill %s", id)
return err
}

// Rename 重命名容器
func (r *containerRepo) Rename(id string, newName string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker rename %s %s", id, newName)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker rename %s %s", id, newName)
return err
}

// Logs 查看容器日志
func (r *containerRepo) Logs(id string) (string, error) {
return shell.ExecfWithTimeout(120*time.Second, "docker logs %s", id)
return shell.ExecfWithTimeout(2*time.Minute, "docker logs %s", id)
}

// Prune 清理未使用的容器
func (r *containerRepo) Prune() error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker container prune -f")
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker container prune -f")
return err
}
4 changes: 2 additions & 2 deletions internal/data/container_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ func (r *containerImageRepo) Pull(req *request.ContainerImagePull) error {

// Remove 删除镜像
func (r *containerImageRepo) Remove(id string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker rmi %s", id)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker rmi %s", id)
return err
}

// Prune 清理未使用的镜像
func (r *containerImageRepo) Prune() error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker image prune -f")
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker image prune -f")
return err
}
4 changes: 2 additions & 2 deletions internal/data/container_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ func (r *containerNetworkRepo) Create(req *request.ContainerNetworkCreate) (stri

// Remove 删除网络
func (r *containerNetworkRepo) Remove(id string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker network rm -f %s", id)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker network rm -f %s", id)
return err
}

// Prune 清理未使用的网络
func (r *containerNetworkRepo) Prune() error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker network prune -f")
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker network prune -f")
return err
}
4 changes: 2 additions & 2 deletions internal/data/container_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ func (r *containerVolumeRepo) Create(req *request.ContainerVolumeCreate) (string

// Remove 删除存储卷
func (r *containerVolumeRepo) Remove(id string) error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker volume rm -f %s", id)
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker volume rm -f %s", id)
return err
}

// Prune 清理未使用的存储卷
func (r *containerVolumeRepo) Prune() error {
_, err := shell.ExecfWithTimeout(120*time.Second, "docker volume prune -f")
_, err := shell.ExecfWithTimeout(2*time.Minute, "docker volume prune -f")
return err
}
16 changes: 8 additions & 8 deletions internal/service/systemctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (s *SystemctlService) Status(w http.ResponseWriter, r *http.Request) {

status, err := systemctl.Status(req.Service)
if err != nil {
Error(w, http.StatusInternalServerError, "获取 %s 服务运行状态失败", req.Service)
Error(w, http.StatusInternalServerError, "获取 %s 服务运行状态失败: %v", req.Service, err)
return
}

Expand All @@ -39,7 +39,7 @@ func (s *SystemctlService) IsEnabled(w http.ResponseWriter, r *http.Request) {

enabled, err := systemctl.IsEnabled(req.Service)
if err != nil {
Error(w, http.StatusInternalServerError, "获取 %s 服务启用状态失败", req.Service)
Error(w, http.StatusInternalServerError, "获取 %s 服务启用状态失败: %v", req.Service, err)
return
}

Expand All @@ -54,7 +54,7 @@ func (s *SystemctlService) Enable(w http.ResponseWriter, r *http.Request) {
}

if err = systemctl.Enable(req.Service); err != nil {
Error(w, http.StatusInternalServerError, "启用 %s 服务失败", req.Service)
Error(w, http.StatusInternalServerError, "启用 %s 服务失败: %v", req.Service, err)
return
}

Expand All @@ -69,7 +69,7 @@ func (s *SystemctlService) Disable(w http.ResponseWriter, r *http.Request) {
}

if err = systemctl.Disable(req.Service); err != nil {
Error(w, http.StatusInternalServerError, "禁用 %s 服务失败", req.Service)
Error(w, http.StatusInternalServerError, "禁用 %s 服务失败: %v", req.Service, err)
return
}

Expand All @@ -84,7 +84,7 @@ func (s *SystemctlService) Restart(w http.ResponseWriter, r *http.Request) {
}

if err = systemctl.Restart(req.Service); err != nil {
Error(w, http.StatusInternalServerError, "重启 %s 服务失败", req.Service)
Error(w, http.StatusInternalServerError, "重启 %s 服务失败: %v", req.Service, err)
return
}

Expand All @@ -99,7 +99,7 @@ func (s *SystemctlService) Reload(w http.ResponseWriter, r *http.Request) {
}

if err = systemctl.Reload(req.Service); err != nil {
Error(w, http.StatusInternalServerError, "重载 %s 服务失败", req.Service)
Error(w, http.StatusInternalServerError, "重载 %s 服务失败: %v", req.Service, err)
return
}

Expand All @@ -114,7 +114,7 @@ func (s *SystemctlService) Start(w http.ResponseWriter, r *http.Request) {
}

if err = systemctl.Start(req.Service); err != nil {
Error(w, http.StatusInternalServerError, "启动 %s 服务失败", req.Service)
Error(w, http.StatusInternalServerError, "启动 %s 服务失败: %v", req.Service, err)
return
}

Expand All @@ -129,7 +129,7 @@ func (s *SystemctlService) Stop(w http.ResponseWriter, r *http.Request) {
}

if err = systemctl.Stop(req.Service); err != nil {
Error(w, http.StatusInternalServerError, "停止 %s 服务失败", req.Service)
Error(w, http.StatusInternalServerError, "停止 %s 服务失败: %v", req.Service, err)
return
}

Expand Down
17 changes: 9 additions & 8 deletions pkg/systemctl/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package systemctl
import (
"errors"
"fmt"
"time"

"github.com/tnb-labs/panel/pkg/shell"
)
Expand Down Expand Up @@ -38,48 +39,48 @@ func IsEnabled(name string) (bool, error) {

// Start 启动服务
func Start(name string) error {
_, err := shell.Execf("systemctl start %s", name)
_, err := shell.ExecfWithTimeout(2*time.Minute, "systemctl start %s", name)
return err
}

// Stop 停止服务
func Stop(name string) error {
_, err := shell.Execf("systemctl stop %s", name)
_, err := shell.ExecfWithTimeout(2*time.Minute, "systemctl stop %s", name)
return err
}

// Restart 重启服务
func Restart(name string) error {
_, err := shell.Execf("systemctl restart %s", name)
_, err := shell.ExecfWithTimeout(2*time.Minute, "systemctl restart %s", name)
return err
}

// Reload 重载服务
func Reload(name string) error {
_, err := shell.Execf("systemctl reload %s", name)
_, err := shell.ExecfWithTimeout(2*time.Minute, "systemctl reload %s", name)
return err
}

// Enable 启用服务
func Enable(name string) error {
_, err := shell.Execf("systemctl enable %s", name)
_, err := shell.ExecfWithTimeout(2*time.Minute, "systemctl enable %s", name)
return err
}

// Disable 禁用服务
func Disable(name string) error {
_, err := shell.Execf("systemctl disable %s", name)
_, err := shell.ExecfWithTimeout(2*time.Minute, "systemctl disable %s", name)
return err
}

// Logs 获取服务日志
func Logs(name string) (string, error) {
return shell.Execf("journalctl -u %s", name)
return shell.ExecfWithTimeout(2*time.Minute, "journalctl -u %s", name)
}

// LogsTail 获取服务日志
func LogsTail(name string, lines int) (string, error) {
return shell.Execf("journalctl -u %s --lines %d", name, lines)
return shell.ExecfWithTimeout(2*time.Minute, "journalctl -u %s --lines %d", name, lines)
}

// LogsClear 清空服务日志
Expand Down
6 changes: 3 additions & 3 deletions web/src/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
"cache": "缓存更新成功",
"warning": "更新应用前强烈建议先备份/快照,以免出现问题时无法第一时间回滚!",
"setup": "设置成功",
"install": "任务已提交,请前往后台任务查看任务进度",
"update": "任务已提交,请前往后台任务查看任务进度",
"uninstall": "任务已提交,请前往后台任务查看任务进度"
"install": "任务已提交,请前往后台任务查看进度",
"update": "任务已提交,请前往后台任务查看进度",
"uninstall": "任务已提交,请前往后台任务查看进度"
},
"buttons": {
"updateCache": "更新缓存",
Expand Down
4 changes: 2 additions & 2 deletions web/src/views/apps/php/PhpView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ const handleReload = async () => {
const handleInstallExtension = async (slug: string) => {
useRequest(php.installExtension(version.value, slug)).onSuccess(() => {
window.$message.success('任务已提交,请稍后查看任务进度')
window.$message.success('任务已提交,请前往后台任务查看进度')
})
}
const handleUninstallExtension = async (name: string) => {
useRequest(php.uninstallExtension(version.value, name)).onSuccess(() => {
window.$message.success('任务已提交,请稍后查看任务进度')
window.$message.success('任务已提交,请前往后台任务查看进度')
})
}
Expand Down
12 changes: 6 additions & 6 deletions web/src/views/dashboard/IndexView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ if (import.meta.hot) {
</script>

<template>
<AppPage :show-footer="true" min-w-375>
<app-page :show-footer="true" min-w-375>
<div flex-1>
<n-space vertical>
<n-card :segmented="true" size="small">
Expand Down Expand Up @@ -664,15 +664,15 @@ if (import.meta.hot) {
item-responsive
responsive="screen"
>
<n-gi v-for="item in homeApps" :key="item.name">
<n-gi v-for="item in homeApps" :key="item.name" mx-10>
<n-card
:segmented="true"
size="small"
cursor-pointer
hover:card-shadow
@click="handleManageApp(item.slug)"
>
<n-space>
<n-flex>
<n-thing>
<template #avatar>
<div class="mt-8">
Expand All @@ -690,12 +690,12 @@ if (import.meta.hot) {
{{ item.version }}
</template>
</n-thing>
</n-space>
</n-flex>
</n-card>
</n-gi>
</n-grid>
</n-scrollbar>
<n-text v-if="!homeAppsLoading && !homeApps">
<n-text v-if="!homeAppsLoading && !homeApps.length">
您还没有设置任何应用在此显示!
</n-text>
<n-skeleton v-if="homeAppsLoading" text :repeat="12" />
Expand Down Expand Up @@ -819,5 +819,5 @@ if (import.meta.hot) {
</n-grid>
</n-space>
</div>
</AppPage>
</app-page>
</template>

0 comments on commit 6cff72d

Please sign in to comment.