mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
enhance(nginx): executable path resolution and add support for relative paths on Windows
This commit is contained in:
parent
0506098647
commit
06121ef515
2 changed files with 85 additions and 37 deletions
|
@ -1,16 +1,47 @@
|
||||||
package nginx
|
package nginx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
|
||||||
"github.com/0xJacky/Nginx-UI/settings"
|
|
||||||
"github.com/uozi-tech/cosy/logger"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||||
|
"github.com/0xJacky/Nginx-UI/settings"
|
||||||
|
"github.com/uozi-tech/cosy/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var nginxExePath string
|
||||||
|
|
||||||
|
// Returns the path to the nginx executable
|
||||||
|
func getNginxExePath() string {
|
||||||
|
if nginxExePath != "" {
|
||||||
|
return nginxExePath
|
||||||
|
}
|
||||||
|
|
||||||
|
var path string
|
||||||
|
var err error
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
path, err = exec.LookPath("nginx.exe")
|
||||||
|
} else {
|
||||||
|
path, err = exec.LookPath("nginx")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
nginxExePath = path
|
||||||
|
return nginxExePath
|
||||||
|
}
|
||||||
|
return nginxExePath
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the directory containing the nginx executable
|
||||||
|
func getNginxExeDir() string {
|
||||||
|
return filepath.Dir(getNginxExePath())
|
||||||
|
}
|
||||||
|
|
||||||
func getNginxV() string {
|
func getNginxV() string {
|
||||||
out, err := exec.Command("nginx", "-V").CombinedOutput()
|
exePath := getNginxExePath()
|
||||||
|
out, err := exec.Command(exePath, "-V").CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
return ""
|
return ""
|
||||||
|
@ -18,6 +49,20 @@ func getNginxV() string {
|
||||||
return string(out)
|
return string(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Resolves relative paths by joining them with the nginx executable directory on Windows
|
||||||
|
func resolvePath(path string) string {
|
||||||
|
if path == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle relative paths on Windows
|
||||||
|
if runtime.GOOS == "windows" && !filepath.IsAbs(path) {
|
||||||
|
return filepath.Join(getNginxExeDir(), path)
|
||||||
|
}
|
||||||
|
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
func GetConfPath(dir ...string) (confPath string) {
|
func GetConfPath(dir ...string) (confPath string) {
|
||||||
if settings.NginxSettings.ConfigDir == "" {
|
if settings.NginxSettings.ConfigDir == "" {
|
||||||
out := getNginxV()
|
out := getNginxV()
|
||||||
|
@ -32,6 +77,8 @@ func GetConfPath(dir ...string) (confPath string) {
|
||||||
confPath = settings.NginxSettings.ConfigDir
|
confPath = settings.NginxSettings.ConfigDir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
confPath = resolvePath(confPath)
|
||||||
|
|
||||||
joined := filepath.Clean(filepath.Join(confPath, filepath.Join(dir...)))
|
joined := filepath.Clean(filepath.Join(confPath, filepath.Join(dir...)))
|
||||||
if !helper.IsUnderDirectory(joined, confPath) {
|
if !helper.IsUnderDirectory(joined, confPath) {
|
||||||
return confPath
|
return confPath
|
||||||
|
@ -53,7 +100,7 @@ func GetConfEntryPath() (path string) {
|
||||||
path = settings.NginxSettings.ConfigPath
|
path = settings.NginxSettings.ConfigPath
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return resolvePath(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPIDPath() (path string) {
|
func GetPIDPath() (path string) {
|
||||||
|
@ -70,7 +117,7 @@ func GetPIDPath() (path string) {
|
||||||
path = settings.NginxSettings.PIDPath
|
path = settings.NginxSettings.PIDPath
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return resolvePath(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSbinPath() (path string) {
|
func GetSbinPath() (path string) {
|
||||||
|
@ -83,7 +130,7 @@ func GetSbinPath() (path string) {
|
||||||
}
|
}
|
||||||
path = match[1]
|
path = match[1]
|
||||||
|
|
||||||
return
|
return resolvePath(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAccessLogPath() (path string) {
|
func GetAccessLogPath() (path string) {
|
||||||
|
@ -100,7 +147,7 @@ func GetAccessLogPath() (path string) {
|
||||||
path = settings.NginxSettings.AccessLogPath
|
path = settings.NginxSettings.AccessLogPath
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return resolvePath(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetErrorLogPath() string {
|
func GetErrorLogPath() string {
|
||||||
|
@ -112,8 +159,36 @@ func GetErrorLogPath() string {
|
||||||
logger.Error("nginx.GetErrorLogPath len(match) < 1")
|
logger.Error("nginx.GetErrorLogPath len(match) < 1")
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return match[1]
|
return resolvePath(match[1])
|
||||||
} else {
|
} else {
|
||||||
return settings.NginxSettings.ErrorLogPath
|
return resolvePath(settings.NginxSettings.ErrorLogPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetModulesPath returns the nginx modules path
|
||||||
|
func GetModulesPath() string {
|
||||||
|
// First try to get from nginx -V output
|
||||||
|
stdOut, stdErr := execCommand(getNginxExePath(), "-V")
|
||||||
|
if stdErr != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if stdOut != "" {
|
||||||
|
// Look for --modules-path in the output
|
||||||
|
if strings.Contains(stdOut, "--modules-path=") {
|
||||||
|
parts := strings.Split(stdOut, "--modules-path=")
|
||||||
|
if len(parts) > 1 {
|
||||||
|
// Extract the path
|
||||||
|
path := strings.Split(parts[1], " ")[0]
|
||||||
|
// Remove quotes if present
|
||||||
|
path = strings.Trim(path, "\"")
|
||||||
|
return resolvePath(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default path if not found
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return resolvePath("modules")
|
||||||
|
}
|
||||||
|
return resolvePath("/usr/lib/nginx/modules")
|
||||||
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package nginx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -63,7 +62,6 @@ func Restart() {
|
||||||
}
|
}
|
||||||
|
|
||||||
lastStdOut, lastStdErr = execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon)
|
lastStdOut, lastStdErr = execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLastOutput returns the last output of the nginx command
|
// GetLastOutput returns the last output of the nginx command
|
||||||
|
@ -73,31 +71,6 @@ func GetLastOutput() (stdOut string, stdErr error) {
|
||||||
return lastStdOut, lastStdErr
|
return lastStdOut, lastStdErr
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetModulesPath returns the nginx modules path
|
|
||||||
func GetModulesPath() string {
|
|
||||||
// First try to get from nginx -V output
|
|
||||||
stdOut, stdErr := execCommand("nginx", "-V")
|
|
||||||
if stdErr != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
if stdOut != "" {
|
|
||||||
// Look for --modules-path in the output
|
|
||||||
if strings.Contains(stdOut, "--modules-path=") {
|
|
||||||
parts := strings.Split(stdOut, "--modules-path=")
|
|
||||||
if len(parts) > 1 {
|
|
||||||
// Extract the path
|
|
||||||
path := strings.Split(parts[1], " ")[0]
|
|
||||||
// Remove quotes if present
|
|
||||||
path = strings.Trim(path, "\"")
|
|
||||||
return path
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default path if not found
|
|
||||||
return "/usr/lib/nginx/modules"
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsNginxRunning() bool {
|
func IsNginxRunning() bool {
|
||||||
pidPath := GetPIDPath()
|
pidPath := GetPIDPath()
|
||||||
switch settings.NginxSettings.RunningInAnotherContainer() {
|
switch settings.NginxSettings.RunningInAnotherContainer() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue