fix: fail to control nginx

This commit is contained in:
0xJacky 2023-12-06 18:31:03 +08:00
parent fcf789e271
commit b6cbc34e6f
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
5 changed files with 27 additions and 13 deletions

View file

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

View file

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

View file

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

View file

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

View file

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