From 8099bcb031917c209341fed95ed278f6839a089d Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Mon, 21 Nov 2022 16:28:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=9D=99=E6=80=81?= =?UTF-8?q?=E7=BD=91=E7=AB=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/nginx/versions/1.23.1/docker-compose.yml | 1 + backend/app/dto/website.go | 2 +- backend/app/service/website.go | 14 +++-- backend/app/service/website_utils.go | 47 +++++++++++++--- backend/utils/nginx/components/server.go | 53 ++++++++++++------- cmd/server/nginx_conf/index.html | 34 ++++++++++++ cmd/server/nginx_conf/nginx_conf.go | 3 ++ cmd/server/nginx_conf/website_default.conf | 5 +- frontend/src/views/app-store/apps/index.vue | 10 ++-- 9 files changed, 130 insertions(+), 39 deletions(-) create mode 100644 cmd/server/nginx_conf/index.html diff --git a/apps/nginx/versions/1.23.1/docker-compose.yml b/apps/nginx/versions/1.23.1/docker-compose.yml index 971fffdcd..94f7ab59c 100644 --- a/apps/nginx/versions/1.23.1/docker-compose.yml +++ b/apps/nginx/versions/1.23.1/docker-compose.yml @@ -10,5 +10,6 @@ services: - ./log:/var/log/nginx - ./conf/conf.d:/etc/nginx/conf.d/ - ./ssl:/etc/nginx/ssl + - ./www:/www/root/ diff --git a/backend/app/dto/website.go b/backend/app/dto/website.go index e93c897aa..8dc7ea4e3 100644 --- a/backend/app/dto/website.go +++ b/backend/app/dto/website.go @@ -21,7 +21,7 @@ type WebSiteCreate struct { Alias string `json:"alias" validate:"required"` Remark string `json:"remark"` OtherDomains string `json:"otherDomains"` - AppType AppType `json:"appType" validate:"required"` + AppType AppType `json:"appType"` AppInstall NewAppInstall `json:"appInstall"` AppID uint `json:"appID"` AppInstallID uint `json:"appInstallID"` diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 910ebc491..343b161b2 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -47,12 +47,18 @@ func (w WebsiteService) CreateWebsite(create dto.WebSiteCreate) error { Protocol: constant.ProtocolHTTP, } - if create.AppType == dto.NewApp { - install, err := ServiceGroupApp.Install(create.AppInstall.Name, create.AppInstall.AppDetailId, create.AppInstall.Params) - if err != nil { + if create.Type == "deployment" { + 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 + } + } else { + if err := createStaticHtml(website); err != nil { return err } - website.AppInstallID = install.ID } tx, ctx := getTxAndContext() diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go index d6637987f..f991a8fc2 100644 --- a/backend/app/service/website_utils.go +++ b/backend/app/service/website_utils.go @@ -43,6 +43,35 @@ func getDomain(domainStr string, websiteID uint) (model.WebSiteDomain, error) { return model.WebSiteDomain{}, nil } +func createStaticHtml(website *model.WebSite) error { + nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx")) + if err != nil { + return err + } + nginxInstall, err := appInstallRepo.GetFirst(appInstallRepo.WithAppId(nginxApp.ID)) + if err != nil { + return err + } + indexFolder := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "www", website.Alias) + indexPath := path.Join(indexFolder, "index.html") + indexContent := string(nginx_conf.Index) + fileOp := files.NewFileOp() + if !fileOp.Stat(indexFolder) { + if err := fileOp.CreateDir(indexFolder, 0755); err != nil { + return err + } + } + if !fileOp.Stat(indexPath) { + if err := fileOp.CreateFile(indexPath); err != nil { + return err + } + } + if err := fileOp.WriteFile(indexPath, strings.NewReader(indexContent), 0755); err != nil { + return err + } + return nil +} + func configDefaultNginx(website *model.WebSite, domains []model.WebSiteDomain) error { nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx")) @@ -53,14 +82,9 @@ func configDefaultNginx(website *model.WebSite, domains []model.WebSiteDomain) e if err != nil { return err } - appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID)) - if err != nil { - return err - } nginxFileName := website.Alias + ".conf" configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", nginxFileName) - nginxContent := string(nginx_conf.WebsiteDefault) config := parser.NewStringParser(nginxContent).Parse() servers := config.FindServers() @@ -74,8 +98,17 @@ func configDefaultNginx(website *model.WebSite, domains []model.WebSiteDomain) e server.UpdateListen(strconv.Itoa(domain.Port), false) } server.UpdateServerName(serverNames) - proxy := fmt.Sprintf("http://127.0.0.1:%d", appInstall.HttpPort) - server.UpdateRootProxy([]string{proxy}) + if website.Type == "deployment" { + appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID)) + if err != nil { + return err + } + proxy := fmt.Sprintf("http://127.0.0.1:%d", appInstall.HttpPort) + server.UpdateRootProxy([]string{proxy}) + } else { + server.UpdateRoot(path.Join("/www/root", website.Alias)) + server.UpdateRootLocation() + } config.FilePath = configPath if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil { diff --git a/backend/utils/nginx/components/server.go b/backend/utils/nginx/components/server.go index b4c23fb84..ae7ef8a24 100644 --- a/backend/utils/nginx/components/server.go +++ b/backend/utils/nginx/components/server.go @@ -127,26 +127,41 @@ func (s *Server) UpdateServerName(names []string) { s.UpdateDirectives("server_name", serverNameDirective) } -func (s *Server) UpdateRootProxy(proxy []string) { - //TODD 根据ID修改 - dirs := s.FindDirectives("location") - for _, dir := range dirs { - location, ok := dir.(*Location) - if ok && location.Match == "/" { - newDir := Directive{ - Name: "location", - Parameters: []string{"/"}, - Block: &Block{}, - } - block := &Block{} - block.Directives = append(block.Directives, &Directive{ - Name: "proxy_pass", - Parameters: proxy, - }) - newDir.Block = block - s.UpdateDirectiveBySecondKey("location", "/", newDir) - } +func (s *Server) UpdateRoot(path string) { + rootDir := Directive{ + Name: "root", + Parameters: []string{path}, } + s.UpdateDirectives("root", rootDir) +} + +func (s *Server) UpdateRootLocation() { + newDir := Directive{ + Name: "location", + Parameters: []string{"/"}, + Block: &Block{}, + } + block := &Block{} + block.Directives = append(block.Directives, &Directive{ + Name: "root", + Parameters: []string{"index.html"}, + }) + newDir.Block = block +} + +func (s *Server) UpdateRootProxy(proxy []string) { + newDir := Directive{ + Name: "location", + Parameters: []string{"/"}, + Block: &Block{}, + } + block := &Block{} + block.Directives = append(block.Directives, &Directive{ + Name: "proxy_pass", + Parameters: proxy, + }) + newDir.Block = block + s.UpdateDirectiveBySecondKey("location", "/", newDir) } func (s *Server) UpdateDirectiveBySecondKey(name string, key string, directive Directive) { diff --git a/cmd/server/nginx_conf/index.html b/cmd/server/nginx_conf/index.html new file mode 100644 index 000000000..b9b07eb54 --- /dev/null +++ b/cmd/server/nginx_conf/index.html @@ -0,0 +1,34 @@ + + + + + 恭喜,站点创建成功! + + + +
+

