Browse Source

enhance: nginx control

0xJacky 1 year ago
parent
commit
f7d9f2564f

+ 1 - 1
api/nginx/nginx.go

@@ -51,7 +51,7 @@ func FormatNginxConfig(c *gin.Context) {
 }
 
 func Status(c *gin.Context) {
-	pidPath := nginx.GetNginxPIDPath()
+	pidPath := nginx.GetPIDPath()
 
 	running := true
 	if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 { // fileInfo.Size() == 0 no process id

+ 1 - 1
api/nginx/router.go

@@ -14,5 +14,5 @@ func InitRouter(r *gin.RouterGroup) {
 }
 
 func InitNginxLogRouter(r *gin.RouterGroup) {
-	r.GET("nginx_log", NginxLog)
+	r.GET("nginx_log", Log)
 }

+ 1 - 1
app/src/api/ngx.ts

@@ -46,7 +46,7 @@ const ngx = {
     return http.post('/ngx/format_code', { content })
   },
 
-  status() {
+  status(): Promise<{ running: boolean }> {
     return http.get('/nginx/status')
   },
 

+ 11 - 13
app/src/components/NginxControl/NginxControl.vue

@@ -4,20 +4,22 @@ import { ReloadOutlined } from '@ant-design/icons-vue'
 import gettext from '@/gettext'
 import ngx from '@/api/ngx'
 import { logLevel } from '@/views/config/constants'
+import { NginxStatus } from '@/constants'
 
 const { $gettext } = gettext
+
 const status = ref(0)
 function get_status() {
   ngx.status().then(r => {
     if (r?.running === true)
-      status.value = 0
+      status.value = NginxStatus.Running
     else
-      status.value = -1
+      status.value = NginxStatus.Stopped
   })
 }
 
 function reload_nginx() {
-  status.value = 1
+  status.value = NginxStatus.Reloading
   ngx.reload().then(r => {
     if (r.level < logLevel.Warn)
       message.success($gettext('Nginx reloaded successfully'))
@@ -27,13 +29,11 @@ function reload_nginx() {
       message.error(r.message)
   }).catch(e => {
     message.error(`${$gettext('Server error')} ${e?.message}`)
-  }).finally(() => {
-    status.value = 0
-  })
+  }).finally(() => get_status())
 }
 
 function restart_nginx() {
-  status.value = 2
+  status.value = NginxStatus.Restarting
   ngx.restart().then(r => {
     if (r.level < logLevel.Warn)
       message.success($gettext('Nginx restarted successfully'))
@@ -43,9 +43,7 @@ function restart_nginx() {
       message.error(r.message)
   }).catch(e => {
     message.error(`${$gettext('Server error')} ${e?.message}`)
-  }).finally(() => {
-    status.value = 0
-  })
+  }).finally(() => get_status())
 }
 
 const visible = ref(false)
@@ -66,17 +64,17 @@ watch(visible, v => {
       <div class="content-wrapper">
         <h4>{{ $gettext('Nginx Control') }}</h4>
         <ABadge
-          v-if="status === 0"
+          v-if="status === NginxStatus.Running"
           color="green"
           :text="$gettext('Running')"
         />
         <ABadge
-          v-else-if="status === 1"
+          v-else-if="status === NginxStatus.Reloading"
           color="blue"
           :text="$gettext('Reloading')"
         />
         <ABadge
-          v-else-if="status === 2"
+          v-else-if="status === NginxStatus.Restarting"
           color="orange"
           :text="$gettext('Restarting')"
         />

+ 7 - 0
app/src/constants/index.ts

@@ -19,3 +19,10 @@ export const NotificationType = {
   [NotificationTypeT.Info]: () => $gettext('Info'),
   [NotificationTypeT.Success]: () => $gettext('Success'),
 } as const
+
+export enum NginxStatus {
+  Running,
+  Reloading,
+  Restarting,
+  Stopped,
+}

+ 2 - 2
app/src/layouts/HeaderLayout.vue

@@ -42,12 +42,12 @@ function logout() {
 
       <Notification />
 
+      <NginxControl />
+
       <a href="/">
         <HomeOutlined />
       </a>
 
-      <NginxControl />
-
       <a @click="logout">
         <LogoutOutlined />
       </a>

+ 97 - 0
internal/nginx/config_args.go

@@ -0,0 +1,97 @@
+package nginx
+
+import (
+	"github.com/0xJacky/Nginx-UI/internal/logger"
+	"github.com/0xJacky/Nginx-UI/settings"
+	"os/exec"
+	"path/filepath"
+	"regexp"
+)
+
+func getNginxV() string {
+	out, err := exec.Command("nginx", "-V").CombinedOutput()
+	if err != nil {
+		logger.Error(err)
+		return ""
+	}
+	return string(out)
+}
+
+func GetConfPath(dir ...string) (confPath string) {
+	if settings.NginxSettings.ConfigDir == "" {
+		out := getNginxV()
+		r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
+		match := r.FindStringSubmatch(out)
+		if len(match) < 1 {
+			logger.Error("nginx.GetConfPath len(match) < 1")
+			return ""
+		}
+		confPath = match[1]
+	} else {
+		confPath = settings.NginxSettings.ConfigDir
+	}
+
+	return filepath.Join(confPath, filepath.Join(dir...))
+}
+
+func GetPIDPath() (path string) {
+	if settings.NginxSettings.PIDPath == "" {
+		out := getNginxV()
+		r, _ := regexp.Compile("--pid-path=(.*.pid)")
+		match := r.FindStringSubmatch(out)
+		if len(match) < 1 {
+			logger.Error("nginx.GetPIDPath len(match) < 1")
+			return ""
+		}
+		path = match[1]
+	} else {
+		path = settings.NginxSettings.PIDPath
+	}
+
+	return path
+}
+
+func GetSbinPath() (path string) {
+	out := getNginxV()
+	r, _ := regexp.Compile("--sbin-path=(\\S+)")
+	match := r.FindStringSubmatch(out)
+	if len(match) < 1 {
+		logger.Error("nginx.GetPIDPath len(match) < 1")
+		return ""
+	}
+	path = match[1]
+
+	return path
+}
+
+func GetAccessLogPath() (path string) {
+	if settings.NginxSettings.AccessLogPath == "" {
+		out := getNginxV()
+		r, _ := regexp.Compile("--http-log-path=(\\S+)")
+		match := r.FindStringSubmatch(out)
+		if len(match) < 1 {
+			logger.Error("nginx.GetAccessLogPath len(match) < 1")
+			return ""
+		}
+		path = match[1]
+	} else {
+		path = settings.NginxSettings.PIDPath
+	}
+
+	return path
+}
+
+func GetErrorLogPath() string {
+	if settings.NginxSettings.ErrorLogPath == "" {
+		out := getNginxV()
+		r, _ := regexp.Compile("--error-log-path=(\\S+)")
+		match := r.FindStringSubmatch(out)
+		if len(match) < 1 {
+			logger.Error("nginx.GetErrorLogPath len(match) < 1")
+			return ""
+		}
+		return match[1]
+	} else {
+		return settings.NginxSettings.ErrorLogPath
+	}
+}

+ 8 - 48
internal/nginx/nginx.go

@@ -1,11 +1,8 @@
 package nginx
 
 import (
-	"github.com/0xJacky/Nginx-UI/internal/logger"
 	"github.com/0xJacky/Nginx-UI/settings"
 	"os/exec"
-	"path/filepath"
-	"regexp"
 )
 
 func execShell(cmd string) (out string) {
@@ -56,55 +53,18 @@ func Restart() (out string) {
 		return
 	}
 
-	out = execCommand("nginx", "-s", "stop")
+	pidPath := GetPIDPath()
+	daemon := GetSbinPath()
 
-	out += execCommand("nginx")
+	out = execCommand("start-stop-daemon", "--stop", "--quiet", "--oknodo", "--retry=TERM/30/KILL/5", "--pidfile", pidPath)
 
-	return
-}
+	if daemon == "" {
+		out += execCommand("nginx")
 
-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
 	}
 
-	return filepath.Join(confPath, filepath.Join(dir...))
-}
+	out += execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon)
 
-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
+	return
 }