fix: 解决网站停止之后无法启动的问题 (#602)

This commit is contained in:
zhengkunwang223 2023-04-12 21:52:30 +08:00 committed by GitHub
parent 7f75ea06c2
commit f8432ba521
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 72 additions and 39 deletions

View File

@ -16,6 +16,7 @@ type Website struct {
WebsiteGroupID uint `gorm:"type:integer" json:"webSiteGroupId"` WebsiteGroupID uint `gorm:"type:integer" json:"webSiteGroupId"`
WebsiteSSLID uint `gorm:"type:integer" json:"webSiteSSLId"` WebsiteSSLID uint `gorm:"type:integer" json:"webSiteSSLId"`
Proxy string `gorm:"type:varchar(128);not null" json:"proxy"` Proxy string `gorm:"type:varchar(128);not null" json:"proxy"`
ProxyType string `gorm:"type:varchar;" json:"proxyType"`
ErrorLog bool `json:"errorLog"` ErrorLog bool `json:"errorLog"`
AccessLog bool `json:"accessLog"` AccessLog bool `json:"accessLog"`
DefaultServer bool `json:"defaultServer"` DefaultServer bool `json:"defaultServer"`

View File

@ -166,6 +166,7 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
} }
} }
}() }()
var proxy string
switch create.Type { switch create.Type {
case constant.Deployment: case constant.Deployment:
@ -184,8 +185,9 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
return err return err
} }
tx.Commit() tx.Commit()
website.AppInstallID = install.ID
appInstall = install appInstall = install
website.AppInstallID = install.ID
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
} else { } else {
var install model.AppInstall var install model.AppInstall
install, err = appInstallRepo.GetFirst(commonRepo.WithByID(create.AppInstallID)) install, err = appInstallRepo.GetFirst(commonRepo.WithByID(create.AppInstallID))
@ -194,6 +196,7 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
} }
appInstall = &install appInstall = &install
website.AppInstallID = appInstall.ID website.AppInstallID = appInstall.ID
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
} }
case constant.Runtime: case constant.Runtime:
runtime, err = runtimeRepo.GetFirst(commonRepo.WithByID(create.RuntimeID)) runtime, err = runtimeRepo.GetFirst(commonRepo.WithByID(create.RuntimeID))
@ -226,23 +229,27 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
tx.Commit() tx.Commit()
website.AppInstallID = install.ID website.AppInstallID = install.ID
appInstall = install appInstall = install
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
} else {
website.ProxyType = create.ProxyType
if website.ProxyType == constant.RuntimeProxyUnix {
proxy = fmt.Sprintf("unix:%s", path.Join("/www/sites", website.Alias, "php-pool", "php-fpm.sock"))
}
if website.ProxyType == constant.RuntimeProxyTcp {
proxy = fmt.Sprintf("127.0.0.1:%d", create.Port)
}
website.Proxy = proxy
} }
} }
tx, ctx := helper.GetTxAndContext()
defer tx.Rollback()
if err = websiteRepo.Create(ctx, website); err != nil {
return err
}
var domains []model.WebsiteDomain var domains []model.WebsiteDomain
domains = append(domains, model.WebsiteDomain{Domain: website.PrimaryDomain, WebsiteID: website.ID, Port: 80}) domains = append(domains, model.WebsiteDomain{Domain: website.PrimaryDomain, Port: 80})
otherDomainArray := strings.Split(create.OtherDomains, "\n") otherDomainArray := strings.Split(create.OtherDomains, "\n")
for _, domain := range otherDomainArray { for _, domain := range otherDomainArray {
if domain == "" { if domain == "" {
continue continue
} }
domainModel, err := getDomain(domain, website.ID) domainModel, err := getDomain(domain)
if err != nil { if err != nil {
return err return err
} }
@ -251,12 +258,18 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
} }
domains = append(domains, domainModel) domains = append(domains, domainModel)
} }
if len(domains) > 0 { if err != configDefaultNginx(website, domains, appInstall, runtime) {
if err = websiteDomainRepo.BatchCreate(ctx, domains); err != nil { return err
return err
}
} }
if err != configDefaultNginx(website, domains, appInstall, runtime, create.RuntimeConfig) { tx, ctx := helper.GetTxAndContext()
defer tx.Rollback()
if err = websiteRepo.Create(ctx, website); err != nil {
return err
}
for i := range domains {
domains[i].WebsiteID = website.ID
}
if err = websiteDomainRepo.BatchCreate(ctx, domains); err != nil {
return err return err
} }
tx.Commit() tx.Commit()

View File

