mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
fix(docker): nginx restart always output network error
This commit is contained in:
parent
070c0b4620
commit
45a68112b1
5 changed files with 113 additions and 94 deletions
|
@ -4,6 +4,7 @@ import (
|
||||||
"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"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Reload(c *gin.Context) {
|
func Reload(c *gin.Context) {
|
||||||
|
@ -23,9 +24,24 @@ func Test(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Restart(c *gin.Context) {
|
func Restart(c *gin.Context) {
|
||||||
output := nginx.Restart()
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": output,
|
"message": "ok",
|
||||||
"level": nginx.GetLogLevel(output),
|
})
|
||||||
|
go nginx.Restart()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Status(c *gin.Context) {
|
||||||
|
pidPath := nginx.GetPIDPath()
|
||||||
|
lastOutput := nginx.GetLastOutput()
|
||||||
|
|
||||||
|
running := true
|
||||||
|
if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 { // fileInfo.Size() == 0 no process id
|
||||||
|
running = false
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"running": running,
|
||||||
|
"message": lastOutput,
|
||||||
|
"level": nginx.GetLogLevel(lastOutput),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,73 +1,59 @@
|
||||||
package nginx
|
package nginx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/0xJacky/Nginx-UI/api"
|
"github.com/0xJacky/Nginx-UI/api"
|
||||||
"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"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func BuildNginxConfig(c *gin.Context) {
|
func BuildNginxConfig(c *gin.Context) {
|
||||||
var ngxConf nginx.NgxConfig
|
var ngxConf nginx.NgxConfig
|
||||||
if !api.BindAndValid(c, &ngxConf) {
|
if !api.BindAndValid(c, &ngxConf) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
content, err := ngxConf.BuildConfig()
|
content, err := ngxConf.BuildConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.ErrHandler(c, err)
|
api.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"content": content,
|
"content": content,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TokenizeNginxConfig(c *gin.Context) {
|
func TokenizeNginxConfig(c *gin.Context) {
|
||||||
var json struct {
|
var json struct {
|
||||||
Content string `json:"content" binding:"required"`
|
Content string `json:"content" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if !api.BindAndValid(c, &json) {
|
if !api.BindAndValid(c, &json) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ngxConfig, err := nginx.ParseNgxConfigByContent(json.Content)
|
ngxConfig, err := nginx.ParseNgxConfigByContent(json.Content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.ErrHandler(c, err)
|
api.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, ngxConfig)
|
c.JSON(http.StatusOK, ngxConfig)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FormatNginxConfig(c *gin.Context) {
|
func FormatNginxConfig(c *gin.Context) {
|
||||||
var json struct {
|
var json struct {
|
||||||
Content string `json:"content" binding:"required"`
|
Content string `json:"content" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if !api.BindAndValid(c, &json) {
|
if !api.BindAndValid(c, &json) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
content, err := nginx.FmtCode(json.Content)
|
content, err := nginx.FmtCode(json.Content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.ErrHandler(c, err)
|
api.ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"content": content,
|
"content": content,
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
func Status(c *gin.Context) {
|
|
||||||
pidPath := nginx.GetPIDPath()
|
|
||||||
|
|
||||||
running := true
|
|
||||||
if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 { // fileInfo.Size() == 0 no process id
|
|
||||||
running = false
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
|
||||||
"running": running,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ const ngx = {
|
||||||
return http.post('/ngx/format_code', { content })
|
return http.post('/ngx/format_code', { content })
|
||||||
},
|
},
|
||||||
|
|
||||||
status(): Promise<{ running: boolean }> {
|
status(): Promise<{ running: boolean; message: string; level: number }> {
|
||||||
return http.get('/nginx/status')
|
return http.get('/nginx/status')
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -6,13 +6,14 @@ import { logLevel } from '@/views/config/constants'
|
||||||
import { NginxStatus } from '@/constants'
|
import { NginxStatus } from '@/constants'
|
||||||
|
|
||||||
const status = ref(0)
|
const status = ref(0)
|
||||||
function get_status() {
|
async function get_status() {
|
||||||
ngx.status().then(r => {
|
const r = await ngx.status()
|
||||||
if (r?.running === true)
|
if (r?.running === true)
|
||||||
status.value = NginxStatus.Running
|
status.value = NginxStatus.Running
|
||||||
else
|
else
|
||||||
status.value = NginxStatus.Stopped
|
status.value = NginxStatus.Stopped
|
||||||
})
|
|
||||||
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
function reload_nginx() {
|
function reload_nginx() {
|
||||||
|
@ -29,9 +30,11 @@ function reload_nginx() {
|
||||||
}).finally(() => get_status())
|
}).finally(() => get_status())
|
||||||
}
|
}
|
||||||
|
|
||||||
function restart_nginx() {
|
async function restart_nginx() {
|
||||||
status.value = NginxStatus.Restarting
|
status.value = NginxStatus.Restarting
|
||||||
ngx.restart().then(r => {
|
await ngx.restart()
|
||||||
|
|
||||||
|
get_status().then(r => {
|
||||||
if (r.level < logLevel.Warn)
|
if (r.level < logLevel.Warn)
|
||||||
message.success($gettext('Nginx restarted successfully'))
|
message.success($gettext('Nginx restarted successfully'))
|
||||||
else if (r.level === logLevel.Warn)
|
else if (r.level === logLevel.Warn)
|
||||||
|
@ -40,7 +43,7 @@ function restart_nginx() {
|
||||||
message.error(r.message)
|
message.error(r.message)
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
message.error(`${$gettext('Server error')} ${e?.message}`)
|
message.error(`${$gettext('Server error')} ${e?.message}`)
|
||||||
}).finally(() => get_status())
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const visible = ref(false)
|
const visible = ref(false)
|
||||||
|
|
|
@ -4,27 +4,13 @@ import (
|
||||||
"github.com/0xJacky/Nginx-UI/settings"
|
"github.com/0xJacky/Nginx-UI/settings"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var mutex sync.Mutex
|
var (
|
||||||
|
mutex sync.Mutex
|
||||||
func execShell(cmd string) (out string) {
|
lastOutput string
|
||||||
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()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConf() (out string) {
|
func TestConf() (out string) {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
@ -53,11 +39,15 @@ func Reload() (out string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func Restart() (out string) {
|
func Restart() {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
|
|
||||||
|
// fix(docker): nginx restart always output network error
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
|
||||||
if settings.NginxSettings.RestartCmd != "" {
|
if settings.NginxSettings.RestartCmd != "" {
|
||||||
out = execShell(settings.NginxSettings.RestartCmd)
|
lastOutput = execShell(settings.NginxSettings.RestartCmd)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -65,15 +55,39 @@ func Restart() (out string) {
|
||||||
pidPath := GetPIDPath()
|
pidPath := GetPIDPath()
|
||||||
daemon := GetSbinPath()
|
daemon := GetSbinPath()
|
||||||
|
|
||||||
out = execCommand("start-stop-daemon", "--stop", "--quiet", "--oknodo", "--retry=TERM/30/KILL/5", "--pidfile", pidPath)
|
lastOutput = execCommand("start-stop-daemon", "--stop", "--quiet", "--oknodo", "--retry=TERM/30/KILL/5", "--pidfile", pidPath)
|
||||||
|
|
||||||
if daemon == "" {
|
if daemon == "" {
|
||||||
out += execCommand("nginx")
|
lastOutput += execCommand("nginx")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
out += execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon)
|
lastOutput += execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetLastOutput() string {
|
||||||
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
|
return lastOutput
|
||||||
|
}
|
||||||
|
|
||||||
|
func execShell(cmd string) (out string) {
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue