2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 11:27:17 +08:00

feat: 优化网站启停

This commit is contained in:
2025-12-03 21:37:13 +08:00
parent 9bd08fcb37
commit e1f0a7d04f
7 changed files with 86 additions and 100 deletions

View File

@@ -1,13 +1,10 @@
package apache
// DisableConfName 禁用配置文件名
const DisableConfName = "000-disable.conf"
// DisablePagePath 禁用页面路径
const DisablePagePath = "/opt/ace/server/apache/stop"
// DisableConfContent 禁用配置内容
const DisableConfContent = `# 网站已停止
RewriteEngine on
RewriteRule ^.*$ - [R=503,L]
`
// SitesPath 网站目录
const SitesPath = "/opt/ace/sites"
// 配置文件序号范围
const (

View File

@@ -104,27 +104,44 @@ func NewProxyVhost(configDir string) (*ProxyVhost, error) {
}
func (v *baseVhost) Enable() bool {
// 检查禁用配置文件是否存在
disableFile := filepath.Join(v.configDir, "site", DisableConfName)
_, err := os.Stat(disableFile)
return os.IsNotExist(err)
root := v.vhost.GetDirectiveValue("DocumentRoot")
return root != DisablePagePath
}
func (v *baseVhost) SetEnable(enable bool, _ ...string) error {
serverDir := filepath.Join(v.configDir, "site")
disableFile := filepath.Join(serverDir, DisableConfName)
func (v *baseVhost) SetEnable(enable bool, siteConfig ...string) error {
name := ""
path := DisablePagePath
if enable {
// 启用:删除禁用配置文件
if err := os.Remove(disableFile); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to remove disable config: %w", err)
if len(siteConfig) != 2 {
return fmt.Errorf("site config is required to enable the vhost")
}
return nil
name = siteConfig[0]
path = siteConfig[1]
}
// 禁用:创建禁用配置文件
if err := os.WriteFile(disableFile, []byte(DisableConfContent), 0644); err != nil {
return fmt.Errorf("failed to write disable config: %w", err)
// 设置根目录
v.vhost.SetDirective("DocumentRoot", path)
// 更新 Directory 块
dirBlock := v.vhost.GetBlock("Directory")
if dirBlock != nil {
dirBlock.Args = []string{path}
} else {
block := v.vhost.AddBlock("Directory", path)
if block.Block != nil {
block.Block.Directives = append(block.Block.Directives,
&Directive{Name: "Options", Args: []string{"-Indexes", "+FollowSymLinks"}},
&Directive{Name: "AllowOverride", Args: []string{"All"}},
&Directive{Name: "Require", Args: []string{"all", "granted"}},
)
}
}
// 设置 Include 配置
v.vhost.RemoveDirectives("IncludeOptional")
if enable {
v.vhost.AddDirective("IncludeOptional", fmt.Sprintf("%s/%s/config/site/*.conf", SitesPath, name))
}
return nil

View File

@@ -51,39 +51,16 @@ func (s *VhostTestSuite) TestNewVhost() {
}
func (s *VhostTestSuite) TestEnable() {
// 默认应该是启用状态(没有 000-disable.conf
// 默认应该是启用状态
s.True(s.vhost.Enable())
// 禁用网站
s.NoError(s.vhost.SetEnable(false))
s.False(s.vhost.Enable())
// 验证禁用文件存在
disableFile := filepath.Join(s.configDir, "site", DisableConfName)
_, err := os.Stat(disableFile)
s.NoError(err)
// 重新启用
s.NoError(s.vhost.SetEnable(true))
s.NoError(s.vhost.SetEnable(true, "testsite", "/var/www/test"))
s.True(s.vhost.Enable())
// 验证禁用文件已删除
_, err = os.Stat(disableFile)
s.True(os.IsNotExist(err))
}
func (s *VhostTestSuite) TestDisableConfigContent() {
// 禁用网站
s.NoError(s.vhost.SetEnable(false))
// 读取禁用配置内容
disableFile := filepath.Join(s.configDir, "site", DisableConfName)
content, err := os.ReadFile(disableFile)
s.NoError(err)
// 验证内容包含 503 返回
s.Contains(string(content), "503")
s.Contains(string(content), "RewriteRule")
}
func (s *VhostTestSuite) TestServerName() {

View File

@@ -1,14 +1,10 @@
package nginx
// DisableConfName 禁用配置文件名
const DisableConfName = "000-disable.conf"
// DisablePagePath 禁用页面路径
const DisablePagePath = "/opt/ace/server/nginx/stop"
// DisableConfContent 禁用配置内容
const DisableConfContent = `# 网站已停止
location / {
return 503;
}
`
// SitesPath 网站目录
const SitesPath = "/opt/ace/sites"
// 配置文件序号范围
const (

View File

@@ -75,6 +75,7 @@ func NewStaticVhost(configDir string) (*StaticVhost, error) {
if err != nil {
return nil, err
}
return &StaticVhost{baseVhost: base}, nil
}
@@ -84,6 +85,7 @@ func NewPHPVhost(configDir string) (*PHPVhost, error) {
if err != nil {
return nil, err
}
return &PHPVhost{baseVhost: base}, nil
}
@@ -93,31 +95,51 @@ func NewProxyVhost(configDir string) (*ProxyVhost, error) {
if err != nil {
return nil, err
}
return &ProxyVhost{baseVhost: base}, nil
}
func (v *baseVhost) Enable() bool {
// 检查禁用配置文件是否存在
disableFile := filepath.Join(v.configDir, "site", DisableConfName)
_, err := os.Stat(disableFile)
return os.IsNotExist(err)
}
func (v *baseVhost) SetEnable(enable bool, _ ...string) error {
serverDir := filepath.Join(v.configDir, "site")
disableFile := filepath.Join(serverDir, DisableConfName)
if enable {
// 启用:删除禁用配置文件
if err := os.Remove(disableFile); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to remove disable config: %w", err)
}
return nil
directive, err := v.parser.FindOne("server.root")
if err != nil {
return false
}
// 禁用:创建禁用配置文件
if err := os.WriteFile(disableFile, []byte(DisableConfContent), 0644); err != nil {
return fmt.Errorf("failed to write disable config: %w", err)
return directive.GetParameters()[0].GetValue() != DisablePagePath
}
func (v *baseVhost) SetEnable(enable bool, siteConfig ...string) error {
name := ""
path := DisablePagePath
if enable {
if len(siteConfig) != 2 {
return fmt.Errorf("site config is required to enable the vhost")
}
name = siteConfig[0]
path = siteConfig[1]
}
// 设置根目录
_ = v.parser.Clear("server.root")
if err := v.parser.Set("server", []*config.Directive{
{
Name: "root",
Parameters: v.parser.slices2Parameters([]string{path}),
},
}); err != nil {
return err
}
// 设置导入配置
_ = v.parser.Clear("server.include")
if enable {
return v.parser.Set("server", []*config.Directive{
{
Name: "include",
Parameters: v.parser.slices2Parameters([]string{fmt.Sprintf("%s/%s/config/site/*.conf", SitesPath, name)}),
},
})
}
return nil

View File

@@ -50,39 +50,16 @@ func (s *VhostTestSuite) TestNewVhost() {
}
func (s *VhostTestSuite) TestEnable() {
// 默认应该是启用状态(没有 000-disable.conf
// 默认应该是启用状态
s.True(s.vhost.Enable())
// 禁用网站
s.NoError(s.vhost.SetEnable(false))
s.False(s.vhost.Enable())
// 验证禁用文件存在
disableFile := filepath.Join(s.configDir, "site", DisableConfName)
_, err := os.Stat(disableFile)
s.NoError(err)
// 重新启用
s.NoError(s.vhost.SetEnable(true))
s.NoError(s.vhost.SetEnable(true, "testsite", "/var/www/test"))
s.True(s.vhost.Enable())
// 验证禁用文件已删除
_, err = os.Stat(disableFile)
s.True(os.IsNotExist(err))
}
func (s *VhostTestSuite) TestDisableConfigContent() {
// 禁用网站
s.NoError(s.vhost.SetEnable(false))
// 读取禁用配置内容
disableFile := filepath.Join(s.configDir, "site", DisableConfName)
content, err := os.ReadFile(disableFile)
s.NoError(err)
// 验证内容包含 503 返回
s.Contains(string(content), "503")
s.Contains(string(content), "return")
}
func (s *VhostTestSuite) TestServerName() {

View File

@@ -6,8 +6,8 @@ type Vhost interface {
// Enable 取启用状态
Enable() bool
// SetEnable 设置启用状态及停止页路径
SetEnable(enable bool, stopPage ...string) error
// SetEnable 设置启用状态及网站名称和根目录
SetEnable(enable bool, siteConfig ...string) error
// Listen 取监听配置
Listen() []Listen