Browse Source

fix: unify symlink handling for site and stream on posix and win

Jacky 1 month ago
parent
commit
909d63bdb1

+ 1 - 1
api/sites/list.go

@@ -65,7 +65,7 @@ func GetSiteList(c *gin.Context) {
 			originalName := strings.TrimSuffix(name, site.MaintenanceSuffix)
 			configStatusMap[originalName] = config.StatusMaintenance
 		} else {
-			configStatusMap[name] = config.StatusEnabled
+			configStatusMap[nginx.GetConfNameBySymlinkName(name)] = config.StatusEnabled
 		}
 	}
 

+ 1 - 1
api/streams/streams.go

@@ -58,7 +58,7 @@ func GetStreams(c *gin.Context) {
 		enabledConfigMap[file.Name()] = config.StatusDisabled
 	}
 	for i := range enabledConfig {
-		enabledConfigMap[enabledConfig[i].Name()] = config.StatusEnabled
+		enabledConfigMap[nginx.GetConfNameBySymlinkName(enabledConfig[i].Name())] = config.StatusEnabled
 	}
 
 	var configs []config.Config

+ 11 - 0
internal/nginx/symlink_posix.go

@@ -0,0 +1,11 @@
+//go:build !windows
+
+package nginx
+
+func GetConfSymlinkPath(path string) string {
+	return path
+}
+
+func GetConfNameBySymlinkName(name string) string {
+	return name
+}

+ 19 - 0
internal/nginx/symlink_win.go

@@ -0,0 +1,19 @@
+//go:build windows
+
+package nginx
+
+import "strings"
+
+// fix #1046
+// nginx.conf include sites-enabled/*.conf
+// sites-enabled/example.com.conf -> example.com.conf.conf
+
+// GetConfSymlinkPath returns the path of the symlink file
+func GetConfSymlinkPath(path string) string {
+	return path + ".conf"
+}
+
+// GetConfNameBySymlinkName returns the name of the symlink file
+func GetConfNameBySymlinkName(name string) string {
+	return strings.TrimSuffix(name, ".conf")
+}

+ 2 - 13
internal/self_check/nginx_conf.go

@@ -3,8 +3,6 @@ package self_check
 import (
 	"fmt"
 	"os"
-	"path/filepath"
-	"runtime"
 	"strings"
 	"time"
 
@@ -14,15 +12,6 @@ import (
 	"github.com/tufanbarisyildirim/gonginx/parser"
 )
 
-func resolvePath(path ...string) string {
-	// fix #1046
-	if runtime.GOOS == "windows" {
-		return strings.TrimLeft(filepath.ToSlash(strings.ReplaceAll(nginx.GetConfPath(path...), nginx.GetNginxExeDir(), "")), "/")
-	}
-
-	return nginx.GetConfPath(path...)
-}
-
 // CheckNginxConfIncludeSites checks if nginx.conf include sites-enabled
 func CheckNginxConfIncludeSites() error {
 	path := nginx.GetConfEntryPath()
@@ -240,7 +229,7 @@ func FixNginxConfIncludeConfD() error {
 			// add include conf.d/*.conf to http block
 			includeDirective := &config.Directive{
 				Name:       "include",
-				Parameters: []config.Parameter{{Value: resolvePath("conf.d/*.conf")}},
+				Parameters: []config.Parameter{{Value: resolvePath("conf.d/*")}},
 			}
 
 			realBlock := v.GetBlock().(*config.HTTP)
@@ -252,6 +241,6 @@ func FixNginxConfIncludeConfD() error {
 	}
 
 	// if no http block, append http block with include conf.d/*.conf
-	content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", resolvePath("conf.d/*.conf"))...)
+	content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", resolvePath("conf.d/*"))...)
 	return os.WriteFile(path, content, 0644)
 }

+ 9 - 0
internal/self_check/resolve_path_posix.go

@@ -0,0 +1,9 @@
+//go:build !windows
+
+package self_check
+
+import "github.com/0xJacky/Nginx-UI/internal/nginx"
+
+func resolvePath(path ...string) string {
+	return nginx.GetConfPath(path...)
+}

+ 13 - 0
internal/self_check/resolve_path_win.go

@@ -0,0 +1,13 @@
+//go:build windows
+
+package self_check
+
+import "path/filepath"
+
+// fix #1046
+// include conf.d/*.conf
+// inclde sites-enabled/*.conf
+
+func resolvePath(path ...string) string {
+	return filepath.Join(path...) + ".conf"
+}

+ 1 - 1
internal/site/enable.go

@@ -18,7 +18,7 @@ import (
 // Enable enables a site by creating a symlink in sites-enabled
 func Enable(name string) (err error) {
 	configFilePath := nginx.GetConfPath("sites-available", name)
-	enabledConfigFilePath := nginx.GetConfPath("sites-enabled", name)
+	enabledConfigFilePath := nginx.GetConfSymlinkPath(nginx.GetConfPath("sites-enabled", name))
 
 	_, err = os.Stat(configFilePath)
 	if err != nil {

+ 1 - 1
internal/site/status.go

@@ -7,7 +7,7 @@ import (
 
 // GetSiteStatus returns the status of the site
 func GetSiteStatus(name string) SiteStatus {
-	enabledFilePath := nginx.GetConfPath("sites-enabled", name)
+	enabledFilePath := nginx.GetConfSymlinkPath(nginx.GetConfPath("sites-enabled", name))
 	if helper.FileExists(enabledFilePath) {
 		return SiteStatusEnabled
 	}

+ 1 - 1
internal/stream/enable.go

@@ -18,7 +18,7 @@ import (
 // Enable enables a site by creating a symlink in sites-enabled
 func Enable(name string) (err error) {
 	configFilePath := nginx.GetConfPath("streams-available", name)
-	enabledConfigFilePath := nginx.GetConfPath("streams-enabled", name)
+	enabledConfigFilePath := nginx.GetConfSymlinkPath(nginx.GetConfPath("streams-enabled", name))
 
 	_, err = os.Stat(configFilePath)
 	if err != nil {