From 89b7a0666272c0444033733a91f7ba1b42ea09e2 Mon Sep 17 00:00:00 2001 From: 1Panel-bot <127940126+1Panel-bot@users.noreply.github.com> Date: Mon, 20 Mar 2023 10:38:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=B3=BB=E7=BB=9F=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=8C=E6=AD=A5=E5=B7=B2=E5=AE=89=E8=A3=85?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E6=AD=A5=E9=AA=A4=20(#295)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: zhengkunwang223 --- backend/app/api/v1/app_install.go | 2 +- backend/app/service/app_install.go | 54 ++++++++++++++++++++++-------- backend/i18n/i18n.go | 14 ++++---- backend/init/business/business.go | 16 +++++++-- 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/backend/app/api/v1/app_install.go b/backend/app/api/v1/app_install.go index a7acd74ee..ea0051a3a 100644 --- a/backend/app/api/v1/app_install.go +++ b/backend/app/api/v1/app_install.go @@ -144,7 +144,7 @@ func (b *BaseApi) DeleteCheck(c *gin.Context) { // @Router /apps/installed/sync [post] // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFuntions":[],"formatZH":"同步已安装应用列表","formatEN":"Sync the list of installed apps"} func (b *BaseApi) SyncInstalled(c *gin.Context) { - if err := appInstallService.SyncAll(); err != nil { + if err := appInstallService.SyncAll(false); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index a6ef4cf95..2d81c0e9c 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -33,7 +33,28 @@ import ( type AppInstallService struct { } -func (a AppInstallService) Page(req request.AppInstalledSearch) (int64, []response.AppInstalledDTO, error) { +type IAppInstallService interface { + Page(req request.AppInstalledSearch) (int64, []response.AppInstalledDTO, error) + CheckExist(key string) (*response.AppInstalledCheck, error) + LoadPort(key string) (int64, error) + LoadPassword(key string) (string, error) + SearchForWebsite(req request.AppInstalledSearch) ([]response.AppInstalledDTO, error) + Operate(req request.AppInstalledOperate) error + Update(req request.AppInstalledUpdate) error + SyncAll(systemInit bool) error + GetServices(key string) ([]response.AppService, error) + GetUpdateVersions(installId uint) ([]dto.AppVersion, error) + GetParams(id uint) ([]response.AppParam, error) + ChangeAppPort(req request.PortUpdate) error + GetDefaultConfigByKey(key string) (string, error) + DeleteCheck(installId uint) ([]dto.AppResource, error) +} + +func NewIAppInstalledService() IAppInstallService { + return &AppInstallService{} +} + +func (a *AppInstallService) Page(req request.AppInstalledSearch) (int64, []response.AppInstalledDTO, error) { var opts []repo.DBOption if req.Name != "" { @@ -73,7 +94,7 @@ func (a AppInstallService) Page(req request.AppInstalledSearch) (int64, []respon return total, installDTOs, nil } -func (a AppInstallService) CheckExist(key string) (*response.AppInstalledCheck, error) { +func (a *AppInstallService) CheckExist(key string) (*response.AppInstalledCheck, error) { res := &response.AppInstalledCheck{ IsExist: false, } @@ -103,7 +124,7 @@ func (a AppInstallService) CheckExist(key string) (*response.AppInstalledCheck, return res, nil } -func (a AppInstallService) LoadPort(key string) (int64, error) { +func (a *AppInstallService) LoadPort(key string) (int64, error) { app, err := appInstallRepo.LoadBaseInfo(key, "") if err != nil { return int64(0), nil @@ -111,7 +132,7 @@ func (a AppInstallService) LoadPort(key string) (int64, error) { return app.Port, nil } -func (a AppInstallService) LoadPassword(key string) (string, error) { +func (a *AppInstallService) LoadPassword(key string) (string, error) { app, err := appInstallRepo.LoadBaseInfo(key, "") if err != nil { return "", nil @@ -119,7 +140,7 @@ func (a AppInstallService) LoadPassword(key string) (string, error) { return app.Password, nil } -func (a AppInstallService) SearchForWebsite(req request.AppInstalledSearch) ([]response.AppInstalledDTO, error) { +func (a *AppInstallService) SearchForWebsite(req request.AppInstalledSearch) ([]response.AppInstalledDTO, error) { var ( installs []model.AppInstall err error @@ -152,7 +173,7 @@ func (a AppInstallService) SearchForWebsite(req request.AppInstalledSearch) ([]r return handleInstalled(installs, false) } -func (a AppInstallService) Operate(req request.AppInstalledOperate) error { +func (a *AppInstallService) Operate(req request.AppInstalledOperate) error { install, err := appInstallRepo.GetFirst(commonRepo.WithByID(req.InstallId)) if err != nil { return err @@ -196,7 +217,7 @@ func (a AppInstallService) Operate(req request.AppInstalledOperate) error { } } -func (a AppInstallService) Update(req request.AppInstalledUpdate) error { +func (a *AppInstallService) Update(req request.AppInstalledUpdate) error { installed, err := appInstallRepo.GetFirst(commonRepo.WithByID(req.InstallId)) if err != nil { return err @@ -270,13 +291,18 @@ func (a AppInstallService) Update(req request.AppInstalledUpdate) error { return nil } -func (a AppInstallService) SyncAll() error { +func (a *AppInstallService) SyncAll(systemInit bool) error { allList, err := appInstallRepo.ListBy() if err != nil { return err } for _, i := range allList { if i.Status == constant.Installing { + if systemInit { + i.Status = constant.Error + i.Message = "System restart causes application exception" + _ = appInstallRepo.Save(&i) + } continue } if err := syncById(i.ID); err != nil { @@ -286,7 +312,7 @@ func (a AppInstallService) SyncAll() error { return nil } -func (a AppInstallService) GetServices(key string) ([]response.AppService, error) { +func (a *AppInstallService) GetServices(key string) ([]response.AppService, error) { app, err := appRepo.GetFirst(appRepo.WithKey(key)) if err != nil { return nil, err @@ -310,7 +336,7 @@ func (a AppInstallService) GetServices(key string) ([]response.AppService, error return res, nil } -func (a AppInstallService) GetUpdateVersions(installId uint) ([]dto.AppVersion, error) { +func (a *AppInstallService) GetUpdateVersions(installId uint) ([]dto.AppVersion, error) { install, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId)) var versions []dto.AppVersion if err != nil { @@ -335,7 +361,7 @@ func (a AppInstallService) GetUpdateVersions(installId uint) ([]dto.AppVersion, return versions, nil } -func (a AppInstallService) ChangeAppPort(req request.PortUpdate) error { +func (a *AppInstallService) ChangeAppPort(req request.PortUpdate) error { if common.ScanPort(int(req.Port)) { return buserr.WithDetail(constant.ErrPortInUsed, req.Port, nil) } @@ -363,7 +389,7 @@ func (a AppInstallService) ChangeAppPort(req request.PortUpdate) error { return nil } -func (a AppInstallService) DeleteCheck(installId uint) ([]dto.AppResource, error) { +func (a *AppInstallService) DeleteCheck(installId uint) ([]dto.AppResource, error) { var res []dto.AppResource appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId)) if err != nil { @@ -404,7 +430,7 @@ func (a AppInstallService) DeleteCheck(installId uint) ([]dto.AppResource, error return res, nil } -func (a AppInstallService) GetDefaultConfigByKey(key string) (string, error) { +func (a *AppInstallService) GetDefaultConfigByKey(key string) (string, error) { appInstall, err := getAppInstallByKey(key) if err != nil { return "", err @@ -426,7 +452,7 @@ func (a AppInstallService) GetDefaultConfigByKey(key string) (string, error) { return string(contentByte), nil } -func (a AppInstallService) GetParams(id uint) ([]response.AppParam, error) { +func (a *AppInstallService) GetParams(id uint) ([]response.AppParam, error) { var ( res []response.AppParam appForm dto.AppForm diff --git a/backend/i18n/i18n.go b/backend/i18n/i18n.go index 7ff4c6acb..f6f76007f 100644 --- a/backend/i18n/i18n.go +++ b/backend/i18n/i18n.go @@ -11,35 +11,35 @@ import ( "gopkg.in/yaml.v3" ) -func GetMsgWithMap(msg string, maps map[string]interface{}) string { +func GetMsgWithMap(key string, maps map[string]interface{}) string { content := "" if maps == nil { content = ginI18n.MustGetMessage(&i18n.LocalizeConfig{ - MessageID: msg, + MessageID: key, }) } else { content = ginI18n.MustGetMessage(&i18n.LocalizeConfig{ - MessageID: msg, + MessageID: key, TemplateData: maps, }) } content = strings.ReplaceAll(content, ": ", "") if content == "" { - return msg + return key } else { return content } } -func GetErrMsg(msg string, maps map[string]interface{}) string { +func GetErrMsg(key string, maps map[string]interface{}) string { content := "" if maps == nil { content = ginI18n.MustGetMessage(&i18n.LocalizeConfig{ - MessageID: msg, + MessageID: key, }) } else { content = ginI18n.MustGetMessage(&i18n.LocalizeConfig{ - MessageID: msg, + MessageID: key, TemplateData: maps, }) } diff --git a/backend/init/business/business.go b/backend/init/business/business.go index 79567ea23..0f33a6f52 100644 --- a/backend/init/business/business.go +++ b/backend/init/business/business.go @@ -6,18 +6,30 @@ import ( ) func Init() { + syncApp() + syncInstalledApp() +} + +func syncApp() { setting, err := service.NewISettingService().GetSettingInfo() if err != nil { global.LOG.Errorf("sync app error: %s", err.Error()) return } if setting.AppStoreVersion != "" { - global.LOG.Info("do not sync") + global.LOG.Info("no need to sync") return } global.LOG.Info("sync app start...") if err := service.NewIAppService().SyncAppList(); err != nil { global.LOG.Errorf("sync app error: %s", err.Error()) + return + } + global.LOG.Info("sync app successful") +} + +func syncInstalledApp() { + if err := service.NewIAppInstalledService().SyncAll(true); err != nil { + global.LOG.Errorf("sync instaled app error: %s", err.Error()) } - global.LOG.Info("sync app success") }