1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-31 14:08:06 +08:00

style: 创建应用增加端口判断

This commit is contained in:
zhengkunwang223 2023-02-07 16:29:54 +08:00 committed by zhengkunwang223
parent ba5d6794b1
commit 24b3d21204
11 changed files with 68 additions and 23 deletions

View File

@ -47,13 +47,19 @@ func (a *AppInstallRepo) WithServiceName(serviceName string) DBOption {
} }
} }
func (a *AppInstallRepo) WithPort(port int) DBOption {
return func(db *gorm.DB) *gorm.DB {
return db.Where("https_port = ? or http_port = ?", port, port)
}
}
func (a *AppInstallRepo) WithIdNotInWebsite() DBOption { func (a *AppInstallRepo) WithIdNotInWebsite() DBOption {
return func(db *gorm.DB) *gorm.DB { return func(db *gorm.DB) *gorm.DB {
return db.Where("id not in (select app_install_id from websites)") return db.Where("id not in (select app_install_id from websites)")
} }
} }
func (a *AppInstallRepo) GetBy(opts ...DBOption) ([]model.AppInstall, error) { func (a *AppInstallRepo) ListBy(opts ...DBOption) ([]model.AppInstall, error) {
var install []model.AppInstall var install []model.AppInstall
db := getDb(opts...).Model(&model.AppInstall{}) db := getDb(opts...).Model(&model.AppInstall{})
err := db.Preload("App").Preload("Backups").Find(&install).Error err := db.Preload("App").Preload("Backups").Find(&install).Error

View File

@ -155,7 +155,7 @@ func (a AppService) GetAppDetail(appId uint, version string) (response.AppDetail
} }
func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (*model.AppInstall, error) { func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (*model.AppInstall, error) {
if list, _ := appInstallRepo.GetBy(commonRepo.WithByName(req.Name)); len(list) > 0 { if list, _ := appInstallRepo.ListBy(commonRepo.WithByName(req.Name)); len(list) > 0 {
return nil, buserr.New(constant.ErrNameIsExist) return nil, buserr.New(constant.ErrNameIsExist)
} }

View File

@ -137,12 +137,12 @@ func (a AppInstallService) SearchForWebsite(req request.AppInstalledSearch) ([]r
opts = append(opts, appInstallRepo.WithIdNotInWebsite()) opts = append(opts, appInstallRepo.WithIdNotInWebsite())
} }
opts = append(opts, appInstallRepo.WithAppIdsIn(ids)) opts = append(opts, appInstallRepo.WithAppIdsIn(ids))
installs, err = appInstallRepo.GetBy(opts...) installs, err = appInstallRepo.ListBy(opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else { } else {
installs, err = appInstallRepo.GetBy() installs, err = appInstallRepo.ListBy()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -178,7 +178,7 @@ func (a AppInstallService) Operate(req request.AppInstalledOperate) error {
install.Status = constant.Running install.Status = constant.Running
case constant.Delete: case constant.Delete:
tx, ctx := getTxAndContext() tx, ctx := getTxAndContext()
if err := deleteAppInstall(ctx, install, req.DeleteBackup); err != nil && !req.ForceDelete { if err := deleteAppInstall(ctx, install, req.DeleteBackup, req.ForceDelete); err != nil && !req.ForceDelete {
tx.Rollback() tx.Rollback()
return err return err
} }
@ -206,7 +206,7 @@ func (a AppInstallService) Operate(req request.AppInstalledOperate) error {
} }
func (a AppInstallService) SyncAll() error { func (a AppInstallService) SyncAll() error {
allList, err := appInstallRepo.GetBy() allList, err := appInstallRepo.ListBy()
if err != nil { if err != nil {
return err return err
} }
@ -253,7 +253,7 @@ func (a AppInstallService) GetServices(key string) ([]response.AppService, error
if err != nil { if err != nil {
return nil, err return nil, err
} }
installs, err := appInstallRepo.GetBy(appInstallRepo.WithAppId(app.ID), appInstallRepo.WithStatus(constant.Running)) installs, err := appInstallRepo.ListBy(appInstallRepo.WithAppId(app.ID), appInstallRepo.WithStatus(constant.Running))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -40,8 +40,17 @@ func checkPort(key string, params map[string]interface{}) (int, error) {
port, ok := params[key] port, ok := params[key]
if ok { if ok {
portN := int(math.Ceil(port.(float64))) portN := int(math.Ceil(port.(float64)))
oldInstalled, _ := appInstallRepo.ListBy(appInstallRepo.WithPort(portN))
if len(oldInstalled) > 0 {
var apps []string
for _, install := range oldInstalled {
apps = append(apps, install.App.Name)
}
return portN, buserr.WithMap(constant.ErrPortInOtherApp, map[string]interface{}{"port": portN, "apps": apps}, nil)
}
if common.ScanPort(portN) { if common.ScanPort(portN) {
return portN, buserr.WithMessage(constant.ErrPortInUsed, portN, nil) return portN, buserr.WithDetail(constant.ErrPortInUsed, portN, nil)
} else { } else {
return portN, nil return portN, nil
} }
@ -105,23 +114,23 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall
return nil return nil
} }
func deleteAppInstall(ctx context.Context, install model.AppInstall, deleteBackup bool) error { func deleteAppInstall(ctx context.Context, install model.AppInstall, deleteBackup bool, forceDelete bool) error {
op := files.NewFileOp() op := files.NewFileOp()
appDir := install.GetPath() appDir := install.GetPath()
dir, _ := os.Stat(appDir) dir, _ := os.Stat(appDir)
if dir != nil { if dir != nil {
out, err := compose.Down(install.GetComposePath()) out, err := compose.Down(install.GetComposePath())
if err != nil { if err != nil && !forceDelete {
return handleErr(install, err, out) return handleErr(install, err, out)
} }
if err := op.DeleteDir(appDir); err != nil { if err := op.DeleteDir(appDir); err != nil && !forceDelete {
return err return err
} }
} }
if err := appInstallRepo.Delete(ctx, install); err != nil { if err := appInstallRepo.Delete(ctx, install); err != nil && !forceDelete {
return err return err
} }
if err := deleteLink(ctx, &install); err != nil { if err := deleteLink(ctx, &install); err != nil && !forceDelete {
return err return err
} }
if deleteBackup { if deleteBackup {
@ -129,7 +138,7 @@ func deleteAppInstall(ctx context.Context, install model.AppInstall, deleteBacku
for _, backup := range backups { for _, backup := range backups {
_ = op.DeleteDir(backup.Path) _ = op.DeleteDir(backup.Path)
} }
if err := appInstallBackupRepo.Delete(ctx, appInstallBackupRepo.WithAppInstallID(install.ID)); err != nil { if err := appInstallBackupRepo.Delete(ctx, appInstallBackupRepo.WithAppInstallID(install.ID)); err != nil && !forceDelete {
return err return err
} }
} }
@ -320,7 +329,7 @@ func getContainerNames(install model.AppInstall) ([]string, error) {
func checkLimit(app model.App) error { func checkLimit(app model.App) error {
if app.Limit > 0 { if app.Limit > 0 {
installs, err := appInstallRepo.GetBy(appInstallRepo.WithAppId(app.ID)) installs, err := appInstallRepo.ListBy(appInstallRepo.WithAppId(app.ID))
if err != nil { if err != nil {
return err return err
} }
@ -361,7 +370,7 @@ func checkRequiredAndLimit(app model.App) error {
_, err = appInstallRepo.GetFirst(appInstallRepo.WithDetailIdsIn(detailIds)) _, err = appInstallRepo.GetFirst(appInstallRepo.WithDetailIdsIn(detailIds))
if err != nil { if err != nil {
return buserr.WithMessage(constant.ErrAppRequired, requireApp.Name, nil) return buserr.WithDetail(constant.ErrAppRequired, requireApp.Name, nil)
} }
} }
} }

View File

@ -60,7 +60,7 @@ func (u *DashboardService) LoadBaseInfo(ioOption string, netOption string) (*dto
} }
} }
appInstall, err := appInstallRepo.GetBy() appInstall, err := appInstallRepo.ListBy()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -334,7 +334,7 @@ func (w WebsiteService) DeleteWebsite(req request.WebsiteDelete) error {
return err return err
} }
if !reflect.DeepEqual(model.AppInstall{}, appInstall) { if !reflect.DeepEqual(model.AppInstall{}, appInstall) {
if err := deleteAppInstall(ctx, appInstall, true); err != nil && !req.ForceDelete { if err := deleteAppInstall(ctx, appInstall, true, req.ForceDelete); err != nil && !req.ForceDelete {
return err return err
} }
} }
@ -638,7 +638,7 @@ func (w WebsiteService) PreInstallCheck(req request.WebsiteInstallCheckReq) ([]r
res = append(res, response.WebsitePreInstallCheck{ res = append(res, response.WebsitePreInstallCheck{
Name: appInstall.Name, Name: appInstall.Name,
AppName: app.Name, AppName: app.Name,
Status: buserr.WithMessage(constant.ErrNotInstall, app.Name, nil).Error(), Status: buserr.WithDetail(constant.ErrNotInstall, app.Name, nil).Error(),
Version: appInstall.Version, Version: appInstall.Version,
}) })
showErr = true showErr = true
@ -651,7 +651,7 @@ func (w WebsiteService) PreInstallCheck(req request.WebsiteInstallCheckReq) ([]r
} }
} }
if len(checkIds) > 0 { if len(checkIds) > 0 {
installList, _ := appInstallRepo.GetBy(commonRepo.WithIdsIn(checkIds)) installList, _ := appInstallRepo.ListBy(commonRepo.WithIdsIn(checkIds))
for _, install := range installList { for _, install := range installList {
res = append(res, response.WebsitePreInstallCheck{ res = append(res, response.WebsitePreInstallCheck{
Name: install.Name, Name: install.Name,

View File

@ -8,6 +8,7 @@ import (
type BusinessError struct { type BusinessError struct {
Msg string Msg string
Detail interface{} Detail interface{}
Map map[string]interface{}
Err error Err error
} }
@ -15,6 +16,8 @@ func (e BusinessError) Error() string {
content := "" content := ""
if e.Detail != nil { if e.Detail != nil {
content = i18n.GetErrMsg(e.Msg, map[string]interface{}{"detail": e.Detail}) content = i18n.GetErrMsg(e.Msg, map[string]interface{}{"detail": e.Detail})
} else if e.Map != nil {
content = i18n.GetErrMsg(e.Msg, e.Map)
} else { } else {
content = i18n.GetErrMsg(e.Msg, nil) content = i18n.GetErrMsg(e.Msg, nil)
} }
@ -35,10 +38,18 @@ func New(Key string) BusinessError {
} }
} }
func WithMessage(Key string, detail interface{}, err error) BusinessError { func WithDetail(Key string, detail interface{}, err error) BusinessError {
return BusinessError{ return BusinessError{
Msg: Key, Msg: Key,
Detail: detail, Detail: detail,
Err: err, Err: err,
} }
} }
func WithMap(Key string, maps map[string]interface{}, err error) BusinessError {
return BusinessError{
Msg: Key,
Map: maps,
Err: err,
}
}

View File

@ -54,6 +54,7 @@ var (
ErrFileCanNotRead = "ErrFileCanNotRead" ErrFileCanNotRead = "ErrFileCanNotRead"
ErrFileToLarge = "ErrFileToLarge" ErrFileToLarge = "ErrFileToLarge"
ErrNotInstall = "ErrNotInstall" ErrNotInstall = "ErrNotInstall"
ErrPortInOtherApp = "ErrPortInOtherApp"
) )
//website //website

View File

@ -21,6 +21,7 @@ ErrPortInUsed: "{{ .detail }} port already in use"
ErrAppLimit: "App exceeds install limit" ErrAppLimit: "App exceeds install limit"
ErrAppRequired: "{{ .detail }} app is required" ErrAppRequired: "{{ .detail }} app is required"
ErrNotInstall: "App not installed" ErrNotInstall: "App not installed"
ErrPortInOtherApp: "{{ .port }} port already in use by {{ .apps }}"
#file #file

View File

@ -21,6 +21,7 @@ ErrPortInUsed: "{{ .detail }} 端口已被占用!"
ErrAppLimit: "应用超出安装数量限制" ErrAppLimit: "应用超出安装数量限制"
ErrAppRequired: "请先安装 {{ .detail }} 应用" ErrAppRequired: "请先安装 {{ .detail }} 应用"
ErrNotInstall: "应用未安装" ErrNotInstall: "应用未安装"
ErrPortInOtherApp: "{{ .port }} 端口已被 {{ .apps }}占用!"
#file #file
ErrFileCanNotRead: "此文件不支持预览" ErrFileCanNotRead: "此文件不支持预览"

View File

@ -1,6 +1,6 @@
<template> <template>
<div v-for="(p, index) in paramObjs" :key="index"> <div v-for="(p, index) in paramObjs" :key="index">
<el-form-item :label="p.labelZh" :prop="p.prop"> <el-form-item :label="getLabel(p)" :prop="p.prop">
<el-input <el-input
v-model.trim="form[p.envKey]" v-model.trim="form[p.envKey]"
v-if="p.type == 'text'" v-if="p.type == 'text'"
@ -43,12 +43,13 @@
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'; import { computed, inject, onMounted, reactive, ref } from 'vue';
import { getRandomStr } from '@/utils/util'; import { getRandomStr } from '@/utils/util';
import { GetAppService } from '@/api/modules/app'; import { GetAppService } from '@/api/modules/app';
import { Rules } from '@/global/form-rules'; import { Rules } from '@/global/form-rules';
import { App } from '@/api/interface/app'; import { App } from '@/api/interface/app';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
const router = useRouter(); const router = useRouter();
interface ParamObj extends App.FromField { interface ParamObj extends App.FromField {
@ -169,8 +170,23 @@ const changeService = (value: string, services: App.AppService[]) => {
updateParam(); updateParam();
}; };
const getLabel = (row: ParamObj): string => {
const language = useI18n().locale.value;
if (language == 'zh') {
return row.labelZh;
} else {
return row.labelEn;
}
};
let reloadPage: Function = inject('reload');
const toPage = () => { const toPage = () => {
router.push({ name: 'App' }); router.push({ name: 'App' });
const nowPath = router.currentRoute.value.path;
if (nowPath && nowPath == '/apps/all') {
reloadPage();
}
}; };
onMounted(() => { onMounted(() => {