mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
enhance: handle websocket error
This commit is contained in:
parent
d6b22c157c
commit
ccb04c07d8
8 changed files with 126 additions and 60 deletions
|
@ -3,6 +3,7 @@ package analytic
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/0xJacky/Nginx-UI/internal/analytic"
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"github.com/0xJacky/Nginx-UI/internal/logger"
|
||||
"github.com/shirou/gopsutil/v3/cpu"
|
||||
"github.com/shirou/gopsutil/v3/host"
|
||||
|
@ -75,10 +76,7 @@ func Analytic(c *gin.Context) {
|
|||
|
||||
// write
|
||||
err = ws.WriteJSON(stat)
|
||||
if err != nil || websocket.IsUnexpectedCloseError(err,
|
||||
websocket.CloseGoingAway,
|
||||
websocket.CloseNoStatusReceived,
|
||||
websocket.CloseNormalClosure) {
|
||||
if helper.IsUnexpectedWebsocketError(err) {
|
||||
logger.Error(err)
|
||||
break
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package analytic
|
|||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/internal/analytic"
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"github.com/0xJacky/Nginx-UI/internal/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
|
@ -27,10 +28,7 @@ func GetNodeStat(c *gin.Context) {
|
|||
for {
|
||||
// write
|
||||
err = ws.WriteJSON(analytic.GetNodeStat())
|
||||
if err != nil || websocket.IsUnexpectedCloseError(err,
|
||||
websocket.CloseGoingAway,
|
||||
websocket.CloseNoStatusReceived,
|
||||
websocket.CloseNormalClosure) {
|
||||
if helper.IsUnexpectedWebsocketError(err) {
|
||||
logger.Error(err)
|
||||
break
|
||||
}
|
||||
|
@ -57,10 +55,7 @@ func GetNodesAnalytic(c *gin.Context) {
|
|||
for {
|
||||
// write
|
||||
err = ws.WriteJSON(analytic.NodeMap)
|
||||
if err != nil || websocket.IsUnexpectedCloseError(err,
|
||||
websocket.CloseGoingAway,
|
||||
websocket.CloseNoStatusReceived,
|
||||
websocket.CloseNormalClosure) {
|
||||
if helper.IsUnexpectedWebsocketError(err) {
|
||||
logger.Error(err)
|
||||
break
|
||||
}
|
||||
|
|
|
@ -32,14 +32,12 @@ func handleIssueCertLogChan(conn *websocket.Conn, log *cert.Logger, logChan chan
|
|||
}()
|
||||
|
||||
for logString := range logChan {
|
||||
|
||||
log.Info(logString)
|
||||
|
||||
err := conn.WriteJSON(IssueCertResponse{
|
||||
Status: Info,
|
||||
Message: logString,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
|
@ -110,7 +108,6 @@ func IssueCert(c *gin.Context) {
|
|||
Status: Error,
|
||||
Message: err.Error(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
|
@ -132,7 +129,7 @@ func IssueCert(c *gin.Context) {
|
|||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
err = ws.WriteJSON(IssueCertResponse{
|
||||
_ = ws.WriteJSON(IssueCertResponse{
|
||||
Status: Error,
|
||||
Message: err.Error(),
|
||||
})
|
||||
|
@ -149,7 +146,6 @@ func IssueCert(c *gin.Context) {
|
|||
SSLCertificateKey: payload.GetCertificateKeyPath(),
|
||||
KeyType: payload.GetKeyType(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
return
|
||||
|
|
|
@ -41,6 +41,18 @@ func GetCurrentVersion(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, curVer)
|
||||
}
|
||||
|
||||
const (
|
||||
UpgradeStatusInfo = "info"
|
||||
UpgradeStatusError = "error"
|
||||
UpgradeStatusProgress = "progress"
|
||||
)
|
||||
|
||||
type CoreUpgradeResp struct {
|
||||
Status string `json:"status"`
|
||||
Progress float64 `json:"progress"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func PerformCoreUpgrade(c *gin.Context) {
|
||||
var upGrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
|
@ -67,49 +79,48 @@ func PerformCoreUpgrade(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
_ = ws.WriteJSON(gin.H{
|
||||
"status": "info",
|
||||
"message": "Initialing core upgrader",
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusInfo,
|
||||
Message: "Initialing core upgrader",
|
||||
})
|
||||
|
||||
u, err := upgrader.NewUpgrader(control.Channel)
|
||||
|
||||
if err != nil {
|
||||
_ = ws.WriteJSON(gin.H{
|
||||
"status": "error",
|
||||
"message": "Initial core upgrader error",
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: "Initial core upgrader error",
|
||||
})
|
||||
_ = ws.WriteJSON(gin.H{
|
||||
"status": "error",
|
||||
"message": err.Error(),
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
logger.Error(err)
|
||||
return
|
||||
}
|
||||
_ = ws.WriteJSON(gin.H{
|
||||
"status": "info",
|
||||
"message": "Downloading latest release",
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusInfo,
|
||||
Message: "Downloading latest release",
|
||||
})
|
||||
progressChan := make(chan float64)
|
||||
go func() {
|
||||
for progress := range progressChan {
|
||||
_ = ws.WriteJSON(gin.H{
|
||||
"status": "progress",
|
||||
"progress": progress,
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusProgress,
|
||||
Progress: progress,
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
tarName, err := u.DownloadLatestRelease(progressChan)
|
||||
|
||||
if err != nil {
|
||||
_ = ws.WriteJSON(gin.H{
|
||||
"status": "error",
|
||||
"message": "Download latest release error",
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: "Download latest release error",
|
||||
})
|
||||
_ = ws.WriteJSON(gin.H{
|
||||
"status": "error",
|
||||
"message": err.Error(),
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
logger.Error(err)
|
||||
return
|
||||
|
@ -119,9 +130,9 @@ func PerformCoreUpgrade(c *gin.Context) {
|
|||
_ = os.Remove(tarName)
|
||||
_ = os.Remove(tarName + ".digest")
|
||||
}()
|
||||
_ = ws.WriteJSON(gin.H{
|
||||
"status": "info",
|
||||
"message": "Performing core upgrade",
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusInfo,
|
||||
Message: "Performing core upgrade",
|
||||
})
|
||||
// dry run
|
||||
if control.DryRun || settings.ServerSettings.Demo {
|
||||
|
@ -132,13 +143,13 @@ func PerformCoreUpgrade(c *gin.Context) {
|
|||
// bye, overseer will restart nginx-ui
|
||||
err = u.PerformCoreUpgrade(u.ExPath, tarName)
|
||||
if err != nil {
|
||||
_ = ws.WriteJSON(gin.H{
|
||||
"status": "error",
|
||||
"message": "Perform core upgrade error",
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: "Perform core upgrade error",
|
||||
})
|
||||
_ = ws.WriteJSON(gin.H{
|
||||
"status": "error",
|
||||
"message": err.Error(),
|
||||
_ = ws.WriteJSON(CoreUpgradeResp{
|
||||
Status: UpgradeStatusError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
logger.Error(err)
|
||||
return
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package upstream
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/internal/helper"
|
||||
"github.com/0xJacky/Nginx-UI/internal/logger"
|
||||
"github.com/0xJacky/Nginx-UI/internal/upstream"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
@ -35,11 +36,7 @@ func AvailabilityTest(c *gin.Context) {
|
|||
|
||||
for {
|
||||
err = ws.WriteJSON(upstream.AvailabilityTest(body))
|
||||
|
||||
if err != nil || websocket.IsUnexpectedCloseError(err,
|
||||
websocket.CloseGoingAway,
|
||||
websocket.CloseNoStatusReceived,
|
||||
websocket.CloseNormalClosure) {
|
||||
if helper.IsUnexpectedWebsocketError(err) {
|
||||
logger.Error(err)
|
||||
break
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue