Skip to content

Commit 909d63b

Browse files
committed
fix: unify symlink handling for site and stream on posix and win
1 parent 5da4136 commit 909d63b

File tree

10 files changed

+59
-18
lines changed

10 files changed

+59
-18
lines changed

api/sites/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func GetSiteList(c *gin.Context) {
6565
originalName := strings.TrimSuffix(name, site.MaintenanceSuffix)
6666
configStatusMap[originalName] = config.StatusMaintenance
6767
} else {
68-
configStatusMap[name] = config.StatusEnabled
68+
configStatusMap[nginx.GetConfNameBySymlinkName(name)] = config.StatusEnabled
6969
}
7070
}
7171

api/streams/streams.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func GetStreams(c *gin.Context) {
5858
enabledConfigMap[file.Name()] = config.StatusDisabled
5959
}
6060
for i := range enabledConfig {
61-
enabledConfigMap[enabledConfig[i].Name()] = config.StatusEnabled
61+
enabledConfigMap[nginx.GetConfNameBySymlinkName(enabledConfig[i].Name())] = config.StatusEnabled
6262
}
6363

6464
var configs []config.Config

internal/nginx/symlink_posix.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !windows
2+
3+
package nginx
4+
5+
func GetConfSymlinkPath(path string) string {
6+
return path
7+
}
8+
9+
func GetConfNameBySymlinkName(name string) string {
10+
return name
11+
}

internal/nginx/symlink_win.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//go:build windows
2+
3+
package nginx
4+
5+
import "strings"
6+
7+
// fix #1046
8+
// nginx.conf include sites-enabled/*.conf
9+
// sites-enabled/example.com.conf -> example.com.conf.conf
10+
11+
// GetConfSymlinkPath returns the path of the symlink file
12+
func GetConfSymlinkPath(path string) string {
13+
return path + ".conf"
14+
}
15+
16+
// GetConfNameBySymlinkName returns the name of the symlink file
17+
func GetConfNameBySymlinkName(name string) string {
18+
return strings.TrimSuffix(name, ".conf")
19+
}

internal/self_check/nginx_conf.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package self_check
33
import (
44
"fmt"
55
"os"
6-
"path/filepath"
7-
"runtime"
86
"strings"
97
"time"
108

@@ -14,15 +12,6 @@ import (
1412
"github.com/tufanbarisyildirim/gonginx/parser"
1513
)
1614

17-
func resolvePath(path ...string) string {
18-
// fix #1046
19-
if runtime.GOOS == "windows" {
20-
return strings.TrimLeft(filepath.ToSlash(strings.ReplaceAll(nginx.GetConfPath(path...), nginx.GetNginxExeDir(), "")), "/")
21-
}
22-
23-
return nginx.GetConfPath(path...)
24-
}
25-
2615
// CheckNginxConfIncludeSites checks if nginx.conf include sites-enabled
2716
func CheckNginxConfIncludeSites() error {
2817
path := nginx.GetConfEntryPath()
@@ -240,7 +229,7 @@ func FixNginxConfIncludeConfD() error {
240229
// add include conf.d/*.conf to http block
241230
includeDirective := &config.Directive{
242231
Name: "include",
243-
Parameters: []config.Parameter{{Value: resolvePath("conf.d/*.conf")}},
232+
Parameters: []config.Parameter{{Value: resolvePath("conf.d/*")}},
244233
}
245234

246235
realBlock := v.GetBlock().(*config.HTTP)
@@ -252,6 +241,6 @@ func FixNginxConfIncludeConfD() error {
252241
}
253242

254243
// if no http block, append http block with include conf.d/*.conf
255-
content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", resolvePath("conf.d/*.conf"))...)
244+
content = append(content, fmt.Appendf(nil, "\nhttp {\n\tinclude %s;\n}\n", resolvePath("conf.d/*"))...)
256245
return os.WriteFile(path, content, 0644)
257246
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !windows
2+
3+
package self_check
4+
5+
import "github.com/0xJacky/Nginx-UI/internal/nginx"
6+
7+
func resolvePath(path ...string) string {
8+
return nginx.GetConfPath(path...)
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build windows
2+
3+
package self_check
4+
5+
import "path/filepath"
6+
7+
// fix #1046
8+
// include conf.d/*.conf
9+
// inclde sites-enabled/*.conf
10+
11+
func resolvePath(path ...string) string {
12+
return filepath.Join(path...) + ".conf"
13+
}

internal/site/enable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
// Enable enables a site by creating a symlink in sites-enabled
1919
func Enable(name string) (err error) {
2020
configFilePath := nginx.GetConfPath("sites-available", name)
21-
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", name)
21+
enabledConfigFilePath := nginx.GetConfSymlinkPath(nginx.GetConfPath("sites-enabled", name))
2222

2323
_, err = os.Stat(configFilePath)
2424
if err != nil {

internal/site/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
// GetSiteStatus returns the status of the site
99
func GetSiteStatus(name string) SiteStatus {
10-
enabledFilePath := nginx.GetConfPath("sites-enabled", name)
10+
enabledFilePath := nginx.GetConfSymlinkPath(nginx.GetConfPath("sites-enabled", name))
1111
if helper.FileExists(enabledFilePath) {
1212
return SiteStatusEnabled
1313
}

internal/stream/enable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
// Enable enables a site by creating a symlink in sites-enabled
1919
func Enable(name string) (err error) {
2020
configFilePath := nginx.GetConfPath("streams-available", name)
21-
enabledConfigFilePath := nginx.GetConfPath("streams-enabled", name)
21+
enabledConfigFilePath := nginx.GetConfSymlinkPath(nginx.GetConfPath("streams-enabled", name))
2222

2323
_, err = os.Stat(configFilePath)
2424
if err != nil {

0 commit comments

Comments
 (0)