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 @@ + + +
+ +