恭喜, 站点创建成功!

+

这是默认index.html,本页面由系统自动生成

+
+ + \ No newline at end of file diff --git a/cmd/server/nginx_conf/nginx_conf.go b/cmd/server/nginx_conf/nginx_conf.go index 085e873ec..ec674a9d9 100644 --- a/cmd/server/nginx_conf/nginx_conf.go +++ b/cmd/server/nginx_conf/nginx_conf.go @@ -15,3 +15,6 @@ var WebsiteDefault []byte //go:embed limit.conf var Limit []byte + +//go:embed index.html +var Index []byte diff --git a/cmd/server/nginx_conf/website_default.conf b/cmd/server/nginx_conf/website_default.conf index 41b124a84..f7da3940c 100644 --- a/cmd/server/nginx_conf/website_default.conf +++ b/cmd/server/nginx_conf/website_default.conf @@ -2,6 +2,8 @@ server { listen 80; server_name ko.wp-1.com; + index index.php index.html index.htm default.php default.htm default.html; + proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name; @@ -10,8 +12,5 @@ server { proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; - location / { - proxy_pass http://127.0.0.1:8080; - } access_log /var/log/nginx/nginx-log.log; } diff --git a/frontend/src/views/app-store/apps/index.vue b/frontend/src/views/app-store/apps/index.vue index 621b1b372..7fe2539c3 100644 --- a/frontend/src/views/app-store/apps/index.vue +++ b/frontend/src/views/app-store/apps/index.vue @@ -1,6 +1,6 @@