Skip to content

Commit

Permalink
refactor: move types
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed May 29, 2024
1 parent 873520e commit 73cc5a3
Show file tree
Hide file tree
Showing 27 changed files with 490 additions and 472 deletions.
4 changes: 2 additions & 2 deletions app/console/commands/cert_renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/goravel/framework/support/carbon"

"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/types"
)

// CertRenew 证书续签
Expand All @@ -39,7 +39,7 @@ func (receiver *CertRenew) Extend() command.Extend {

// Handle Execute the console command.
func (receiver *CertRenew) Handle(console.Context) error {
if internal.Status != internal.StatusNormal {
if types.Status != types.StatusNormal {
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions app/console/commands/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/spf13/cast"

"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)

// Monitoring 系统监控
Expand All @@ -40,7 +40,7 @@ func (receiver *Monitoring) Extend() command.Extend {

// Handle Execute the console command.
func (receiver *Monitoring) Handle(console.Context) error {
if internal.Status != internal.StatusNormal {
if types.Status != types.StatusNormal {
return nil
}

Expand Down Expand Up @@ -68,7 +68,7 @@ func (receiver *Monitoring) Handle(console.Context) error {
cpu.Flags = nil
}

if internal.Status != internal.StatusNormal {
if types.Status != types.StatusNormal {
return nil
}
err := facades.Orm().Query().Create(&models.Monitor{
Expand All @@ -82,7 +82,7 @@ func (receiver *Monitoring) Handle(console.Context) error {

// 删除过期数据
days := cast.ToInt(setting.Get(models.SettingKeyMonitorDays))
if days <= 0 || internal.Status != internal.StatusNormal {
if days <= 0 || types.Status != types.StatusNormal {
return nil
}
if _, err = facades.Orm().Query().Where("created_at < ?", carbon.Now().SubDays(days).ToDateTimeString()).Delete(&models.Monitor{}); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions app/console/commands/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
"github.com/spf13/cast"

"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)

// Panel 面板命令行
Expand Down Expand Up @@ -99,14 +99,14 @@ func (receiver *Panel) Handle(ctx console.Context) error {
return err
}

internal.Status = internal.StatusUpgrade
types.Status = types.StatusUpgrade
if err = tools.UpdatePanel(panel); err != nil {
internal.Status = internal.StatusFailed
types.Status = types.StatusFailed
color.Red().Printfln(translate.Get("commands.panel.update.fail") + ": " + err.Error())
return nil
}

internal.Status = internal.StatusNormal
types.Status = types.StatusNormal
color.Green().Printfln(translate.Get("commands.panel.update.success"))
tools.RestartPanel()

Expand Down Expand Up @@ -603,7 +603,7 @@ func (receiver *Panel) Handle(ctx console.Context) error {
return nil
}

_, err = website.Add(internal.PanelWebsite{
_, err = website.Add(types.Website{
Name: name,
Status: true,
Domains: domains,
Expand Down
12 changes: 6 additions & 6 deletions app/console/commands/panel_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/goravel/framework/facades"
"github.com/goravel/framework/support/carbon"

"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)

// PanelTask 面板每日任务
Expand All @@ -35,11 +35,11 @@ func (receiver *PanelTask) Extend() command.Extend {

// Handle Execute the console command.
func (receiver *PanelTask) Handle(console.Context) error {
internal.Status = internal.StatusMaintain
types.Status = types.StatusMaintain

// 优化数据库
if _, err := facades.Orm().Query().Exec("VACUUM"); err != nil {
internal.Status = internal.StatusFailed
types.Status = types.StatusFailed
facades.Log().Tags("面板", "每日任务").
With(map[string]any{
"error": err.Error(),
Expand All @@ -49,7 +49,7 @@ func (receiver *PanelTask) Handle(console.Context) error {

// 备份面板
if err := tools.Archive([]string{"/www/panel"}, "/www/backup/panel/panel-"+carbon.Now().ToShortDateTimeString()+".zip"); err != nil {
internal.Status = internal.StatusFailed
types.Status = types.StatusFailed
facades.Log().Tags("面板", "每日任务").
With(map[string]any{
"error": err.Error(),
Expand All @@ -59,14 +59,14 @@ func (receiver *PanelTask) Handle(console.Context) error {

// 清理 7 天前的备份
if _, err := tools.Exec(`find /www/backup/panel -mtime +7 -name "*.zip" -exec rm -rf {} \;`); err != nil {
internal.Status = internal.StatusFailed
types.Status = types.StatusFailed
facades.Log().Tags("面板", "每日任务").
With(map[string]any{
"error": err.Error(),
}).Error("清理面板备份失败")
return err
}

internal.Status = internal.StatusNormal
types.Status = types.StatusNormal
return nil
}
7 changes: 4 additions & 3 deletions app/http/controllers/info_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)

type MenuItem struct {
Expand Down Expand Up @@ -310,16 +311,16 @@ func (r *InfoController) Update(ctx http.Context) http.Response {
return Error(ctx, http.StatusInternalServerError, "获取最新版本失败")
}

internal.Status = internal.StatusUpgrade
types.Status = types.StatusUpgrade
if err = tools.UpdatePanel(panel); err != nil {
internal.Status = internal.StatusFailed
types.Status = types.StatusFailed
facades.Log().Request(ctx.Request()).Tags("面板", "基础信息").With(map[string]any{
"error": err.Error(),
}).Info("更新面板失败")
return Error(ctx, http.StatusInternalServerError, err.Error())
}

internal.Status = internal.StatusNormal
types.Status = types.StatusNormal
tools.RestartPanel()
return Success(ctx, nil)
}
Expand Down
5 changes: 3 additions & 2 deletions app/http/controllers/plugins/mysql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)

type MySQLController struct {
Expand Down Expand Up @@ -377,15 +378,15 @@ func (r *MySQLController) BackupList(ctx http.Context) http.Response {
if startIndex > len(backupList) {
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []internal.BackupFile{},
"items": []types.BackupFile{},
})
}
if endIndex > len(backupList) {
endIndex = len(backupList)
}
pagedBackupList := backupList[startIndex:endIndex]
if pagedBackupList == nil {
pagedBackupList = []internal.BackupFile{}
pagedBackupList = []types.BackupFile{}
}

return controllers.Success(ctx, http.Json{
Expand Down
4 changes: 2 additions & 2 deletions app/http/controllers/plugins/postgresql15_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,15 @@ func (r *Postgresql15Controller) BackupList(ctx http.Context) http.Response {
if startIndex > len(backupList) {
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []internal.BackupFile{},
"items": []types.BackupFile{},
})
}
if endIndex > len(backupList) {
endIndex = len(backupList)
}
pagedBackupList := backupList[startIndex:endIndex]
if pagedBackupList == nil {
pagedBackupList = []internal.BackupFile{}
pagedBackupList = []types.BackupFile{}
}

return controllers.Success(ctx, http.Json{
Expand Down
4 changes: 2 additions & 2 deletions app/http/controllers/plugins/postgresql16_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,15 @@ func (r *Postgresql16Controller) BackupList(ctx http.Context) http.Response {
if startIndex > len(backupList) {
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []internal.BackupFile{},
"items": []types.BackupFile{},
})
}
if endIndex > len(backupList) {
endIndex = len(backupList)
}
pagedBackupList := backupList[startIndex:endIndex]
if pagedBackupList == nil {
pagedBackupList = []internal.BackupFile{}
pagedBackupList = []types.BackupFile{}
}

return controllers.Success(ctx, http.Json{
Expand Down
11 changes: 6 additions & 5 deletions app/http/controllers/website_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)

type WebsiteController struct {
Expand Down Expand Up @@ -83,7 +84,7 @@ func (r *WebsiteController) Add(ctx http.Context) http.Response {
addRequest.Path = r.setting.Get(models.SettingKeyWebsitePath) + "/" + addRequest.Name
}

website := internal.PanelWebsite{
website := types.Website{
Name: addRequest.Name,
Status: true,
Domains: addRequest.Domains,
Expand Down Expand Up @@ -205,7 +206,7 @@ func (r *WebsiteController) SaveDefaultConfig(ctx http.Context) http.Response {
// @Produce json
// @Security BearerToken
// @Param id path int true "网站 ID"
// @Success 200 {object} SuccessResponse{data=internal.PanelWebsite}
// @Success 200 {object} SuccessResponse{data=types.Website}
// @Router /panel/websites/{id}/config [get]
func (r *WebsiteController) GetConfig(ctx http.Context) http.Response {
var idRequest requests.ID
Expand Down Expand Up @@ -328,7 +329,7 @@ func (r *WebsiteController) UpdateRemark(ctx http.Context) http.Response {
// @Produce json
// @Security BearerToken
// @Param data query commonrequests.Paginate true "request"
// @Success 200 {object} SuccessResponse{data=[]internal.BackupFile}
// @Success 200 {object} SuccessResponse{data=[]types.BackupFile}
// @Router /panel/website/backupList [get]
func (r *WebsiteController) BackupList(ctx http.Context) http.Response {
var paginateRequest commonrequests.Paginate
Expand All @@ -350,15 +351,15 @@ func (r *WebsiteController) BackupList(ctx http.Context) http.Response {
if startIndex > len(backupList) {
return Success(ctx, http.Json{
"total": 0,
"items": []internal.BackupFile{},
"items": []types.BackupFile{},
})
}
if endIndex > len(backupList) {
endIndex = len(backupList)
}
pagedBackupList := backupList[startIndex:endIndex]
if pagedBackupList == nil {
pagedBackupList = []internal.BackupFile{}
pagedBackupList = []types.BackupFile{}
}

return Success(ctx, http.Json{
Expand Down
12 changes: 6 additions & 6 deletions app/http/middleware/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/facades"

"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/types"
)

// Status 检查程序状态
func Status() http.Middleware {
return func(ctx http.Context) {
translate := facades.Lang(ctx)
switch internal.Status {
case internal.StatusUpgrade:
switch types.Status {
case types.StatusUpgrade:
ctx.Request().AbortWithStatusJson(http.StatusServiceUnavailable, http.Json{
"message": translate.Get("status.upgrade"),
})
return
case internal.StatusMaintain:
case types.StatusMaintain:
ctx.Request().AbortWithStatusJson(http.StatusServiceUnavailable, http.Json{
"message": translate.Get("status.maintain"),
})
return
case internal.StatusClosed:
case types.StatusClosed:
ctx.Request().AbortWithStatusJson(http.StatusForbidden, http.Json{
"message": translate.Get("status.closed"),
})
return
case internal.StatusFailed:
case types.StatusFailed:
ctx.Request().AbortWithStatusJson(http.StatusInternalServerError, http.Json{
"message": translate.Get("status.failed"),
})
Expand Down
Loading

0 comments on commit 73cc5a3

Please sign in to comment.