enhance: nginx control

This commit is contained in:
0xJacky 2023-12-06 22:56:36 +08:00
parent 30f75ff7cd
commit f7d9f2564f
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
8 changed files with 131 additions and 69 deletions

View file

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

View file

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

View file

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

View file

@ -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')"
/>

View file

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

View file

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

View file

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

View file

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