1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-19 16:29:17 +08:00
1Panel/backend/app/service/website.go

168 lines
4.6 KiB
Go
Raw Normal View History

2022-10-28 17:04:57 +08:00
package service
import (
2022-11-03 17:06:48 +08:00
"context"
"fmt"
2022-10-28 17:04:57 +08:00
"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"
2022-11-03 17:06:48 +08:00
"strings"
2022-10-28 17:04:57 +08:00
"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-11-03 17:06:48 +08:00
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})
2022-11-03 17:06:48 +08:00
otherDomainArray := strings.Split(create.OtherDomains, "\n")
for _, domain := range otherDomainArray {
2022-10-28 17:04:57 +08:00
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 {
2022-11-03 17:06:48 +08:00
websites, _ := websiteRepo.GetBy(websiteRepo.WithAppInstallId(website.AppInstallID))
if len(websites) > 1 {
return errors.New("other website use this app")
}
2022-11-02 15:19:14 +08:00
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
}
2022-11-03 17:06:48 +08:00
func (w WebsiteService) CreateWebsiteDomain() {
}
func (w WebsiteService) GetWebsiteDomain(websiteId uint) ([]model.WebSiteDomain, error) {
return websiteDomainRepo.GetBy(websiteDomainRepo.WithWebSiteId(websiteId))
}
func (w WebsiteService) DeleteWebsiteDomain(domainId uint) error {
webSiteDomain, err := websiteDomainRepo.GetFirst(commonRepo.WithByID(domainId))
if err != nil {
return err
}
if websiteDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebSiteId(webSiteDomain.WebSiteID)); len(websiteDomains) == 1 {
return fmt.Errorf("can not delete last domain")
}
website, err := websiteRepo.GetFirst(commonRepo.WithByID(webSiteDomain.WebSiteID))
if err != nil {
return err
}
var ports []int
if oldDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebSiteId(webSiteDomain.WebSiteID), websiteDomainRepo.WithPort(webSiteDomain.Port)); len(oldDomains) == 1 {
ports = append(ports, webSiteDomain.Port)
}
var domains []string
if oldDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebSiteId(webSiteDomain.WebSiteID), websiteDomainRepo.WithDomain(webSiteDomain.Domain)); len(oldDomains) == 1 {
domains = append(domains, webSiteDomain.Domain)
}
if len(ports) > 0 || len(domains) > 0 {
if err := deleteListenAndServerName(website, ports, domains); err != nil {
return err
}
}
return websiteDomainRepo.DeleteBy(context.TODO(), commonRepo.WithByID(domainId))
}