From d60e8167b088c8c64decc95877503ff51d7c637e Mon Sep 17 00:00:00 2001 From: Node <8974108+qwenode@users.noreply.github.com> Date: Tue, 12 Dec 2023 10:36:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20php=E8=BF=90=E8=A1=8C=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=AC=AC=E4=B8=89=E6=96=B9=E5=95=86=E5=BA=97?= =?UTF-8?q?app=20(#3253)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 支持创建自定义php运行环境 #3252 --- backend/app/model/app.go | 17 +++++++++++++++++ backend/app/service/app.go | 21 +++++++++++---------- backend/app/service/app_utils.go | 6 +++++- backend/app/service/runtime.go | 4 +++- backend/app/service/website.go | 2 +- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/backend/app/model/app.go b/backend/app/model/app.go index 5a02eb425..0a9914231 100644 --- a/backend/app/model/app.go +++ b/backend/app/model/app.go @@ -1,5 +1,11 @@ package model +import ( + "github.com/1Panel-dev/1Panel/backend/constant" + "path/filepath" + "strings" +) + type App struct { BaseModel Name string `json:"name" gorm:"type:varchar(64);not null"` @@ -24,3 +30,14 @@ type App struct { TagsKey []string `json:"tags" yaml:"tags" gorm:"-"` AppTags []AppTag `json:"-" gorm:"-:migration"` } + +func (i *App) IsLocalApp() bool { + return i.Resource == constant.ResourceLocal +} +func (i *App) GetAppResourcePath() string { + if i.IsLocalApp() { + //这里要去掉本地应用的local前缀 + return filepath.Join(constant.LocalAppResourceDir, strings.TrimPrefix(i.Key, "local")) + } + return filepath.Join(constant.RemoteAppResourceDir, i.Key) +} diff --git a/backend/app/service/app.go b/backend/app/service/app.go index a4cb2c8ba..f069db890 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -8,7 +8,7 @@ import ( "io" "net/http" "os" - "path" + "path/filepath" "reflect" "strconv" "strings" @@ -173,7 +173,8 @@ func (a AppService) GetAppDetail(appID uint, version, appType string) (response. return appDetailDTO, err } fileOp := files.NewFileOp() - versionPath := path.Join(constant.AppResourceDir, app.Resource, app.Key, detail.Version) + + versionPath := filepath.Join(app.GetAppResourcePath(), detail.Version) if !fileOp.Stat(versionPath) || detail.Update { if err = downloadApp(app, detail, nil); err != nil { return appDetailDTO, err @@ -181,8 +182,8 @@ func (a AppService) GetAppDetail(appID uint, version, appType string) (response. } switch app.Type { case constant.RuntimePHP: - buildPath := path.Join(versionPath, "build") - paramsPath := path.Join(buildPath, "config.json") + buildPath := filepath.Join(versionPath, "build") + paramsPath := filepath.Join(buildPath, "config.json") if !fileOp.Stat(paramsPath) { return appDetailDTO, buserr.New(constant.ErrFileNotExist) } @@ -195,7 +196,7 @@ func (a AppService) GetAppDetail(appID uint, version, appType string) (response. return appDetailDTO, err } appDetailDTO.Params = paramMap - composePath := path.Join(buildPath, "docker-compose.yml") + composePath := filepath.Join(buildPath, "docker-compose.yml") if !fileOp.Stat(composePath) { return appDetailDTO, buserr.New(constant.ErrFileNotExist) } @@ -223,7 +224,7 @@ func (a AppService) GetAppDetail(appID uint, version, appType string) (response. } if appDetailDTO.DockerCompose == "" { - filename := path.Base(appDetailDTO.DownloadUrl) + filename := filepath.Base(appDetailDTO.DownloadUrl) dockerComposeUrl := fmt.Sprintf("%s%s", strings.TrimSuffix(appDetailDTO.DownloadUrl, filename), "docker-compose.yml") composeRes, err := http.Get(dockerComposeUrl) if err != nil { @@ -489,7 +490,7 @@ func (a AppService) SyncAppListFromLocal() { } for _, dirEntry := range dirEntries { if dirEntry.IsDir() { - appDir := path.Join(localAppDir, dirEntry.Name()) + appDir := filepath.Join(localAppDir, dirEntry.Name()) appDirEntries, err := os.ReadDir(appDir) if err != nil { global.LOG.Errorf(i18n.GetMsgWithMap("ErrAppDirNull", map[string]interface{}{"name": dirEntry.Name(), "err": err.Error()})) @@ -507,7 +508,7 @@ func (a AppService) SyncAppListFromLocal() { Version: appDirEntry.Name(), Status: constant.AppNormal, } - versionDir := path.Join(appDir, appDirEntry.Name()) + versionDir := filepath.Join(appDir, appDirEntry.Name()) if err = handleLocalAppDetail(versionDir, &appDetail); err != nil { global.LOG.Errorf(i18n.GetMsgWithMap("LocalAppVersionErr", map[string]interface{}{"name": app.Name, "version": appDetail.Version, "err": err.Error()})) continue @@ -742,7 +743,7 @@ func getAppFromRepo(downloadPath string) error { downloadUrl := downloadPath global.LOG.Infof("[AppStore] download file from %s", downloadUrl) fileOp := files.NewFileOp() - packagePath := path.Join(constant.ResourceDir, path.Base(downloadUrl)) + packagePath := filepath.Join(constant.ResourceDir, filepath.Base(downloadUrl)) if err := fileOp.DownloadFile(downloadUrl, packagePath); err != nil { return err } @@ -760,7 +761,7 @@ func getAppList() (*dto.AppList, error) { if err := getAppFromRepo(fmt.Sprintf("%s/%s/1panel.json.zip", global.CONF.System.AppRepo, global.CONF.System.Mode)); err != nil { return nil, err } - listFile := path.Join(constant.ResourceDir, "1panel.json") + listFile := filepath.Join(constant.ResourceDir, "1panel.json") content, err := os.ReadFile(listFile) if err != nil { return nil, err diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index e135a09ce..ebdc096fd 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -637,8 +637,12 @@ func handleMap(params map[string]interface{}, envParams map[string]string) { } func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.AppInstall) (err error) { + if app.IsLocalApp() { + //本地应用,不去官网下载 + return nil + } appResourceDir := path.Join(constant.AppResourceDir, app.Resource) - appDownloadDir := path.Join(appResourceDir, app.Key) + appDownloadDir := app.GetAppResourcePath() appVersionDir := path.Join(appDownloadDir, appDetail.Version) fileOp := files.NewFileOp() if !appDetail.Update && fileOp.Stat(appVersionDir) { diff --git a/backend/app/service/runtime.go b/backend/app/service/runtime.go index 248bbf4f4..f9ea07536 100644 --- a/backend/app/service/runtime.go +++ b/backend/app/service/runtime.go @@ -21,6 +21,7 @@ import ( "github.com/subosito/gotenv" "os" "path" + "path/filepath" "regexp" "strconv" "strings" @@ -107,7 +108,8 @@ func (r *RuntimeService) Create(create request.RuntimeCreate) (err error) { if err != nil { return err } - appVersionDir := path.Join(constant.AppResourceDir, app.Resource, app.Key, appDetail.Version) + + appVersionDir := filepath.Join(app.GetAppResourcePath(), appDetail.Version) if !fileOp.Stat(appVersionDir) || appDetail.Update { if err := downloadApp(app, appDetail, nil); err != nil { return err diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 5ad8f8c02..6e9604385 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -1210,7 +1210,7 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) error if err != nil { return err } - oldRuntime, err := runtimeRepo.GetFirst(commonRepo.WithByID(req.RuntimeID)) + oldRuntime, err := runtimeRepo.GetFirst(commonRepo.WithByID(website.RuntimeID)) if err != nil { return err }