2022-10-28 17:04:57 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-11-02 15:19:14 +08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gorm.io/gorm"
|
2022-10-28 17:04:57 +08:00
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WebsiteService struct {
|
|
|
|
}
|
|
|
|
|
2022-11-02 15:19:14 +08:00
|
|
|
func (w WebsiteService) PageWebSite(req dto.WebSiteReq) (int64, []dto.WebSiteDTO, error) {
|
|
|
|
var websiteDTOs []dto.WebSiteDTO
|
|
|
|
total, websites, err := websiteRepo.Page(req.Page, req.PageSize)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
for _, web := range websites {
|
|
|
|
websiteDTOs = append(websiteDTOs, dto.WebSiteDTO{
|
|
|
|
WebSite: web,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return total, websiteDTOs, nil
|
|
|
|
}
|
2022-10-28 17:04:57 +08:00
|
|
|
|
2022-11-02 15:19:14 +08:00
|
|
|
func (w WebsiteService) CreateWebsite(create dto.WebSiteCreate) error {
|
2022-10-28 17:04:57 +08:00
|
|
|
defaultDate, _ := time.Parse(constant.DateLayout, constant.DefaultDate)
|
|
|
|
website := &model.WebSite{
|
|
|
|
PrimaryDomain: create.PrimaryDomain,
|
|
|
|
Type: create.Type,
|
|
|
|
Alias: create.Alias,
|
|
|
|
Remark: create.Remark,
|
|
|
|
Status: constant.WebRunning,
|
|
|
|
ExpireDate: defaultDate,
|
|
|
|
AppInstallID: create.AppInstallID,
|
|
|
|
WebSiteGroupID: create.WebSiteGroupID,
|
|
|
|
}
|
|
|
|
|
2022-11-02 15:19:14 +08:00
|
|
|
if create.AppType == dto.NewApp {
|
|
|
|
install, err := ServiceGroupApp.Install(create.AppInstall.Name, create.AppInstall.AppDetailId, create.AppInstall.Params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
website.AppInstallID = install.ID
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:04:57 +08:00
|
|
|
tx, ctx := getTxAndContext()
|
|
|
|
if err := websiteRepo.Create(ctx, website); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var domains []model.WebSiteDomain
|
|
|
|
domains = append(domains, model.WebSiteDomain{Domain: website.PrimaryDomain, WebSiteID: website.ID, Port: 80})
|
|
|
|
for _, domain := range create.Domains {
|
|
|
|
domainModel, err := getDomain(domain, website.ID)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if reflect.DeepEqual(domainModel, model.WebSiteDomain{}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
domains = append(domains, domainModel)
|
|
|
|
}
|
|
|
|
if len(domains) > 0 {
|
|
|
|
if err := websiteDomainRepo.BatchCreate(ctx, domains); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 15:19:14 +08:00
|
|
|
if err := configDefaultNginx(website, domains); err != nil {
|
2022-10-28 17:04:57 +08:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-02 15:19:14 +08:00
|
|
|
|
|
|
|
func (w WebsiteService) DeleteWebSite(req dto.WebSiteDel) error {
|
|
|
|
|
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := delNginxConfig(website); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx, ctx := getTxAndContext()
|
|
|
|
|
|
|
|
if req.DeleteApp {
|
|
|
|
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(model.AppInstall{}, appInstall) {
|
|
|
|
if err := deleteAppInstall(ctx, appInstall); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//TODO 删除备份
|
|
|
|
if err := websiteRepo.DeleteBy(ctx, commonRepo.WithByID(req.ID)); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := websiteDomainRepo.DeleteBy(ctx, websiteDomainRepo.WithWebSiteId(req.ID)); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
|
|
|
}
|