feat: 增加 docker network 初始化

pull/41/head
zhengkunwang223 2022-11-21 11:27:56 +08:00 committed by zhengkunwang223
parent b8473096f8
commit ae4662cf26
12 changed files with 72 additions and 42 deletions

View File

@ -4,6 +4,7 @@ import "time"
type WebSite struct {
BaseModel
Protocol string `gorm:"type:varchar(64);not null" json:"protocol"`
PrimaryDomain string `gorm:"type:varchar(128);not null" json:"primaryDomain"`
Type string `gorm:"type:varchar(64);not null" json:"type"`
Alias string `gorm:"type:varchar(128);not null" json:"alias"`

View File

@ -264,10 +264,7 @@ func backupInstall(ctx context.Context, install model.AppInstall) error {
backup.AppDetailId = install.AppDetailId
backup.Param = install.Param
if err := appInstallBackupRepo.Create(ctx, backup); err != nil {
return err
}
return nil
return appInstallBackupRepo.Create(ctx, backup)
}
func restoreInstall(install model.AppInstall, backupId uint) error {

View File

@ -44,6 +44,7 @@ func (w WebsiteService) CreateWebsite(create dto.WebSiteCreate) error {
ExpireDate: defaultDate,
AppInstallID: create.AppInstallID,
WebSiteGroupID: create.WebSiteGroupID,
Protocol: constant.ProtocolHTTP,
}
if create.AppType == dto.NewApp {
@ -313,9 +314,12 @@ func (w WebsiteService) OpWebsiteHTTPS(req dto.WebsiteHTTPSOp) (dto.WebsiteHTTPS
}
if req.Enable {
website.Protocol = constant.ProtocolHTTPS
if err := applySSL(website, ssl); err != nil {
return dto.WebsiteHTTPS{}, err
}
} else {
website.Protocol = constant.ProtocolHTTP
}
return res, nil

View File

@ -9,3 +9,8 @@ const (
DateLayout = "2006-01-02"
DefaultDate = "1970-01-01"
)
const (
ProtocolHTTP = "HTTP"
ProtocolHTTPS = "HTTPS"
)

47
backend/init/app/app.go Normal file
View File

@ -0,0 +1,47 @@
package app
import (
"fmt"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/docker"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"path"
)
func Init() {
constant.DefaultDataDir = "/opt/1Panel/data"
constant.ResourceDir = path.Join(constant.DefaultDataDir, "resource")
constant.AppResourceDir = path.Join(constant.ResourceDir, "apps")
constant.AppInstallDir = path.Join(constant.DefaultDataDir, "apps")
constant.BackupDir = path.Join(constant.DefaultDataDir, "backup")
constant.AppBackupDir = path.Join(constant.BackupDir, "apps")
dirs := []string{constant.DefaultDataDir, constant.ResourceDir, constant.AppResourceDir, constant.AppInstallDir, constant.BackupDir, constant.AppBackupDir}
fileOp := files.NewFileOp()
for _, dir := range dirs {
createDir(fileOp, dir)
}
createDefaultDockerNetwork()
}
func createDir(fileOp files.FileOp, dirPath string) {
if !fileOp.Stat(dirPath) {
_ = fileOp.CreateDir(dirPath, 0755)
}
}
func createDefaultDockerNetwork() {
cli, err := docker.NewClient()
if err != nil {
fmt.Println("init docker client error", err.Error())
return
}
if !cli.NetworkExist("1panel") {
if err := cli.CreateNetwork("1panel"); err != nil {
fmt.Println("init docker client error", err.Error())
return
}
}
}

View File

@ -3,11 +3,7 @@ package viper
import (
"fmt"
"github.com/1Panel-dev/1Panel/backend/configs"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"path"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
@ -32,27 +28,4 @@ func Init() {
panic(err)
}
global.CONF = serverConfig
InitDir()
}
func InitDir() {
constant.DefaultDataDir = "/opt/1Panel/data"
constant.ResourceDir = path.Join(constant.DefaultDataDir, "resource")
constant.AppResourceDir = path.Join(constant.ResourceDir, "apps")
constant.AppInstallDir = path.Join(constant.DefaultDataDir, "apps")
constant.BackupDir = path.Join(constant.DefaultDataDir, "backup")
constant.AppBackupDir = path.Join(constant.BackupDir, "apps")
dirs := []string{constant.DefaultDataDir, constant.ResourceDir, constant.AppResourceDir, constant.AppInstallDir, constant.BackupDir, constant.AppBackupDir}
fileOp := files.NewFileOp()
for _, dir := range dirs {
createDir(fileOp, dir)
}
}
func createDir(fileOp files.FileOp, dirPath string) {
if !fileOp.Stat(dirPath) {
_ = fileOp.CreateDir(dirPath, 0755)
}
}

View File

@ -3,6 +3,7 @@ package server
import (
"encoding/gob"
"fmt"
"github.com/1Panel-dev/1Panel/backend/init/app"
"time"
"github.com/1Panel-dev/1Panel/backend/cron"
@ -23,6 +24,7 @@ import (
)
func Start() {
app.Init()
viper.Init()
log.Init()
db.Init()

View File

@ -672,14 +672,14 @@ export default {
delete: '',
deleteWarn: ',,?',
syncSuccess: '',
canUpdate: '',
canUpdate: '',
backup: '',
backupName: '',
backupPath: '',
backupdate: '',
restore: '',
restoreWarn: ',,,?',
update: '',
update: '',
versioneSelect: '',
},
website: {
@ -740,5 +740,6 @@ export default {
renewSuccess: '',
config: '',
enableHTTPS: 'HTTPS',
aliasHelper: '',
},
};

View File

@ -73,7 +73,7 @@ const search = (req: WebSite.NginxConfigReq) => {
loading.value = true;
GetNginxConfig(req)
.then((res) => {
if (res.data.length > 0) {
if (res.data && res.data.length > 0) {
const indexParam = res.data[0];
let values = '';
for (const param of indexParam.params) {

View File

@ -108,7 +108,7 @@ const get = () => {
if (res.data) {
form.enable = res.data.enable;
}
if (res.data?.SSL) {
if (res.data?.SSL && res.data?.SSL.id > 0) {
form.websiteSSLId = res.data.SSL.id;
websiteSSL.value = res.data.SSL;
}

View File

@ -64,7 +64,7 @@ const search = (req: WebSite.NginxConfigReq) => {
loading.value = true;
GetNginxConfig(req)
.then((res) => {
if (res.data.length > 0) {
if (res.data && res.data.length > 0) {
enable.value = true;
for (const param of res.data) {
if (param.name === 'limit_conn') {

View File

@ -8,7 +8,7 @@
</el-select>
</el-form-item>
<el-form-item :label="$t('website.group')" prop="webSiteGroupID">
<el-select v-model="website.webSiteGroupID">
<el-select v-model="website.webSiteGroupId">
<el-option
v-for="(group, index) in groups"
:key="index"
@ -33,7 +33,7 @@
:label="$t('website.appInstalled')"
prop="appInstallID"
>
<el-select v-model="website.appInstallID">
<el-select v-model="website.appInstallId">
<el-option
v-for="(appInstall, index) in appInstalles"
:key="index"
@ -94,7 +94,7 @@
></el-input>
</el-form-item>
<el-form-item :label="$t('website.alias')" prop="alias">
<el-input v-model="website.alias"></el-input>
<el-input v-model="website.alias" :placeholder="$t('website.aliasHelper')"></el-input>
</el-form-item>
<el-form-item :label="$t('website.remark')" prop="remark">
<el-input v-model="website.remark"></el-input>
@ -129,8 +129,8 @@ const website = ref({
alias: '',
remark: '',
appType: 'installed',
appInstallID: 0,
webSiteGroupID: 1,
appInstallId: 0,
webSiteGroupId: 1,
otherDomains: '',
appinstall: {
appID: 0,
@ -180,7 +180,7 @@ const searchAppInstalled = () => {
SearchAppInstalled({ type: 'website' }).then((res) => {
appInstalles.value = res.data;
if (res.data.length > 0) {
website.value.appInstallID = res.data[0].id;
website.value.appInstallId = res.data[0].id;
}
});
};