Przeglądaj źródła

enhance: replace error handling with cosy wrapper for nginx reload and test failures

Jacky 2 tygodni temu
rodzic
commit
d789326230

+ 1 - 3
api/config/add.go

@@ -76,9 +76,7 @@ func AddConfig(c *gin.Context) {
 
 	output := nginx.Reload()
 	if nginx.GetLogLevel(output) >= nginx.Warn {
-		c.JSON(http.StatusInternalServerError, gin.H{
-			"message": output,
-		})
+		cosy.ErrHandler(c, cosy.WrapErrorWithParams(config.ErrNginxReloadFailed, output))
 		return
 	}
 

+ 8 - 9
app/src/layouts/HeaderLayout.vue

@@ -35,13 +35,6 @@ const isWorkspace = computed(() => {
     <div class="tool">
       <MenuUnfoldOutlined @click="emit('clickUnFold')" />
     </div>
-    <div v-if="!isWorkspace" class="workspace-entry">
-      <RouterLink to="/workspace">
-        <ATooltip :title="$gettext('Workspace')">
-          <DesktopOutlined />
-        </ATooltip>
-      </RouterLink>
-    </div>
 
     <ASpace
       class="user-wrapper"
@@ -51,6 +44,14 @@ const isWorkspace = computed(() => {
 
       <SwitchAppearance />
 
+      <div v-if="!isWorkspace" class="workspace-entry">
+        <RouterLink to="/workspace">
+          <ATooltip :title="$gettext('Workspace')">
+            <DesktopOutlined />
+          </ATooltip>
+        </RouterLink>
+      </div>
+
       <Notification :header-ref="headerRef" />
 
       <NginxControl />
@@ -98,8 +99,6 @@ const isWorkspace = computed(() => {
 }
 
 .workspace-entry {
-  position: absolute;
-  left: 20px;
   @media (max-width: 600px) {
     display: none;
   }

+ 2 - 0
internal/config/errors.go

@@ -6,4 +6,6 @@ var (
 	e                                = cosy.NewErrorScope("config")
 	ErrPathIsNotUnderTheNginxConfDir = e.New(50006, "path: {0} is not under the nginx conf dir: {1}")
 	ErrDstFileExists                 = e.New(50007, "destination file: {0} already exists")
+	ErrNginxTestFailed               = e.New(50008, "nginx test failed: {0}")
+	ErrNginxReloadFailed             = e.New(50009, "nginx reload failed: {0}")
 )

+ 2 - 2
internal/config/save.go

@@ -1,13 +1,13 @@
 package config
 
 import (
-	"fmt"
 	"os"
 
 	"github.com/0xJacky/Nginx-UI/internal/helper"
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/query"
+	"github.com/uozi-tech/cosy"
 	"gorm.io/gen/field"
 )
 
@@ -47,7 +47,7 @@ func Save(absPath string, content string, cfg *model.Config) (err error) {
 
 	output := nginx.Reload()
 	if nginx.GetLogLevel(output) >= nginx.Warn {
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
 	}
 
 	err = SyncToRemoteServer(cfg)

+ 7 - 5
internal/site/disable.go

@@ -2,15 +2,17 @@ package site
 
 import (
 	"fmt"
+	"net/http"
+	"os"
+	"runtime"
+	"sync"
+
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/go-resty/resty/v2"
+	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
-	"net/http"
-	"os"
-	"runtime"
-	"sync"
 )
 
 // Disable disables a site by removing the symlink in sites-enabled
@@ -35,7 +37,7 @@ func Disable(name string) (err error) {
 
 	output := nginx.Reload()
 	if nginx.GetLogLevel(output) > nginx.Warn {
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
 	}
 
 	go syncDisable(name)

+ 3 - 2
internal/site/enable.go

@@ -11,6 +11,7 @@ import (
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/go-resty/resty/v2"
+	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 )
 
@@ -37,12 +38,12 @@ func Enable(name string) (err error) {
 	output := nginx.TestConf()
 	if nginx.GetLogLevel(output) > nginx.Warn {
 		_ = os.Remove(enabledConfigFilePath)
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
 	}
 
 	output = nginx.Reload()
 	if nginx.GetLogLevel(output) > nginx.Warn {
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
 	}
 
 	go syncEnable(name)

+ 2 - 0
internal/site/errors.go

@@ -8,4 +8,6 @@ var (
 	ErrDstFileExists       = e.New(50001, "destination file already exists")
 	ErrSiteIsEnabled       = e.New(50002, "site is enabled")
 	ErrSiteIsInMaintenance = e.New(50003, "site is in maintenance mode")
+	ErrNginxTestFailed     = e.New(50004, "nginx test failed: {0}")
+	ErrNginxReloadFailed   = e.New(50005, "nginx reload failed: {0}")
 )

+ 3 - 2
internal/site/maintenance.go

@@ -16,6 +16,7 @@ import (
 	"github.com/go-resty/resty/v2"
 	"github.com/tufanbarisyildirim/gonginx/config"
 	"github.com/tufanbarisyildirim/gonginx/parser"
+	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 	cSettings "github.com/uozi-tech/cosy/settings"
 )
@@ -82,13 +83,13 @@ func EnableMaintenance(name string) (err error) {
 		if helper.FileExists(originalEnabledPath + "_backup") {
 			_ = os.Rename(originalEnabledPath+"_backup", originalEnabledPath)
 		}
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
 	}
 
 	// Reload nginx
 	output = nginx.Reload()
 	if nginx.GetLogLevel(output) > nginx.Warn {
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
 	}
 
 	// Synchronize with other nodes

+ 3 - 2
internal/site/save.go

@@ -14,6 +14,7 @@ import (
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/query"
 	"github.com/go-resty/resty/v2"
+	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 )
 
@@ -40,13 +41,13 @@ func Save(name string, content string, overwrite bool, envGroupId uint64, syncNo
 		output := nginx.TestConf()
 
 		if nginx.GetLogLevel(output) > nginx.Warn {
-			return fmt.Errorf("%s", output)
+			return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
 		}
 
 		if postAction == model.PostSyncActionReloadNginx {
 			output = nginx.Reload()
 			if nginx.GetLogLevel(output) > nginx.Warn {
-				return fmt.Errorf("%s", output)
+				return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
 			}
 		}
 	}

+ 7 - 5
internal/stream/disable.go

@@ -2,15 +2,17 @@ package stream
 
 import (
 	"fmt"
+	"net/http"
+	"os"
+	"runtime"
+	"sync"
+
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/go-resty/resty/v2"
+	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
-	"net/http"
-	"os"
-	"runtime"
-	"sync"
 )
 
 // Disable disables a site by removing the symlink in sites-enabled
@@ -35,7 +37,7 @@ func Disable(name string) (err error) {
 
 	output := nginx.Reload()
 	if nginx.GetLogLevel(output) > nginx.Warn {
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
 	}
 
 	go syncDisable(name)

+ 8 - 6
internal/stream/enable.go

@@ -2,15 +2,17 @@ package stream
 
 import (
 	"fmt"
+	"net/http"
+	"os"
+	"runtime"
+	"sync"
+
 	"github.com/0xJacky/Nginx-UI/internal/helper"
 	"github.com/0xJacky/Nginx-UI/internal/nginx"
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/go-resty/resty/v2"
+	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
-	"net/http"
-	"os"
-	"runtime"
-	"sync"
 )
 
 // Enable enables a site by creating a symlink in sites-enabled
@@ -36,12 +38,12 @@ func Enable(name string) (err error) {
 	output := nginx.TestConf()
 	if nginx.GetLogLevel(output) > nginx.Warn {
 		_ = os.Remove(enabledConfigFilePath)
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
 	}
 
 	output = nginx.Reload()
 	if nginx.GetLogLevel(output) > nginx.Warn {
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
 	}
 
 	go syncEnable(name)

+ 2 - 0
internal/stream/errors.go

@@ -7,4 +7,6 @@ var (
 	ErrStreamNotFound  = e.New(40401, "stream not found")
 	ErrDstFileExists = e.New(50001, "destination file already exists")
 	ErrStreamIsEnabled = e.New(50002, "stream is enabled")
+	ErrNginxTestFailed = e.New(50003, "nginx test failed: {0}")
+	ErrNginxReloadFailed = e.New(50004, "nginx reload failed: {0}")
 )

+ 3 - 2
internal/stream/rename.go

@@ -7,6 +7,7 @@ import (
 	"github.com/0xJacky/Nginx-UI/internal/notification"
 	"github.com/0xJacky/Nginx-UI/query"
 	"github.com/go-resty/resty/v2"
+	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 	"net/http"
 	"os"
@@ -49,13 +50,13 @@ func Rename(oldName string, newName string) (err error) {
 	// test nginx configuration
 	output := nginx.TestConf()
 	if nginx.GetLogLevel(output) > nginx.Warn {
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
 	}
 
 	// reload nginx
 	output = nginx.Reload()
 	if nginx.GetLogLevel(output) > nginx.Warn {
-		return fmt.Errorf("%s", output)
+		return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
 	}
 
 	go syncRename(oldName, newName)

+ 3 - 2
internal/stream/save.go

@@ -14,6 +14,7 @@ import (
 	"github.com/0xJacky/Nginx-UI/model"
 	"github.com/0xJacky/Nginx-UI/query"
 	"github.com/go-resty/resty/v2"
+	"github.com/uozi-tech/cosy"
 	"github.com/uozi-tech/cosy/logger"
 )
 
@@ -40,13 +41,13 @@ func Save(name string, content string, overwrite bool, syncNodeIds []uint64, pos
 		output := nginx.TestConf()
 
 		if nginx.GetLogLevel(output) > nginx.Warn {
-			return fmt.Errorf("%s", output)
+			return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
 		}
 
 		if postAction == model.PostSyncActionReloadNginx {
 			output = nginx.Reload()
 			if nginx.GetLogLevel(output) > nginx.Warn {
-				return fmt.Errorf("%s", output)
+				return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
 			}
 		}
 	}