Browse Source

fix: nginx control failure

Jacky 1 year ago
parent
commit
7c6b4fec91

+ 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}

+ 3 - 0
app/src/views/domain/DomainList.vue

@@ -57,6 +57,7 @@ function enable(name: string) {
   domain.enable(name).then(() => {
     message.success($gettext('Enabled successfully'))
     table.value?.get_list()
+    inspect_config.value?.test()
   }).catch(r => {
     message.error($gettext('Failed to enable %{msg}', { msg: r.message ?? '' }), 10)
   })
@@ -66,6 +67,7 @@ function disable(name: string) {
   domain.disable(name).then(() => {
     message.success($gettext('Disabled successfully'))
     table.value?.get_list()
+    inspect_config.value?.test()
   }).catch(r => {
     message.error($gettext('Failed to disable %{msg}', { msg: r.message ?? '' }))
   })
@@ -75,6 +77,7 @@ function destroy(site_name: string) {
   domain.destroy(site_name).then(() => {
     table.value.get_list()
     message.success($gettext('Delete site: %{site_name}', { site_name }))
+    inspect_config.value?.test()
   }).catch(e => {
     message.error(e?.message ?? $gettext('Server error'))
   })

+ 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}

+ 7 - 4
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
@@ -20,11 +21,13 @@ var logLevel = [...]string{
 	"debug", "info", "notice", "warn", "error", "crit", "alert", "emerg",
 }
 
-func GetLogLevel(output string) int {
+func GetLogLevel(output string) (level int) {
+	level = -1
 	for k, v := range logLevel {
 		if strings.Contains(output, v) {
-			return k
+			// Try to find the highest log level
+			level = k
 		}
 	}
-	return -1
+	return
 }

+ 15 - 4
internal/nginx/nginx.go

@@ -9,7 +9,16 @@ import (
 )
 
 func execShell(cmd string) (out string) {
-	bytes, err := exec.Command("/bin/sh -c", cmd).CombinedOutput()
+	bytes, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
+	out = string(bytes)
+	if err != nil {
+		out += " " + err.Error()
+	}
+	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()
@@ -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,9 @@ func Restart() (out string) {
 		return
 	}
 
-	out = execShell("nginx -s reopen")
+	out = execCommand("nginx", "-s", "stop")
+
+	out += execCommand("nginx")
 
 	return
 }