2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 13:47:15 +08:00

feat: 优化网站启停

This commit is contained in:
2025-12-03 22:00:35 +08:00
parent 2ed9d0b6fa
commit 62bd8927e3
12 changed files with 37 additions and 1294 deletions

View File

@@ -108,40 +108,35 @@ func (v *baseVhost) Enable() bool {
return root != DisablePagePath
}
func (v *baseVhost) SetEnable(enable bool, siteConfig ...string) error {
name := ""
func (v *baseVhost) SetEnable(enable bool) error {
path := DisablePagePath
if enable {
if len(siteConfig) != 2 {
return fmt.Errorf("site config is required to enable the vhost")
// 尝试获取保存的根目录
if root, err := os.ReadFile(filepath.Join(v.configDir, "root.saved")); err != nil {
path = filepath.Join(SitesPath, filepath.Dir(v.configDir), "public") // 默认根目录
} else {
path = strings.TrimSpace(string(root))
}
} else {
// 禁用时,保存当前根目录
currentRoot := v.Root()
if currentRoot != "" && currentRoot != DisablePagePath {
if err := os.WriteFile(filepath.Join(v.configDir, "root.saved"), []byte(currentRoot), 0644); err != nil {
return fmt.Errorf("failed to save current root: %w", err)
}
}
name = siteConfig[0]
path = siteConfig[1]
}
// 设置根目录
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"}},
)
}
if err := v.SetRoot(path); err != nil {
return err
}
// 设置 Include 配置
v.vhost.RemoveDirectives("IncludeOptional")
if enable {
v.vhost.AddDirective("IncludeOptional", fmt.Sprintf("%s/%s/config/site/*.conf", SitesPath, name))
v.vhost.AddDirective("IncludeOptional", fmt.Sprintf("%s/site/*.conf", v.configDir))
}
return nil

View File

@@ -59,7 +59,7 @@ func (s *VhostTestSuite) TestEnable() {
s.False(s.vhost.Enable())
// 重新启用
s.NoError(s.vhost.SetEnable(true, "testsite", "/var/www/test"))
s.NoError(s.vhost.SetEnable(true))
s.True(s.vhost.Enable())
}