1Panel/backend/app/service/website.go

242 lines
6.7 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
2022-11-08 17:21:13 +08:00
func (w WebsiteService) UpdateWebsite(req dto.WebSiteUpdate) error {
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.ID))
if err != nil {
return err
}
website.PrimaryDomain = req.PrimaryDomain
website.WebSiteGroupID = req.WebSiteGroupID
website.Remark = req.Remark
return websiteRepo.Save(context.TODO(), &website)
}
2022-11-16 10:31:35 +08:00
func (w WebsiteService) GetWebsite(id uint) (dto.WebsiteDTO, error) {
var res dto.WebsiteDTO
2022-11-08 17:21:13 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(id))
if err != nil {
2022-11-16 10:31:35 +08:00
return res, err
2022-11-08 17:21:13 +08:00
}
2022-11-16 10:31:35 +08:00
res.WebSite = website
return res, nil
2022-11-08 17:21:13 +08:00
}
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
2022-11-03 18:02:07 +08:00
func (w WebsiteService) CreateWebsiteDomain(create dto.WebSiteDomainCreate) (model.WebSiteDomain, error) {
var domainModel model.WebSiteDomain
var ports []int
var domains []string
2022-11-03 17:06:48 +08:00
2022-11-03 18:02:07 +08:00
website, err := websiteRepo.GetFirst(commonRepo.WithByID(create.WebSiteID))
if err != nil {
return domainModel, err
}
if oldDomains, _ := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebSiteId(create.WebSiteID), websiteDomainRepo.WithPort(create.Port)); len(oldDomains) == 0 {
ports = append(ports, create.Port)
}
domains = append(domains, create.Domain)
if err := addListenAndServerName(website, ports, domains); err != nil {
return domainModel, err
}
domainModel = model.WebSiteDomain{
Domain: create.Domain,
Port: create.Port,
WebSiteID: create.WebSiteID,
}
return domainModel, websiteDomainRepo.Create(context.TODO(), &domainModel)
2022-11-03 17:06:48 +08:00
}
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))
}
2022-11-07 16:19:05 +08:00
2022-11-08 15:42:31 +08:00
func (w WebsiteService) GetNginxConfigByScope(req dto.NginxConfigReq) ([]dto.NginxParam, error) {
2022-11-07 16:19:05 +08:00
keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 {
return nil, nil
}
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebSiteID))
if err != nil {
return nil, err
}
return getNginxConfigByKeys(website, keys)
}
func (w WebsiteService) UpdateNginxConfigByScope(req dto.NginxConfigReq) error {
keys, ok := dto.ScopeKeyMap[req.Scope]
if !ok || len(keys) == 0 {
return nil
}
website, err := websiteRepo.GetFirst(commonRepo.WithByID(req.WebSiteID))
if err != nil {
return err
}
2022-11-08 15:42:31 +08:00
if req.Operate == dto.ConfigDel {
return deleteNginxConfig(website, keys)
2022-11-08 15:42:31 +08:00
}
2022-11-07 16:19:05 +08:00
2022-11-08 15:42:31 +08:00
return updateNginxConfig(website, getNginxParams(req.Params, keys), req.Scope)
2022-11-07 16:19:05 +08:00
}