diff --git a/api/nginx/nginx.go b/api/nginx/nginx.go index 1a492056..ae42d884 100644 --- a/api/nginx/nginx.go +++ b/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 diff --git a/api/nginx/router.go b/api/nginx/router.go index 899187c7..a1869008 100644 --- a/api/nginx/router.go +++ b/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) } diff --git a/app/src/api/ngx.ts b/app/src/api/ngx.ts index 6b8f8b12..bb76cbb6 100644 --- a/app/src/api/ngx.ts +++ b/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') }, diff --git a/app/src/components/NginxControl/NginxControl.vue b/app/src/components/NginxControl/NginxControl.vue index 706718b6..bda745ca 100644 --- a/app/src/components/NginxControl/NginxControl.vue +++ b/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 => {

{{ $gettext('Nginx Control') }}

diff --git a/app/src/constants/index.ts b/app/src/constants/index.ts index fbd8bee4..be3081fe 100644 --- a/app/src/constants/index.ts +++ b/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, +} diff --git a/app/src/layouts/HeaderLayout.vue b/app/src/layouts/HeaderLayout.vue index 2179c8ad..e2d6b75b 100644 --- a/app/src/layouts/HeaderLayout.vue +++ b/app/src/layouts/HeaderLayout.vue @@ -42,12 +42,12 @@ function logout() { + + - - diff --git a/internal/nginx/config_args.go b/internal/nginx/config_args.go new file mode 100644 index 00000000..700dd130 --- /dev/null +++ b/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 + } +} diff --git a/internal/nginx/nginx.go b/internal/nginx/nginx.go index d69a3607..fe1c95e9 100644 --- a/internal/nginx/nginx.go +++ b/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) + + if daemon == "" { + out += execCommand("nginx") + + return + } + + out += execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon) 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...)) -} - -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 -}