From cfa5f08cddee818ed4c603b4b15878eefff852dd Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Thu, 3 Nov 2022 18:02:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/website.go | 14 ++++ backend/app/dto/website.go | 20 +++-- backend/app/service/website.go | 22 +++++- backend/app/service/website_utils.go | 53 ++++++++++--- backend/router/ro_website.go | 1 + backend/utils/nginx/components/server.go | 7 ++ frontend/src/api/interface/website.ts | 9 ++- frontend/src/api/modules/website.ts | 4 + .../config/basic/domain/create/index.vue | 77 +++++++++++++++++++ .../project/config/basic/domain/index.vue | 11 ++- 10 files changed, 199 insertions(+), 19 deletions(-) create mode 100644 frontend/src/views/website/project/config/basic/domain/create/index.vue diff --git a/backend/app/api/v1/website.go b/backend/app/api/v1/website.go index a9a65f63e..e43f32b12 100644 --- a/backend/app/api/v1/website.go +++ b/backend/app/api/v1/website.go @@ -82,3 +82,17 @@ func (b *BaseApi) DeleteWebDomain(c *gin.Context) { } helper.SuccessWithData(c, nil) } + +func (b *BaseApi) CreateWebDomain(c *gin.Context) { + var req dto.WebSiteDomainCreate + if err := c.ShouldBindJSON(&req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } + domain, err := websiteService.CreateWebsiteDomain(req) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, domain) +} diff --git a/backend/app/dto/website.go b/backend/app/dto/website.go index c3fd83dd5..d0db5e735 100644 --- a/backend/app/dto/website.go +++ b/backend/app/dto/website.go @@ -1,6 +1,9 @@ package dto -import "github.com/1Panel-dev/1Panel/backend/app/model" +import ( + "github.com/1Panel-dev/1Panel/backend/app/model" + "github.com/1Panel-dev/1Panel/backend/utils/nginx/components" +) type WebSiteReq struct { PageInfo @@ -43,16 +46,23 @@ type WebSiteDTO struct { } type WebSiteGroupCreate struct { - Name string + Name string `json:"name"` } type WebSiteGroupUpdate struct { - ID uint - Name string + ID uint `json:"id"` + Name string `json:"name"` } type WebSiteDomainCreate struct { + WebSiteID uint `json:"webSiteId"` + Port int `json:"port"` + Domain string `json:"domain"` } -type WebSiteDomainDel struct { +type NginxConfig struct { + FilePath string `json:"filePath"` + ContainerName string `json:"containerName"` + Config *components.Config `json:"config"` + OldContent string `json:"oldContent"` } diff --git a/backend/app/service/website.go b/backend/app/service/website.go index e3d228c66..79694d9b7 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -126,8 +126,28 @@ func (w WebsiteService) DeleteWebSite(req dto.WebSiteDel) error { return nil } -func (w WebsiteService) CreateWebsiteDomain() { +func (w WebsiteService) CreateWebsiteDomain(create dto.WebSiteDomainCreate) (model.WebSiteDomain, error) { + var domainModel model.WebSiteDomain + var ports []int + var domains []string + 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) } func (w WebsiteService) GetWebsiteDomain(websiteId uint) ([]model.WebSiteDomain, error) { diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go index a4479aa50..417ee3b97 100644 --- a/backend/app/service/website_utils.go +++ b/backend/app/service/website_utils.go @@ -2,6 +2,7 @@ package service import ( "fmt" + "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/utils/cmd" @@ -137,23 +138,59 @@ func nginxCheckAndReload(oldContent string, filePath string, containerName strin return nil } -func deleteListenAndServerName(website model.WebSite, ports []int, domains []string) error { - +func getNginxConfig(primaryDomain string) (dto.NginxConfig, error) { + var nginxConfig dto.NginxConfig nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx")) if err != nil { - return err + return nginxConfig, err } nginxInstall, err := appInstallRepo.GetFirst(appInstallRepo.WithAppId(nginxApp.ID)) if err != nil { - return err + return nginxConfig, err } - configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", website.PrimaryDomain+".conf") + configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", primaryDomain+".conf") content, err := os.ReadFile(configPath) if err != nil { - return err + return nginxConfig, err } config := parser.NewStringParser(string(content)).Parse() + config.FilePath = configPath + nginxConfig.Config = config + nginxConfig.OldContent = string(content) + nginxConfig.ContainerName = nginxInstall.ContainerName + nginxConfig.FilePath = configPath + + return nginxConfig, nil +} + +func addListenAndServerName(website model.WebSite, ports []int, domains []string) error { + + nginxConfig, err := getNginxConfig(website.PrimaryDomain) + if err != nil { + return nil + } + config := nginxConfig.Config + server := config.FindServers()[0] + for _, port := range ports { + server.AddListen(strconv.Itoa(port), false) + } + for _, domain := range domains { + server.AddServerName(domain) + } + if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil { + return err + } + return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName) +} + +func deleteListenAndServerName(website model.WebSite, ports []int, domains []string) error { + + nginxConfig, err := getNginxConfig(website.PrimaryDomain) + if err != nil { + return nil + } + config := nginxConfig.Config server := config.FindServers()[0] for _, port := range ports { server.DeleteListen(strconv.Itoa(port)) @@ -162,10 +199,8 @@ func deleteListenAndServerName(website model.WebSite, ports []int, domains []str server.DeleteServerName(domain) } - config.FilePath = configPath if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil { return err } - return nginxCheckAndReload(string(content), configPath, nginxInstall.ContainerName) - + return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName) } diff --git a/backend/router/ro_website.go b/backend/router/ro_website.go index 4272f5c96..24ef61f63 100644 --- a/backend/router/ro_website.go +++ b/backend/router/ro_website.go @@ -20,5 +20,6 @@ func (a *WebsiteRouter) InitWebsiteRouter(Router *gin.RouterGroup) { groupRouter.POST("/del", baseApi.DeleteWebSite) groupRouter.GET("/domains/:websiteId", baseApi.GetWebDomains) groupRouter.DELETE("/domains/:id", baseApi.DeleteWebDomain) + groupRouter.POST("/domains", baseApi.CreateWebDomain) } } diff --git a/backend/utils/nginx/components/server.go b/backend/utils/nginx/components/server.go index 568ce44d4..98af79ccf 100644 --- a/backend/utils/nginx/components/server.go +++ b/backend/utils/nginx/components/server.go @@ -112,6 +112,13 @@ func (s *Server) DeleteServerName(name string) { s.UpdateServerName(names) } +func (s *Server) AddServerName(name string) { + dirs := s.FindDirectives("server_name") + params := dirs[0].GetParameters() + params = append(params, name) + s.UpdateServerName(params) +} + func (s *Server) UpdateServerName(names []string) { serverNameDirective := Directive{ Name: "server_name", diff --git a/frontend/src/api/interface/website.ts b/frontend/src/api/interface/website.ts index 15e14cc77..565fccdf1 100644 --- a/frontend/src/api/interface/website.ts +++ b/frontend/src/api/interface/website.ts @@ -52,8 +52,15 @@ export namespace WebSite { } export interface Domain { - name: string; + websiteId: number; port: number; id: number; + domain: string; + } + + export interface DomainCreate { + websiteId: number; + port: number; + domain: string; } } diff --git a/frontend/src/api/modules/website.ts b/frontend/src/api/modules/website.ts index b19d4a946..967d882de 100644 --- a/frontend/src/api/modules/website.ts +++ b/frontend/src/api/modules/website.ts @@ -37,3 +37,7 @@ export const ListDomains = (id: number) => { export const DeleteDomain = (id: number) => { return http.delete(`/websites/domains/${id}`); }; + +export const CreateDomain = (req: WebSite.DomainCreate) => { + return http.post(`/websites/domains`, req); +}; diff --git a/frontend/src/views/website/project/config/basic/domain/create/index.vue b/frontend/src/views/website/project/config/basic/domain/create/index.vue new file mode 100644 index 000000000..657c98423 --- /dev/null +++ b/frontend/src/views/website/project/config/basic/domain/create/index.vue @@ -0,0 +1,77 @@ + + + diff --git a/frontend/src/views/website/project/config/basic/domain/index.vue b/frontend/src/views/website/project/config/basic/domain/index.vue index ca8fd69b5..63c13af1a 100644 --- a/frontend/src/views/website/project/config/basic/domain/index.vue +++ b/frontend/src/views/website/project/config/basic/domain/index.vue @@ -1,16 +1,18 @@