|
@@ -1,102 +1,99 @@
|
|
|
package nginx
|
|
|
|
|
|
import (
|
|
|
- "github.com/0xJacky/Nginx-UI/server/internal/logger"
|
|
|
- "github.com/0xJacky/Nginx-UI/server/settings"
|
|
|
- "os/exec"
|
|
|
- "path/filepath"
|
|
|
- "regexp"
|
|
|
+ "github.com/0xJacky/Nginx-UI/server/internal/logger"
|
|
|
+ "github.com/0xJacky/Nginx-UI/server/settings"
|
|
|
+ "os/exec"
|
|
|
+ "path/filepath"
|
|
|
+ "regexp"
|
|
|
)
|
|
|
|
|
|
-func execShell(cmdArgs ...string) (out string) {
|
|
|
- cmd := []string{"-c"}
|
|
|
- cmd = append(cmd, cmdArgs...)
|
|
|
-
|
|
|
- bytes, err := exec.Command("/bin/sh", cmd...).CombinedOutput()
|
|
|
- out = string(bytes)
|
|
|
- if err != nil {
|
|
|
- out += " " + err.Error()
|
|
|
- }
|
|
|
- return
|
|
|
+func execShell(cmd string) (out string) {
|
|
|
+ bytes, err := exec.Command("/bin/sh -c", cmd).CombinedOutput()
|
|
|
+ out = string(bytes)
|
|
|
+ if err != nil {
|
|
|
+ out += " " + err.Error()
|
|
|
+ }
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
func TestConf() (out string) {
|
|
|
- if settings.NginxSettings.TestConfigCmd != "" {
|
|
|
- out = execShell(settings.NginxSettings.TestConfigCmd)
|
|
|
+ if settings.NginxSettings.TestConfigCmd != "" {
|
|
|
+ out = execShell(settings.NginxSettings.TestConfigCmd)
|
|
|
|
|
|
- return
|
|
|
- }
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- out = execShell("nginx", "-t")
|
|
|
+ out = execShell("nginx -t")
|
|
|
|
|
|
- return
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
func Reload() (out string) {
|
|
|
- if settings.NginxSettings.ReloadCmd != "" {
|
|
|
- out = execShell(settings.NginxSettings.ReloadCmd)
|
|
|
- return
|
|
|
- }
|
|
|
+ if settings.NginxSettings.ReloadCmd != "" {
|
|
|
+ out = execShell(settings.NginxSettings.ReloadCmd)
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- out = execShell("nginx", "-s", "reload")
|
|
|
+ out = execShell("nginx -s reload")
|
|
|
|
|
|
- return
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
func Restart() (out string) {
|
|
|
- if settings.NginxSettings.RestartCmd != "" {
|
|
|
- out = execShell(settings.NginxSettings.RestartCmd)
|
|
|
+ if settings.NginxSettings.RestartCmd != "" {
|
|
|
+ out = execShell(settings.NginxSettings.RestartCmd)
|
|
|
|
|
|
- return
|
|
|
- }
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- out = execShell("nginx", "-s", "reopen")
|
|
|
+ out = execShell("nginx -s reopen")
|
|
|
|
|
|
- return
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
func GetConfPath(dir ...string) string {
|
|
|
- var confPath string
|
|
|
-
|
|
|
- if settings.NginxSettings.ConfigDir == "" {
|
|
|
- out, err := exec.Command("nginx", "-V").CombinedOutput()
|
|
|
- if err != nil {
|
|
|
- logger.Error(err)
|
|
|
- return ""
|
|
|
- }
|
|
|
- r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
|
|
|
- match := r.FindStringSubmatch(string(out))
|
|
|
- if len(match) < 1 {
|
|
|
- logger.Error("nginx.GetConfPath len(match) < 1")
|
|
|
- return ""
|
|
|
- }
|
|
|
- confPath = r.FindStringSubmatch(string(out))[1]
|
|
|
- } else {
|
|
|
- confPath = settings.NginxSettings.ConfigDir
|
|
|
- }
|
|
|
-
|
|
|
- return filepath.Join(confPath, filepath.Join(dir...))
|
|
|
+ var confPath string
|
|
|
+
|
|
|
+ if settings.NginxSettings.ConfigDir == "" {
|
|
|
+ out, err := exec.Command("nginx", "-V").CombinedOutput()
|
|
|
+ if err != nil {
|
|
|
+ logger.Error(err)
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
|
|
|
+ match := r.FindStringSubmatch(string(out))
|
|
|
+ if len(match) < 1 {
|
|
|
+ logger.Error("nginx.GetConfPath len(match) < 1")
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ confPath = r.FindStringSubmatch(string(out))[1]
|
|
|
+ } else {
|
|
|
+ confPath = settings.NginxSettings.ConfigDir
|
|
|
+ }
|
|
|
+
|
|
|
+ return filepath.Join(confPath, filepath.Join(dir...))
|
|
|
}
|
|
|
|
|
|
func GetNginxPIDPath() string {
|
|
|
- var confPath string
|
|
|
-
|
|
|
- if settings.NginxSettings.PIDPath == "" {
|
|
|
- out, err := exec.Command("nginx", "-V").CombinedOutput()
|
|
|
- if err != nil {
|
|
|
- logger.Error(err)
|
|
|
- return ""
|
|
|
- }
|
|
|
- r, _ := regexp.Compile("--pid-path=(.*.pid)")
|
|
|
- match := r.FindStringSubmatch(string(out))
|
|
|
- if len(match) < 1 {
|
|
|
- logger.Error("nginx.GetNginxPIDPath len(match) < 1")
|
|
|
- return ""
|
|
|
- }
|
|
|
- confPath = r.FindStringSubmatch(string(out))[1]
|
|
|
- } else {
|
|
|
- confPath = settings.NginxSettings.PIDPath
|
|
|
- }
|
|
|
-
|
|
|
- return confPath
|
|
|
+ var confPath string
|
|
|
+
|
|
|
+ if settings.NginxSettings.PIDPath == "" {
|
|
|
+ out, err := exec.Command("nginx", "-V").CombinedOutput()
|
|
|
+ if err != nil {
|
|
|
+ logger.Error(err)
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ r, _ := regexp.Compile("--pid-path=(.*.pid)")
|
|
|
+ match := r.FindStringSubmatch(string(out))
|
|
|
+ if len(match) < 1 {
|
|
|
+ logger.Error("nginx.GetNginxPIDPath len(match) < 1")
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ confPath = r.FindStringSubmatch(string(out))[1]
|
|
|
+ } else {
|
|
|
+ confPath = settings.NginxSettings.PIDPath
|
|
|
+ }
|
|
|
+
|
|
|
+ return confPath
|
|
|
}
|