mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 19:37:18 +08:00
feat: 重命名配置目录
This commit is contained in:
@@ -186,7 +186,7 @@ func (r *websiteRepo) Get(id uint) (*types.WebsiteSetting, error) {
|
||||
if phpVhost, ok := vhost.(webservertypes.PHPVhost); ok {
|
||||
setting.PHP = phpVhost.PHP()
|
||||
// 伪静态
|
||||
rewrite, _ := io.Read(filepath.Join(app.Root, "sites", website.Name, "config", "vhost", "010-rewrite.conf"))
|
||||
rewrite, _ := io.Read(filepath.Join(app.Root, "sites", website.Name, "config", "site", "010-rewrite.conf"))
|
||||
setting.Rewrite = rewrite
|
||||
}
|
||||
|
||||
@@ -289,10 +289,10 @@ func (r *websiteRepo) Create(req *request.WebsiteCreate) (*biz.Website, error) {
|
||||
}
|
||||
|
||||
// 创建配置文件目录
|
||||
if err = os.MkdirAll(filepath.Join(app.Root, "sites", req.Name, "config", "vhost"), 0644); err != nil {
|
||||
if err = os.MkdirAll(filepath.Join(app.Root, "sites", req.Name, "config", "site"), 0644); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = os.MkdirAll(filepath.Join(app.Root, "sites", req.Name, "config", "global"), 0644); err != nil {
|
||||
if err = os.MkdirAll(filepath.Join(app.Root, "sites", req.Name, "config", "shared"), 0644); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 创建日志目录
|
||||
@@ -317,7 +317,7 @@ func (r *websiteRepo) Create(req *request.WebsiteCreate) (*biz.Website, error) {
|
||||
if err = phpVhost.SetPHP(req.PHP); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", req.Name, "config", "vhost", "010-rewrite.conf"), "", 0644); err != nil {
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", req.Name, "config", "site", "010-rewrite.conf"), "", 0644); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -361,7 +361,7 @@ func (r *websiteRepo) Create(req *request.WebsiteCreate) (*biz.Website, error) {
|
||||
if err = vhost.Save(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", req.Name, "config", "vhost", "001-acme.conf"), "", 0644); err != nil {
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", req.Name, "config", "site", "001-acme.conf"), "", 0644); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", req.Name, "config", "fullchain.pem"), "", 0644); err != nil {
|
||||
@@ -534,7 +534,7 @@ func (r *websiteRepo) Update(req *request.WebsiteUpdate) error {
|
||||
return err
|
||||
}
|
||||
// 伪静态
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", website.Name, "config", "vhost", "010-rewrite.conf"), req.Rewrite, 0644); err != nil {
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", website.Name, "config", "site", "010-rewrite.conf"), req.Rewrite, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
// 防跨站
|
||||
@@ -684,7 +684,7 @@ func (r *websiteRepo) ResetConfig(id uint) error {
|
||||
if err = vhost.Save(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", website.Name, "config", "vhost", "001-acme.conf"), "", 0644); err != nil {
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", website.Name, "config", "site", "001-acme.conf"), "", 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", website.Name, "config", "fullchain.pem"), "", 0644); err != nil {
|
||||
@@ -695,7 +695,7 @@ func (r *websiteRepo) ResetConfig(id uint) error {
|
||||
}
|
||||
// PHP 网站默认伪静态
|
||||
if website.Type == biz.WebsiteTypePHP {
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", website.Name, "config", "vhost", "010-rewrite.conf"), "", 0644); err != nil {
|
||||
if err = io.Write(filepath.Join(app.Root, "sites", website.Name, "config", "site", "010-rewrite.conf"), "", 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ const DefaultVhostConf = `<VirtualHost *:80>
|
||||
CustomLog /opt/ace/sites/default/log/access.log combined
|
||||
|
||||
# custom configs
|
||||
IncludeOptional /opt/ace/sites/default/config/vhost/*.conf
|
||||
IncludeOptional /opt/ace/sites/default/config/site/*.conf
|
||||
|
||||
<Directory /opt/ace/sites/default/public>
|
||||
Options -Indexes +FollowSymLinks
|
||||
|
||||
@@ -18,9 +18,9 @@ var proxyFilePattern = regexp.MustCompile(`^(\d{3})-proxy\.conf$`)
|
||||
// balancerFilePattern 匹配负载均衡配置文件名
|
||||
var balancerFilePattern = regexp.MustCompile(`^(\d{3})-balancer-(.+)\.conf$`)
|
||||
|
||||
// parseProxyFiles 从 vhost 目录解析所有代理配置
|
||||
func parseProxyFiles(vhostDir string) ([]types.Proxy, error) {
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
// parseProxyFiles 从 site 目录解析所有代理配置
|
||||
func parseProxyFiles(siteDir string) ([]types.Proxy, error) {
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
@@ -44,7 +44,7 @@ func parseProxyFiles(vhostDir string) ([]types.Proxy, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
filePath := filepath.Join(vhostDir, entry.Name())
|
||||
filePath := filepath.Join(siteDir, entry.Name())
|
||||
proxy, err := parseProxyFile(filePath)
|
||||
if err != nil {
|
||||
continue // 跳过解析失败的文件
|
||||
@@ -123,9 +123,9 @@ func parseProxyFile(filePath string) (*types.Proxy, error) {
|
||||
}
|
||||
|
||||
// writeProxyFiles 将代理配置写入文件
|
||||
func writeProxyFiles(vhostDir string, proxies []types.Proxy) error {
|
||||
func writeProxyFiles(siteDir string, proxies []types.Proxy) error {
|
||||
// 删除现有的代理配置文件 (200-299)
|
||||
if err := clearProxyFiles(vhostDir); err != nil {
|
||||
if err := clearProxyFiles(siteDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ func writeProxyFiles(vhostDir string, proxies []types.Proxy) error {
|
||||
}
|
||||
|
||||
fileName := fmt.Sprintf("%03d-proxy.conf", num)
|
||||
filePath := filepath.Join(vhostDir, fileName)
|
||||
filePath := filepath.Join(siteDir, fileName)
|
||||
|
||||
content := generateProxyConfig(proxy)
|
||||
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
|
||||
@@ -149,8 +149,8 @@ func writeProxyFiles(vhostDir string, proxies []types.Proxy) error {
|
||||
}
|
||||
|
||||
// clearProxyFiles 清除所有代理配置文件
|
||||
func clearProxyFiles(vhostDir string) error {
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
func clearProxyFiles(siteDir string) error {
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
@@ -170,7 +170,7 @@ func clearProxyFiles(vhostDir string) error {
|
||||
|
||||
num, _ := strconv.Atoi(matches[1])
|
||||
if num >= ProxyStartNum && num <= ProxyEndNum {
|
||||
filePath := filepath.Join(vhostDir, entry.Name())
|
||||
filePath := filepath.Join(siteDir, entry.Name())
|
||||
if err := os.Remove(filePath); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to delete proxy config: %w", err)
|
||||
}
|
||||
@@ -251,9 +251,9 @@ func generateProxyConfig(proxy types.Proxy) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// parseBalancerFiles 从 global 目录解析所有负载均衡配置(Apache 的 upstream 等价物)
|
||||
func parseBalancerFiles(globalDir string) (map[string]types.Upstream, error) {
|
||||
entries, err := os.ReadDir(globalDir)
|
||||
// parseBalancerFiles 从 shared 目录解析所有负载均衡配置(Apache 的 upstream 等价物)
|
||||
func parseBalancerFiles(sharedDir string) (map[string]types.Upstream, error) {
|
||||
entries, err := os.ReadDir(sharedDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
@@ -273,7 +273,7 @@ func parseBalancerFiles(globalDir string) (map[string]types.Upstream, error) {
|
||||
}
|
||||
|
||||
name := matches[2]
|
||||
filePath := filepath.Join(globalDir, entry.Name())
|
||||
filePath := filepath.Join(sharedDir, entry.Name())
|
||||
upstream, err := parseBalancerFile(filePath)
|
||||
if err != nil {
|
||||
continue // 跳过解析失败的文件
|
||||
@@ -342,9 +342,9 @@ func parseBalancerFile(filePath string) (*types.Upstream, error) {
|
||||
}
|
||||
|
||||
// writeBalancerFiles 将负载均衡配置写入文件
|
||||
func writeBalancerFiles(globalDir string, upstreams map[string]types.Upstream) error {
|
||||
func writeBalancerFiles(sharedDir string, upstreams map[string]types.Upstream) error {
|
||||
// 删除现有的负载均衡配置文件
|
||||
if err := clearBalancerFiles(globalDir); err != nil {
|
||||
if err := clearBalancerFiles(sharedDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -352,7 +352,7 @@ func writeBalancerFiles(globalDir string, upstreams map[string]types.Upstream) e
|
||||
num := 100
|
||||
for name, upstream := range upstreams {
|
||||
fileName := fmt.Sprintf("%03d-balancer-%s.conf", num, name)
|
||||
filePath := filepath.Join(globalDir, fileName)
|
||||
filePath := filepath.Join(sharedDir, fileName)
|
||||
|
||||
content := generateBalancerConfig(name, upstream)
|
||||
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
|
||||
@@ -365,8 +365,8 @@ func writeBalancerFiles(globalDir string, upstreams map[string]types.Upstream) e
|
||||
}
|
||||
|
||||
// clearBalancerFiles 清除所有负载均衡配置文件
|
||||
func clearBalancerFiles(globalDir string) error {
|
||||
entries, err := os.ReadDir(globalDir)
|
||||
func clearBalancerFiles(sharedDir string) error {
|
||||
entries, err := os.ReadDir(sharedDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
@@ -380,7 +380,7 @@ func clearBalancerFiles(globalDir string) error {
|
||||
}
|
||||
|
||||
if balancerFilePattern.MatchString(entry.Name()) {
|
||||
filePath := filepath.Join(globalDir, entry.Name())
|
||||
filePath := filepath.Join(sharedDir, entry.Name())
|
||||
if err := os.Remove(filePath); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to delete balancer config: %w", err)
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ import (
|
||||
// redirectFilePattern 匹配重定向配置文件名 (100-199)
|
||||
var redirectFilePattern = regexp.MustCompile(`^(\d{3})-redirect\.conf$`)
|
||||
|
||||
// parseRedirectFiles 从 vhost 目录解析所有重定向配置
|
||||
func parseRedirectFiles(vhostDir string) ([]types.Redirect, error) {
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
// parseRedirectFiles 从 site 目录解析所有重定向配置
|
||||
func parseRedirectFiles(siteDir string) ([]types.Redirect, error) {
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
@@ -40,7 +40,7 @@ func parseRedirectFiles(vhostDir string) ([]types.Redirect, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
filePath := filepath.Join(vhostDir, entry.Name())
|
||||
filePath := filepath.Join(siteDir, entry.Name())
|
||||
redirect, err := parseRedirectFile(filePath)
|
||||
if err != nil {
|
||||
continue // 跳过解析失败的文件
|
||||
@@ -118,9 +118,9 @@ func parseRedirectFile(filePath string) (*types.Redirect, error) {
|
||||
}
|
||||
|
||||
// writeRedirectFiles 将重定向配置写入文件
|
||||
func writeRedirectFiles(vhostDir string, redirects []types.Redirect) error {
|
||||
func writeRedirectFiles(siteDir string, redirects []types.Redirect) error {
|
||||
// 删除现有的重定向配置文件 (100-199)
|
||||
if err := clearRedirectFiles(vhostDir); err != nil {
|
||||
if err := clearRedirectFiles(siteDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ func writeRedirectFiles(vhostDir string, redirects []types.Redirect) error {
|
||||
}
|
||||
|
||||
fileName := fmt.Sprintf("%03d-redirect.conf", num)
|
||||
filePath := filepath.Join(vhostDir, fileName)
|
||||
filePath := filepath.Join(siteDir, fileName)
|
||||
|
||||
content := generateRedirectConfig(redirect)
|
||||
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
|
||||
@@ -144,8 +144,8 @@ func writeRedirectFiles(vhostDir string, redirects []types.Redirect) error {
|
||||
}
|
||||
|
||||
// clearRedirectFiles 清除所有重定向配置文件
|
||||
func clearRedirectFiles(vhostDir string) error {
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
func clearRedirectFiles(siteDir string) error {
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
@@ -165,7 +165,7 @@ func clearRedirectFiles(vhostDir string) error {
|
||||
|
||||
num, _ := strconv.Atoi(matches[1])
|
||||
if num >= RedirectStartNum && num <= RedirectEndNum {
|
||||
filePath := filepath.Join(vhostDir, entry.Name())
|
||||
filePath := filepath.Join(siteDir, entry.Name())
|
||||
if err := os.Remove(filePath); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to delete redirect config: %w", err)
|
||||
}
|
||||
|
||||
@@ -105,13 +105,13 @@ func NewProxyVhost(configDir string) (*ProxyVhost, error) {
|
||||
|
||||
func (v *baseVhost) Enable() bool {
|
||||
// 检查禁用配置文件是否存在
|
||||
disableFile := filepath.Join(v.configDir, "vhost", DisableConfName)
|
||||
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, "vhost")
|
||||
serverDir := filepath.Join(v.configDir, "site")
|
||||
disableFile := filepath.Join(serverDir, DisableConfName)
|
||||
|
||||
if enable {
|
||||
@@ -548,14 +548,14 @@ func (v *baseVhost) ClearBasicAuth() error {
|
||||
}
|
||||
|
||||
func (v *baseVhost) Redirects() []types.Redirect {
|
||||
vhostDir := filepath.Join(v.configDir, "vhost")
|
||||
redirects, _ := parseRedirectFiles(vhostDir)
|
||||
siteDir := filepath.Join(v.configDir, "site")
|
||||
redirects, _ := parseRedirectFiles(siteDir)
|
||||
return redirects
|
||||
}
|
||||
|
||||
func (v *baseVhost) SetRedirects(redirects []types.Redirect) error {
|
||||
vhostDir := filepath.Join(v.configDir, "vhost")
|
||||
return writeRedirectFiles(vhostDir, redirects)
|
||||
siteDir := filepath.Join(v.configDir, "site")
|
||||
return writeRedirectFiles(siteDir, redirects)
|
||||
}
|
||||
|
||||
// ========== PHPVhost ==========
|
||||
@@ -647,33 +647,33 @@ func (v *PHPVhost) SetPHP(version uint) error {
|
||||
// ========== ProxyVhost ==========
|
||||
|
||||
func (v *ProxyVhost) Proxies() []types.Proxy {
|
||||
vhostDir := filepath.Join(v.configDir, "vhost")
|
||||
proxies, _ := parseProxyFiles(vhostDir)
|
||||
siteDir := filepath.Join(v.configDir, "site")
|
||||
proxies, _ := parseProxyFiles(siteDir)
|
||||
return proxies
|
||||
}
|
||||
|
||||
func (v *ProxyVhost) SetProxies(proxies []types.Proxy) error {
|
||||
vhostDir := filepath.Join(v.configDir, "vhost")
|
||||
return writeProxyFiles(vhostDir, proxies)
|
||||
siteDir := filepath.Join(v.configDir, "site")
|
||||
return writeProxyFiles(siteDir, proxies)
|
||||
}
|
||||
|
||||
func (v *ProxyVhost) ClearProxies() error {
|
||||
vhostDir := filepath.Join(v.configDir, "vhost")
|
||||
return clearProxyFiles(vhostDir)
|
||||
siteDir := filepath.Join(v.configDir, "site")
|
||||
return clearProxyFiles(siteDir)
|
||||
}
|
||||
|
||||
func (v *ProxyVhost) Upstreams() map[string]types.Upstream {
|
||||
globalDir := filepath.Join(v.configDir, "global")
|
||||
upstreams, _ := parseBalancerFiles(globalDir)
|
||||
sharedDir := filepath.Join(v.configDir, "shared")
|
||||
upstreams, _ := parseBalancerFiles(sharedDir)
|
||||
return upstreams
|
||||
}
|
||||
|
||||
func (v *ProxyVhost) SetUpstreams(upstreams map[string]types.Upstream) error {
|
||||
globalDir := filepath.Join(v.configDir, "global")
|
||||
return writeBalancerFiles(globalDir, upstreams)
|
||||
sharedDir := filepath.Join(v.configDir, "shared")
|
||||
return writeBalancerFiles(sharedDir, upstreams)
|
||||
}
|
||||
|
||||
func (v *ProxyVhost) ClearUpstreams() error {
|
||||
globalDir := filepath.Join(v.configDir, "global")
|
||||
return clearBalancerFiles(globalDir)
|
||||
sharedDir := filepath.Join(v.configDir, "shared")
|
||||
return clearBalancerFiles(sharedDir)
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func (s *VhostTestSuite) SetupTest() {
|
||||
s.configDir = configDir
|
||||
|
||||
// 创建 vhost 目录
|
||||
err = os.MkdirAll(filepath.Join(configDir, "vhost"), 0755)
|
||||
err = os.MkdirAll(filepath.Join(configDir, "site"), 0755)
|
||||
s.Require().NoError(err)
|
||||
|
||||
vhost, err := NewPHPVhost(configDir)
|
||||
@@ -59,7 +59,7 @@ func (s *VhostTestSuite) TestEnable() {
|
||||
s.False(s.vhost.Enable())
|
||||
|
||||
// 验证禁用文件存在
|
||||
disableFile := filepath.Join(s.configDir, "vhost", DisableConfName)
|
||||
disableFile := filepath.Join(s.configDir, "site", DisableConfName)
|
||||
_, err := os.Stat(disableFile)
|
||||
s.NoError(err)
|
||||
|
||||
@@ -77,7 +77,7 @@ func (s *VhostTestSuite) TestDisableConfigContent() {
|
||||
s.NoError(s.vhost.SetEnable(false))
|
||||
|
||||
// 读取禁用配置内容
|
||||
disableFile := filepath.Join(s.configDir, "vhost", DisableConfName)
|
||||
disableFile := filepath.Join(s.configDir, "site", DisableConfName)
|
||||
content, err := os.ReadFile(disableFile)
|
||||
s.NoError(err)
|
||||
|
||||
@@ -354,8 +354,8 @@ func (s *VhostTestSuite) TestPHPFilesMatchBlock() {
|
||||
}
|
||||
|
||||
func (s *VhostTestSuite) TestDefaultVhostConfIncludesServerD() {
|
||||
// 验证默认配置包含 vhost 的 include
|
||||
s.Contains(DefaultVhostConf, "vhost")
|
||||
// 验证默认配置包含 site 的 include
|
||||
s.Contains(DefaultVhostConf, "site")
|
||||
s.Contains(DefaultVhostConf, "IncludeOptional")
|
||||
}
|
||||
|
||||
@@ -382,8 +382,8 @@ func (s *VhostTestSuite) TestRedirects() {
|
||||
s.NoError(s.vhost.SetRedirects(redirects))
|
||||
|
||||
// 验证重定向文件已创建
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
s.NoError(err)
|
||||
|
||||
redirectCount := 0
|
||||
@@ -411,8 +411,8 @@ func (s *VhostTestSuite) TestRedirectURL() {
|
||||
s.NoError(s.vhost.SetRedirects(redirects))
|
||||
|
||||
// 读取配置文件内容
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
content, err := os.ReadFile(filepath.Join(vhostDir, "100-redirect.conf"))
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
content, err := os.ReadFile(filepath.Join(siteDir, "100-redirect.conf"))
|
||||
s.NoError(err)
|
||||
|
||||
s.Contains(string(content), "Redirect 301")
|
||||
@@ -433,8 +433,8 @@ func (s *VhostTestSuite) TestRedirectHost() {
|
||||
s.NoError(s.vhost.SetRedirects(redirects))
|
||||
|
||||
// 读取配置文件内容
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
content, err := os.ReadFile(filepath.Join(vhostDir, "100-redirect.conf"))
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
content, err := os.ReadFile(filepath.Join(siteDir, "100-redirect.conf"))
|
||||
s.NoError(err)
|
||||
|
||||
s.Contains(string(content), "RewriteEngine")
|
||||
@@ -453,8 +453,8 @@ func (s *VhostTestSuite) TestRedirect404() {
|
||||
s.NoError(s.vhost.SetRedirects(redirects))
|
||||
|
||||
// 读取配置文件内容
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
content, err := os.ReadFile(filepath.Join(vhostDir, "100-redirect.conf"))
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
content, err := os.ReadFile(filepath.Join(siteDir, "100-redirect.conf"))
|
||||
s.NoError(err)
|
||||
|
||||
s.Contains(string(content), "ErrorDocument 404")
|
||||
@@ -478,7 +478,7 @@ func (s *ProxyVhostTestSuite) SetupTest() {
|
||||
s.configDir = configDir
|
||||
|
||||
// 创建 vhost 和 global 目录
|
||||
s.NoError(os.MkdirAll(filepath.Join(configDir, "vhost"), 0755))
|
||||
s.NoError(os.MkdirAll(filepath.Join(configDir, "site"), 0755))
|
||||
s.NoError(os.MkdirAll(filepath.Join(configDir, "global"), 0755))
|
||||
|
||||
vhost, err := NewProxyVhost(configDir)
|
||||
@@ -512,8 +512,8 @@ func (s *ProxyVhostTestSuite) TestProxies() {
|
||||
s.NoError(s.vhost.SetProxies(proxies))
|
||||
|
||||
// 验证代理文件已创建
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
s.NoError(err)
|
||||
|
||||
proxyCount := 0
|
||||
@@ -541,8 +541,8 @@ func (s *ProxyVhostTestSuite) TestProxyConfig() {
|
||||
s.NoError(s.vhost.SetProxies(proxies))
|
||||
|
||||
// 读取配置文件内容
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
content, err := os.ReadFile(filepath.Join(vhostDir, "200-proxy.conf"))
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
content, err := os.ReadFile(filepath.Join(siteDir, "200-proxy.conf"))
|
||||
s.NoError(err)
|
||||
|
||||
s.Contains(string(content), "ProxyPass /")
|
||||
@@ -581,8 +581,8 @@ func (s *ProxyVhostTestSuite) TestUpstreams() {
|
||||
s.NoError(s.vhost.SetUpstreams(upstreams))
|
||||
|
||||
// 验证 balancer 文件已创建
|
||||
globalDir := filepath.Join(s.configDir, "global")
|
||||
entries, err := os.ReadDir(globalDir)
|
||||
sharedDir := filepath.Join(s.configDir, "shared")
|
||||
entries, err := os.ReadDir(sharedDir)
|
||||
s.NoError(err)
|
||||
s.NotEmpty(entries)
|
||||
|
||||
@@ -605,12 +605,12 @@ func (s *ProxyVhostTestSuite) TestBalancerConfig() {
|
||||
s.NoError(s.vhost.SetUpstreams(upstreams))
|
||||
|
||||
// 读取配置文件内容
|
||||
globalDir := filepath.Join(s.configDir, "global")
|
||||
entries, err := os.ReadDir(globalDir)
|
||||
sharedDir := filepath.Join(s.configDir, "shared")
|
||||
entries, err := os.ReadDir(sharedDir)
|
||||
s.NoError(err)
|
||||
s.Require().NotEmpty(entries)
|
||||
|
||||
content, err := os.ReadFile(filepath.Join(globalDir, entries[0].Name()))
|
||||
content, err := os.ReadFile(filepath.Join(sharedDir, entries[0].Name()))
|
||||
s.NoError(err)
|
||||
|
||||
s.Contains(string(content), "balancer://mybackend")
|
||||
|
||||
@@ -19,7 +19,7 @@ const (
|
||||
UpstreamStartNum = 100 // 上游服务器配置起始序号
|
||||
)
|
||||
|
||||
const DefaultConf = `include /opt/ace/sites/default/config/global/*.conf;
|
||||
const DefaultConf = `include /opt/ace/sites/default/config/shared/*.conf;
|
||||
server {
|
||||
listen 80;
|
||||
server_name localhost;
|
||||
@@ -28,7 +28,7 @@ server {
|
||||
# error page
|
||||
error_page 404 /404.html;
|
||||
# custom configs
|
||||
include /opt/ace/sites/default/config/vhost/*.conf;
|
||||
include /opt/ace/sites/default/config/site/*.conf;
|
||||
# browser cache
|
||||
location ~ .*\.(bmp|jpg|jpeg|png|gif|svg|ico|tiff|webp|avif|heif|heic|jxl)$ {
|
||||
expires 30d;
|
||||
|
||||
@@ -15,9 +15,9 @@ import (
|
||||
// proxyFilePattern 匹配代理配置文件名 (200-299)
|
||||
var proxyFilePattern = regexp.MustCompile(`^(\d{3})-proxy\.conf$`)
|
||||
|
||||
// parseProxyFiles 从 vhost 目录解析所有代理配置
|
||||
func parseProxyFiles(vhostDir string) ([]types.Proxy, error) {
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
// parseProxyFiles 从 site 目录解析所有代理配置
|
||||
func parseProxyFiles(siteDir string) ([]types.Proxy, error) {
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
@@ -41,7 +41,7 @@ func parseProxyFiles(vhostDir string) ([]types.Proxy, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
filePath := filepath.Join(vhostDir, entry.Name())
|
||||
filePath := filepath.Join(siteDir, entry.Name())
|
||||
proxy, err := parseProxyFile(filePath)
|
||||
if err != nil {
|
||||
continue // 跳过解析失败的文件
|
||||
@@ -150,9 +150,9 @@ func parseProxyFile(filePath string) (*types.Proxy, error) {
|
||||
}
|
||||
|
||||
// writeProxyFiles 将代理配置写入文件
|
||||
func writeProxyFiles(vhostDir string, proxies []types.Proxy) error {
|
||||
func writeProxyFiles(siteDir string, proxies []types.Proxy) error {
|
||||
// 删除现有的代理配置文件 (200-299)
|
||||
if err := clearProxyFiles(vhostDir); err != nil {
|
||||
if err := clearProxyFiles(siteDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ func writeProxyFiles(vhostDir string, proxies []types.Proxy) error {
|
||||
}
|
||||
|
||||
fileName := fmt.Sprintf("%03d-proxy.conf", num)
|
||||
filePath := filepath.Join(vhostDir, fileName)
|
||||
filePath := filepath.Join(siteDir, fileName)
|
||||
|
||||
content := generateProxyConfig(proxy)
|
||||
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
|
||||
@@ -176,8 +176,8 @@ func writeProxyFiles(vhostDir string, proxies []types.Proxy) error {
|
||||
}
|
||||
|
||||
// clearProxyFiles 清除所有代理配置文件
|
||||
func clearProxyFiles(vhostDir string) error {
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
func clearProxyFiles(siteDir string) error {
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
@@ -197,7 +197,7 @@ func clearProxyFiles(vhostDir string) error {
|
||||
|
||||
num, _ := strconv.Atoi(matches[1])
|
||||
if num >= ProxyStartNum && num <= ProxyEndNum {
|
||||
filePath := filepath.Join(vhostDir, entry.Name())
|
||||
filePath := filepath.Join(siteDir, entry.Name())
|
||||
if err := os.Remove(filePath); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to delete proxy config: %w", err)
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ import (
|
||||
// redirectFilePattern 匹配重定向配置文件名 (100-199)
|
||||
var redirectFilePattern = regexp.MustCompile(`^(\d{3})-redirect\.conf$`)
|
||||
|
||||
// parseRedirectFiles 从 vhost 目录解析所有重定向配置
|
||||
func parseRedirectFiles(vhostDir string) ([]types.Redirect, error) {
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
// parseRedirectFiles 从 site 目录解析所有重定向配置
|
||||
func parseRedirectFiles(siteDir string) ([]types.Redirect, error) {
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
@@ -40,7 +40,7 @@ func parseRedirectFiles(vhostDir string) ([]types.Redirect, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
filePath := filepath.Join(vhostDir, entry.Name())
|
||||
filePath := filepath.Join(siteDir, entry.Name())
|
||||
redirect, err := parseRedirectFile(filePath)
|
||||
if err != nil {
|
||||
continue // 跳过解析失败的文件
|
||||
@@ -105,9 +105,9 @@ func parseRedirectFile(filePath string) (*types.Redirect, error) {
|
||||
}
|
||||
|
||||
// writeRedirectFiles 将重定向配置写入文件
|
||||
func writeRedirectFiles(vhostDir string, redirects []types.Redirect) error {
|
||||
func writeRedirectFiles(siteDir string, redirects []types.Redirect) error {
|
||||
// 删除现有的重定向配置文件 (100-199)
|
||||
if err := clearRedirectFiles(vhostDir); err != nil {
|
||||
if err := clearRedirectFiles(siteDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ func writeRedirectFiles(vhostDir string, redirects []types.Redirect) error {
|
||||
}
|
||||
|
||||
fileName := fmt.Sprintf("%03d-redirect.conf", num)
|
||||
filePath := filepath.Join(vhostDir, fileName)
|
||||
filePath := filepath.Join(siteDir, fileName)
|
||||
|
||||
content := generateRedirectConfig(redirect)
|
||||
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
|
||||
@@ -131,8 +131,8 @@ func writeRedirectFiles(vhostDir string, redirects []types.Redirect) error {
|
||||
}
|
||||
|
||||
// clearRedirectFiles 清除所有重定向配置文件
|
||||
func clearRedirectFiles(vhostDir string) error {
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
func clearRedirectFiles(siteDir string) error {
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
@@ -152,7 +152,7 @@ func clearRedirectFiles(vhostDir string) error {
|
||||
|
||||
num, _ := strconv.Atoi(matches[1])
|
||||
if num >= RedirectStartNum && num <= RedirectEndNum {
|
||||
filePath := filepath.Join(vhostDir, entry.Name())
|
||||
filePath := filepath.Join(siteDir, entry.Name())
|
||||
if err = os.Remove(filePath); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to delete redirect config: %w", err)
|
||||
}
|
||||
|
||||
4
pkg/webserver/nginx/testdata/http.conf
vendored
4
pkg/webserver/nginx/testdata/http.conf
vendored
@@ -1,4 +1,4 @@
|
||||
include /opt/ace/sites/default/config/global/*.conf;
|
||||
include /opt/ace/sites/default/config/shared/*.conf;
|
||||
server {
|
||||
listen 80;
|
||||
server_name localhost;
|
||||
@@ -7,7 +7,7 @@ server {
|
||||
# error page
|
||||
error_page 404 /404.html;
|
||||
# custom configs
|
||||
include /opt/ace/sites/default/config/vhost/*.conf;
|
||||
include /opt/ace/sites/default/config/site/*.conf;
|
||||
# browser cache
|
||||
location ~ .*\.(bmp|jpg|jpeg|png|gif|svg|ico|tiff|webp|avif|heif|heic|jxl)$ {
|
||||
expires 30d;
|
||||
|
||||
4
pkg/webserver/nginx/testdata/https.conf
vendored
4
pkg/webserver/nginx/testdata/https.conf
vendored
@@ -1,4 +1,4 @@
|
||||
include /opt/ace/sites/default/config/global/*.conf;
|
||||
include /opt/ace/sites/default/config/shared/*.conf;
|
||||
server {
|
||||
listen 80;
|
||||
server_name localhost;
|
||||
@@ -15,7 +15,7 @@ server {
|
||||
# error page
|
||||
error_page 404 /404.html;
|
||||
# custom configs
|
||||
include /opt/ace/sites/default/config/vhost/*.conf;
|
||||
include /opt/ace/sites/default/config/site/*.conf;
|
||||
# browser cache
|
||||
location ~ .*\.(bmp|jpg|jpeg|png|gif|svg|ico|tiff|webp|avif|heif|heic|jxl)$ {
|
||||
expires 30d;
|
||||
|
||||
@@ -14,9 +14,9 @@ import (
|
||||
// upstreamFilePattern 匹配 upstream 配置文件名 (100-XXX-name.conf)
|
||||
var upstreamFilePattern = regexp.MustCompile(`^(\d{3})-(.+)\.conf$`)
|
||||
|
||||
// parseUpstreamFiles 从 global 目录解析所有 upstream 配置
|
||||
func parseUpstreamFiles(globalDir string) (map[string]types.Upstream, error) {
|
||||
entries, err := os.ReadDir(globalDir)
|
||||
// parseUpstreamFiles 从 shared 目录解析所有 upstream 配置
|
||||
func parseUpstreamFiles(sharedDir string) (map[string]types.Upstream, error) {
|
||||
entries, err := os.ReadDir(sharedDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
@@ -41,7 +41,7 @@ func parseUpstreamFiles(globalDir string) (map[string]types.Upstream, error) {
|
||||
}
|
||||
|
||||
name := matches[2]
|
||||
filePath := filepath.Join(globalDir, entry.Name())
|
||||
filePath := filepath.Join(sharedDir, entry.Name())
|
||||
upstream, err := parseUpstreamFile(filePath, name)
|
||||
if err != nil {
|
||||
continue // 跳过解析失败的文件
|
||||
@@ -116,9 +116,9 @@ func parseUpstreamFile(filePath string, expectedName string) (*types.Upstream, e
|
||||
}
|
||||
|
||||
// writeUpstreamFiles 将 upstream 配置写入文件
|
||||
func writeUpstreamFiles(globalDir string, upstreams map[string]types.Upstream) error {
|
||||
func writeUpstreamFiles(sharedDir string, upstreams map[string]types.Upstream) error {
|
||||
// 删除现有的 upstream 配置文件
|
||||
if err := clearUpstreamFiles(globalDir); err != nil {
|
||||
if err := clearUpstreamFiles(sharedDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ func writeUpstreamFiles(globalDir string, upstreams map[string]types.Upstream) e
|
||||
num := UpstreamStartNum
|
||||
for name, upstream := range upstreams {
|
||||
fileName := fmt.Sprintf("%03d-%s.conf", num, name)
|
||||
filePath := filepath.Join(globalDir, fileName)
|
||||
filePath := filepath.Join(sharedDir, fileName)
|
||||
|
||||
content := generateUpstreamConfig(name, upstream)
|
||||
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
|
||||
@@ -139,8 +139,8 @@ func writeUpstreamFiles(globalDir string, upstreams map[string]types.Upstream) e
|
||||
}
|
||||
|
||||
// clearUpstreamFiles 清除所有 upstream 配置文件
|
||||
func clearUpstreamFiles(globalDir string) error {
|
||||
entries, err := os.ReadDir(globalDir)
|
||||
func clearUpstreamFiles(sharedDir string) error {
|
||||
entries, err := os.ReadDir(sharedDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
@@ -160,7 +160,7 @@ func clearUpstreamFiles(globalDir string) error {
|
||||
|
||||
num, _ := strconv.Atoi(matches[1])
|
||||
if num >= UpstreamStartNum {
|
||||
filePath := filepath.Join(globalDir, entry.Name())
|
||||
filePath := filepath.Join(sharedDir, entry.Name())
|
||||
if err = os.Remove(filePath); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to delete upstream config: %w", err)
|
||||
}
|
||||
|
||||
@@ -98,13 +98,13 @@ func NewProxyVhost(configDir string) (*ProxyVhost, error) {
|
||||
|
||||
func (v *baseVhost) Enable() bool {
|
||||
// 检查禁用配置文件是否存在
|
||||
disableFile := filepath.Join(v.configDir, "vhost", DisableConfName)
|
||||
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, "vhost")
|
||||
serverDir := filepath.Join(v.configDir, "site")
|
||||
disableFile := filepath.Join(serverDir, DisableConfName)
|
||||
|
||||
if enable {
|
||||
@@ -624,20 +624,20 @@ func (v *baseVhost) ClearBasicAuth() error {
|
||||
}
|
||||
|
||||
func (v *baseVhost) Redirects() []types.Redirect {
|
||||
vhostDir := filepath.Join(v.configDir, "vhost")
|
||||
redirects, _ := parseRedirectFiles(vhostDir)
|
||||
siteDir := filepath.Join(v.configDir, "site")
|
||||
redirects, _ := parseRedirectFiles(siteDir)
|
||||
return redirects
|
||||
}
|
||||
|
||||
func (v *baseVhost) SetRedirects(redirects []types.Redirect) error {
|
||||
vhostDir := filepath.Join(v.configDir, "vhost")
|
||||
return writeRedirectFiles(vhostDir, redirects)
|
||||
siteDir := filepath.Join(v.configDir, "site")
|
||||
return writeRedirectFiles(siteDir, redirects)
|
||||
}
|
||||
|
||||
// ========== PHPVhost ==========
|
||||
|
||||
func (v *PHPVhost) PHP() uint {
|
||||
phpConf := filepath.Join(v.configDir, "vhost", "010-php.conf")
|
||||
phpConf := filepath.Join(v.configDir, "site", "010-php.conf")
|
||||
content, err := os.ReadFile(phpConf)
|
||||
if err != nil {
|
||||
return 0
|
||||
@@ -654,10 +654,10 @@ func (v *PHPVhost) PHP() uint {
|
||||
|
||||
func (v *PHPVhost) SetPHP(version uint) error {
|
||||
if version == 0 {
|
||||
return os.Remove(filepath.Join(v.configDir, "vhost", "010-php.conf"))
|
||||
return os.Remove(filepath.Join(v.configDir, "site", "010-php.conf"))
|
||||
}
|
||||
|
||||
phpConf := filepath.Join(v.configDir, "vhost", "010-php.conf")
|
||||
phpConf := filepath.Join(v.configDir, "site", "010-php.conf")
|
||||
content := fmt.Sprintf("include enable-php-%d.conf;\n", version)
|
||||
if err := os.WriteFile(phpConf, []byte(content), 0644); err != nil {
|
||||
return fmt.Errorf("failed to write php config: %w", err)
|
||||
@@ -669,35 +669,35 @@ func (v *PHPVhost) SetPHP(version uint) error {
|
||||
// ========== ProxyVhost ==========
|
||||
|
||||
func (v *ProxyVhost) Proxies() []types.Proxy {
|
||||
vhostDir := filepath.Join(v.configDir, "vhost")
|
||||
proxies, _ := parseProxyFiles(vhostDir)
|
||||
siteDir := filepath.Join(v.configDir, "site")
|
||||
proxies, _ := parseProxyFiles(siteDir)
|
||||
return proxies
|
||||
}
|
||||
|
||||
func (v *ProxyVhost) SetProxies(proxies []types.Proxy) error {
|
||||
vhostDir := filepath.Join(v.configDir, "vhost")
|
||||
return writeProxyFiles(vhostDir, proxies)
|
||||
siteDir := filepath.Join(v.configDir, "site")
|
||||
return writeProxyFiles(siteDir, proxies)
|
||||
}
|
||||
|
||||
func (v *ProxyVhost) ClearProxies() error {
|
||||
vhostDir := filepath.Join(v.configDir, "vhost")
|
||||
return clearProxyFiles(vhostDir)
|
||||
siteDir := filepath.Join(v.configDir, "site")
|
||||
return clearProxyFiles(siteDir)
|
||||
}
|
||||
|
||||
func (v *ProxyVhost) Upstreams() map[string]types.Upstream {
|
||||
globalDir := filepath.Join(v.configDir, "global")
|
||||
upstreams, _ := parseUpstreamFiles(globalDir)
|
||||
sharedDir := filepath.Join(v.configDir, "shared")
|
||||
upstreams, _ := parseUpstreamFiles(sharedDir)
|
||||
return upstreams
|
||||
}
|
||||
|
||||
func (v *ProxyVhost) SetUpstreams(upstreams map[string]types.Upstream) error {
|
||||
globalDir := filepath.Join(v.configDir, "global")
|
||||
return writeUpstreamFiles(globalDir, upstreams)
|
||||
sharedDir := filepath.Join(v.configDir, "shared")
|
||||
return writeUpstreamFiles(sharedDir, upstreams)
|
||||
}
|
||||
|
||||
func (v *ProxyVhost) ClearUpstreams() error {
|
||||
globalDir := filepath.Join(v.configDir, "global")
|
||||
return clearUpstreamFiles(globalDir)
|
||||
sharedDir := filepath.Join(v.configDir, "shared")
|
||||
return clearUpstreamFiles(sharedDir)
|
||||
}
|
||||
|
||||
func (v *baseVhost) setHSTS(hsts bool) error {
|
||||
|
||||
@@ -27,8 +27,8 @@ func (s *VhostTestSuite) SetupTest() {
|
||||
s.Require().NoError(err)
|
||||
s.configDir = configDir
|
||||
|
||||
// 创建 vhost 目录
|
||||
err = os.MkdirAll(filepath.Join(configDir, "vhost"), 0755)
|
||||
// 创建 site 目录
|
||||
err = os.MkdirAll(filepath.Join(configDir, "site"), 0755)
|
||||
s.Require().NoError(err)
|
||||
|
||||
vhost, err := NewPHPVhost(configDir)
|
||||
@@ -58,7 +58,7 @@ func (s *VhostTestSuite) TestEnable() {
|
||||
s.False(s.vhost.Enable())
|
||||
|
||||
// 验证禁用文件存在
|
||||
disableFile := filepath.Join(s.configDir, "vhost", DisableConfName)
|
||||
disableFile := filepath.Join(s.configDir, "site", DisableConfName)
|
||||
_, err := os.Stat(disableFile)
|
||||
s.NoError(err)
|
||||
|
||||
@@ -76,7 +76,7 @@ func (s *VhostTestSuite) TestDisableConfigContent() {
|
||||
s.NoError(s.vhost.SetEnable(false))
|
||||
|
||||
// 读取禁用配置内容
|
||||
disableFile := filepath.Join(s.configDir, "vhost", DisableConfName)
|
||||
disableFile := filepath.Join(s.configDir, "site", DisableConfName)
|
||||
content, err := os.ReadFile(disableFile)
|
||||
s.NoError(err)
|
||||
|
||||
@@ -327,8 +327,8 @@ func (s *VhostTestSuite) TestAltSvc() {
|
||||
}
|
||||
|
||||
func (s *VhostTestSuite) TestDefaultConfIncludesServerD() {
|
||||
// 验证默认配置包含 vhost 的 include
|
||||
s.Contains(DefaultConf, "vhost")
|
||||
// 验证默认配置包含 site 的 include
|
||||
s.Contains(DefaultConf, "site")
|
||||
s.Contains(DefaultConf, "include")
|
||||
}
|
||||
|
||||
@@ -355,8 +355,8 @@ func (s *VhostTestSuite) TestRedirects() {
|
||||
s.NoError(s.vhost.SetRedirects(redirects))
|
||||
|
||||
// 验证重定向文件已创建
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
s.NoError(err)
|
||||
|
||||
redirectCount := 0
|
||||
@@ -384,8 +384,8 @@ func (s *VhostTestSuite) TestRedirectURL() {
|
||||
s.NoError(s.vhost.SetRedirects(redirects))
|
||||
|
||||
// 读取配置文件内容
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
content, err := os.ReadFile(filepath.Join(vhostDir, "100-redirect.conf"))
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
content, err := os.ReadFile(filepath.Join(siteDir, "100-redirect.conf"))
|
||||
s.NoError(err)
|
||||
|
||||
s.Contains(string(content), "location = /old-page")
|
||||
@@ -406,8 +406,8 @@ func (s *VhostTestSuite) TestRedirectHost() {
|
||||
s.NoError(s.vhost.SetRedirects(redirects))
|
||||
|
||||
// 读取配置文件内容
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
content, err := os.ReadFile(filepath.Join(vhostDir, "100-redirect.conf"))
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
content, err := os.ReadFile(filepath.Join(siteDir, "100-redirect.conf"))
|
||||
s.NoError(err)
|
||||
|
||||
s.Contains(string(content), "$host")
|
||||
@@ -427,8 +427,8 @@ func (s *VhostTestSuite) TestRedirect404() {
|
||||
s.NoError(s.vhost.SetRedirects(redirects))
|
||||
|
||||
// 读取配置文件内容
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
content, err := os.ReadFile(filepath.Join(vhostDir, "100-redirect.conf"))
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
content, err := os.ReadFile(filepath.Join(siteDir, "100-redirect.conf"))
|
||||
s.NoError(err)
|
||||
|
||||
s.Contains(string(content), "error_page 404")
|
||||
@@ -452,8 +452,8 @@ func (s *ProxyVhostTestSuite) SetupTest() {
|
||||
s.configDir = configDir
|
||||
|
||||
// 创建 vhost 和 global 目录
|
||||
s.NoError(os.MkdirAll(filepath.Join(configDir, "vhost"), 0755))
|
||||
s.NoError(os.MkdirAll(filepath.Join(configDir, "global"), 0755))
|
||||
s.NoError(os.MkdirAll(filepath.Join(configDir, "site"), 0755))
|
||||
s.NoError(os.MkdirAll(filepath.Join(configDir, "shared"), 0755))
|
||||
|
||||
vhost, err := NewProxyVhost(configDir)
|
||||
s.Require().NoError(err)
|
||||
@@ -486,8 +486,8 @@ func (s *ProxyVhostTestSuite) TestProxies() {
|
||||
s.NoError(s.vhost.SetProxies(proxies))
|
||||
|
||||
// 验证代理文件已创建
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
entries, err := os.ReadDir(vhostDir)
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
entries, err := os.ReadDir(siteDir)
|
||||
s.NoError(err)
|
||||
|
||||
proxyCount := 0
|
||||
@@ -516,8 +516,8 @@ func (s *ProxyVhostTestSuite) TestProxyConfig() {
|
||||
s.NoError(s.vhost.SetProxies(proxies))
|
||||
|
||||
// 读取配置文件内容
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
content, err := os.ReadFile(filepath.Join(vhostDir, "200-proxy.conf"))
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
content, err := os.ReadFile(filepath.Join(siteDir, "200-proxy.conf"))
|
||||
s.NoError(err)
|
||||
|
||||
s.Contains(string(content), "location /")
|
||||
@@ -557,8 +557,8 @@ func (s *ProxyVhostTestSuite) TestUpstreams() {
|
||||
s.NoError(s.vhost.SetUpstreams(upstreams))
|
||||
|
||||
// 验证 upstream 文件已创建
|
||||
globalDir := filepath.Join(s.configDir, "global")
|
||||
entries, err := os.ReadDir(globalDir)
|
||||
sharedDir := filepath.Join(s.configDir, "shared")
|
||||
entries, err := os.ReadDir(sharedDir)
|
||||
s.NoError(err)
|
||||
s.NotEmpty(entries)
|
||||
|
||||
@@ -583,12 +583,12 @@ func (s *ProxyVhostTestSuite) TestUpstreamConfig() {
|
||||
s.NoError(s.vhost.SetUpstreams(upstreams))
|
||||
|
||||
// 读取配置文件内容
|
||||
globalDir := filepath.Join(s.configDir, "global")
|
||||
entries, err := os.ReadDir(globalDir)
|
||||
sharedDir := filepath.Join(s.configDir, "shared")
|
||||
entries, err := os.ReadDir(sharedDir)
|
||||
s.NoError(err)
|
||||
s.Require().NotEmpty(entries)
|
||||
|
||||
content, err := os.ReadFile(filepath.Join(globalDir, entries[0].Name()))
|
||||
content, err := os.ReadFile(filepath.Join(sharedDir, entries[0].Name()))
|
||||
s.NoError(err)
|
||||
|
||||
s.Contains(string(content), "upstream mybackend")
|
||||
@@ -638,8 +638,8 @@ func (s *ProxyVhostTestSuite) TestProxyWithUpstream() {
|
||||
s.Len(s.vhost.Proxies(), 1)
|
||||
|
||||
// 验证 proxy 配置中引用了 upstream
|
||||
vhostDir := filepath.Join(s.configDir, "vhost")
|
||||
content, err := os.ReadFile(filepath.Join(vhostDir, "200-proxy.conf"))
|
||||
siteDir := filepath.Join(s.configDir, "site")
|
||||
content, err := os.ReadFile(filepath.Join(siteDir, "200-proxy.conf"))
|
||||
s.NoError(err)
|
||||
s.Contains(string(content), "http://api-servers")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user