@ -20,10 +20,8 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func getDomain(domainStr string, websiteID uint) (model.WebsiteDomain, error) { func getDomain(domainStr string) (model.WebsiteDomain, error) {
domain := model.WebsiteDomain{ domain := model.WebsiteDomain{}
WebsiteID: websiteID,
}
domainArray := strings.Split(domainStr, ":") domainArray := strings.Split(domainStr, ":")
if len(domainArray) == 1 { if len(domainArray) == 1 {
domain.Domain = domainArray[0] domain.Domain = domainArray[0]
@ -123,7 +121,7 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website,
return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf")) return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf"))
} }
func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, appInstall *model.AppInstall, runtime *model.Runtime, runtimeConfig request.RuntimeConfig) error { func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, appInstall *model.AppInstall, runtime *model.Runtime) error {
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
if err != nil { if err != nil {
return err return err
@ -171,23 +169,15 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a
switch runtime.Type { switch runtime.Type {
case constant.RuntimePHP: case constant.RuntimePHP:
server.UpdateRoot(rootIndex) server.UpdateRoot(rootIndex)
proxy := ""
localPath := path.Join(nginxInstall.GetPath(), rootIndex, "index.php") localPath := path.Join(nginxInstall.GetPath(), rootIndex, "index.php")
if runtimeConfig.ProxyType == constant.RuntimeProxyUnix { server.UpdatePHPProxy([]string{website.Proxy}, localPath)
proxy = fmt.Sprintf("unix:%s", path.Join("/www/sites", website.Alias, "php-pool", "php-fpm.sock"))
}
if runtimeConfig.ProxyType == constant.RuntimeProxyTcp {
proxy = fmt.Sprintf("127.0.0.1:%d", runtimeConfig.Port)
}
server.UpdatePHPProxy([]string{proxy}, localPath)
} }
} }
if runtime.Resource == constant.ResourceAppstore { if runtime.Resource == constant.ResourceAppstore {
switch runtime.Type { switch runtime.Type {
case constant.RuntimePHP: case constant.RuntimePHP:
server.UpdateRoot(rootIndex) server.UpdateRoot(rootIndex)
proxy := fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort) server.UpdatePHPProxy([]string{website.Proxy}, "")
server.UpdatePHPProxy([]string{proxy}, "")
} }
} }
} }
@ -457,8 +447,11 @@ func opWebsite(website *model.Website, operate string) error {
} }
server := servers[0] server := servers[0]
if operate == constant.StopWeb { if operate == constant.StopWeb {
if website.Type != constant.Static { if website.Type == constant.Deployment || website.Type == constant.Static || website.Type == constant.Proxy {
server.RemoveDirective("location", []string{"/"}) server.RemoveDirective("location", []string{"", "/"})
}
if website.Type == constant.Runtime {
server.RemoveDirective("location", []string{"~", "[^/]\\.php(/|$)"})
} }
server.UpdateRoot("/usr/share/nginx/html/stop") server.UpdateRoot("/usr/share/nginx/html/stop")
website.Status = constant.WebStopped website.Status = constant.WebStopped
@ -479,6 +472,14 @@ func opWebsite(website *model.Website, operate string) error {
case constant.Proxy: case constant.Proxy:
server.RemoveDirective("root", nil) server.RemoveDirective("root", nil)
server.UpdateRootProxy([]string{website.Proxy}) server.UpdateRootProxy([]string{website.Proxy})
case constant.Runtime:
rootIndex := path.Join("/www/sites", website.Alias, "index")
server.UpdateRoot(rootIndex)
localPath := ""
if website.ProxyType == constant.RuntimeProxyUnix {
localPath = path.Join(nginxInstall.Install.GetPath(), rootIndex, "index.php")
}
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
} }
website.Status = constant.WebRunning website.Status = constant.WebRunning
now := time.Now() now := time.Now()

View File

@ -24,6 +24,7 @@ func Init() {
migrations.AddTableRuntime, migrations.AddTableRuntime,
migrations.UpdateTableApp, migrations.UpdateTableApp,
migrations.UpdateTableHost, migrations.UpdateTableHost,
migrations.UpdateTableWebsite,
}) })
if err := m.Migrate(); err != nil { if err := m.Migrate(); err != nil {
global.LOG.Error(err) global.LOG.Error(err)

View File

@ -251,7 +251,7 @@ var AddDefaultGroup = &gormigrate.Migration{
var AddTableRuntime = &gormigrate.Migration{ var AddTableRuntime = &gormigrate.Migration{
ID: "20230406-add-table-runtime", ID: "20230406-add-table-runtime",
Migrate: func(tx *gorm.DB) error { Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&model.Runtime{}, &model.Website{}) return tx.AutoMigrate(&model.Runtime{})
}, },
} }
@ -274,3 +274,10 @@ var UpdateTableHost = &gormigrate.Migration{
return nil return nil
}, },
} }
var UpdateTableWebsite = &gormigrate.Migration{
ID: "20230406-update-table-website",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&model.Website{})
},
}

View File

@ -44,6 +44,7 @@ var repeatKeys = map[string]struct {
"set": {}, "set": {},
"if": {}, "if": {},
"proxy_set_header": {}, "proxy_set_header": {},
"location": {},
} }
func IsRepeatKey(key string) bool { func IsRepeatKey(key string) bool {

View File

@ -24,8 +24,8 @@ func NewLocation(directive *Directive) *Location {
location.Match = directive.Parameters[0] location.Match = directive.Parameters[0]
return location return location
} else if len(directive.Parameters) == 2 { } else if len(directive.Parameters) == 2 {
location.Modifier = directive.Parameters[0]
location.Match = directive.Parameters[1] location.Match = directive.Parameters[1]
location.Modifier = directive.Parameters[0]
return location return location
} }
return nil return nil

View File

@ -116,14 +116,22 @@ func (s *Server) RemoveDirective(key string, params []string) {
directives := s.Directives directives := s.Directives
var newDirectives []IDirective var newDirectives []IDirective
for _, dir := range directives { for _, dir := range directives {
if dir.GetName() == key { if key == "location" {
if IsRepeatKey(key) && len(params) > 0 { if location, ok := dir.(*Location); ok {
oldParams := dir.GetParameters() if len(params) == 2 && location.Match == params[1] && location.Modifier == params[0] {
if oldParams[0] == params[0] { continue
}
}
} else {
if dir.GetName() == key {
if len(params) > 0 {
oldParams := dir.GetParameters()
if oldParams[0] == params[0] {
continue
}
} else {
continue continue
} }
} else {
continue
} }
} }
newDirectives = append(newDirectives, dir) newDirectives = append(newDirectives, dir)

View File

@ -55,6 +55,7 @@ export namespace Website {
webSiteGroupId: number; webSiteGroupId: number;
otherDomains: string; otherDomains: string;
proxy: string; proxy: string;
proxyType: string;
} }
export interface WebSiteUpdateReq { export interface WebSiteUpdateReq {