Browse Source

fix: fail to control nginx

0xJacky 1 year ago
parent
commit
b6cbc34e6f
5 changed files with 27 additions and 13 deletions
  1. 7 7
      api/nginx/control.go
  2. 1 1
      app/src/version.json
  3. 1 1
      app/version.json
  4. 2 1
      internal/nginx/log.go
  5. 16 3
      internal/nginx/nginx.go

+ 7 - 7
api/nginx/control.go

@@ -1,31 +1,31 @@
 package nginx
 
 import (
-	nginx2 "github.com/0xJacky/Nginx-UI/internal/nginx"
+	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/gin-gonic/gin"
 	"net/http"
 )
 
 func Reload(c *gin.Context) {
-	output := nginx2.Reload()
+	output := nginx.Reload()
 	c.JSON(http.StatusOK, gin.H{
 		"message": output,
-		"level":   nginx2.GetLogLevel(output),
+		"level":   nginx.GetLogLevel(output),
 	})
 }
 
 func Test(c *gin.Context) {
-	output := nginx2.TestConf()
+	output := nginx.TestConf()
 	c.JSON(http.StatusOK, gin.H{
 		"message": output,
-		"level":   nginx2.GetLogLevel(output),
+		"level":   nginx.GetLogLevel(output),
 	})
 }
 
 func Restart(c *gin.Context) {
-	output := nginx2.Restart()
+	output := nginx.Restart()
 	c.JSON(http.StatusOK, gin.H{
 		"message": output,
-		"level":   nginx2.GetLogLevel(output),
+		"level":   nginx.GetLogLevel(output),
 	})
 }

+ 1 - 1
app/src/version.json

@@ -1 +1 @@
-{"version":"2.0.0-beta.5","build_id":74,"total_build":278}
+{"version":"2.0.0-beta.5","build_id":75,"total_build":279}

+ 1 - 1
app/version.json

@@ -1 +1 @@
-{"version":"2.0.0-beta.5","build_id":74,"total_build":278}
+{"version":"2.0.0-beta.5","build_id":75,"total_build":279}

+ 2 - 1
internal/nginx/log.go

@@ -6,7 +6,8 @@ import "strings"
 // nginx log level: debug, info, notice, warn, error, crit, alert, or emerg
 
 const (
-	Debug = iota
+	Unknown = -1
+	Debug   = iota
 	Info
 	Notice
 	Warn

+ 16 - 3
internal/nginx/nginx.go

@@ -17,6 +17,15 @@ func execShell(cmd string) (out string) {
 	return
 }
 
+func execCommand(name string, cmd ...string) (out string) {
+	bytes, err := exec.Command(name, 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)
@@ -24,7 +33,7 @@ func TestConf() (out string) {
 		return
 	}
 
-	out = execShell("nginx -t")
+	out = execCommand("nginx", "-t")
 
 	return
 }
@@ -35,7 +44,7 @@ func Reload() (out string) {
 		return
 	}
 
-	out = execShell("nginx -s reload")
+	out = execCommand("nginx", "-s", "reload")
 
 	return
 }
@@ -47,7 +56,11 @@ func Restart() (out string) {
 		return
 	}
 
-	out = execShell("nginx -s reopen")
+	out = execCommand("nginx", "-s", "stop")
+
+	out += execCommand("nginx")
+
+	logger.Debug(out)
 
 	return
 }